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 theredisclient.- 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:7All lessons in this course
- Lightweight Offloading with BackgroundTasks
- Wiring Celery Workers to a FastAPI App
- Retries, Idempotency and Dead-Letter Handling
- Scheduled and Periodic Jobs with Celery Beat