0PricingLogin
FastAPI Backend Development Bootcamp · Lesson

Wiring Celery Workers to a FastAPI App

Configure Celery with a Redis broker, define tasks, and dispatch jobs from endpoints with result backends.

Why Celery in a FastAPI App?

FastAPI request handlers must return quickly. Work like sending emails, generating reports, resizing images, or calling slow third-party APIs can take seconds, blocking your worker process and hurting throughput.

Celery is a distributed task queue. You push a job onto a queue, and a separate pool of worker processes runs it outside the request/response cycle.

  • Broker — the message transport that holds queued jobs (we use Redis).
  • Worker — long-running process that pulls and executes tasks.
  • Result backend — optional store for return values and task state.

The endpoint stays fast and just answers: "accepted, here is your job id."

Installing the Pieces

Install Celery with the Redis extra, plus a Redis server reachable from both your API and your workers.

  • celery[redis] pulls in Celery and the redis client.
  • Run Redis locally with Docker: docker run -p 6379:6379 redis.

Both the FastAPI process and the worker process import the same Celery application object, so they must share the same codebase and broker URL.

# requirements.txt
fastapi
uvicorn[standard]
celery[redis]
redis

# install
# pip install -r requirements.txt
# run redis: docker run -p 6379:6379 redis:7

All lessons in this course

  1. Lightweight Offloading with BackgroundTasks
  2. Wiring Celery Workers to a FastAPI App
  3. Retries, Idempotency and Dead-Letter Handling
  4. Scheduled and Periodic Jobs with Celery Beat
← Back to FastAPI Backend Development Bootcamp