Celery and background jobs · core
How Celery works, and Redis or RabbitMQ as the broker
The web app publishes a message to a broker, workers consume it, and an optional result backend stores outcomes. When a message is acknowledged decides whether a crash loses a task or runs it twice, and Redis visibility timeout can run a long task again.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- The producer serialises the task name and arguments (JSON by default) into a message and publishes it to a queue on the broker: Redis, RabbitMQ or SQS.
- Workers consume from queues. The default prefork pool runs tasks in child processes; concurrency defaults to the number of CPUs.
- By default the message is acknowledged when the worker receives it, before the task runs (early ack): a crash mid-task loses it. acks_late=True acknowledges after the task, so the task may run twice.
- Each worker reserves worker_prefetch_multiplier (default 4) times its concurrency in messages. For long tasks use 1 with acks_late, or idle workers sit beside a busy one.
- The result backend (Redis or a database) stores states and return values and is needed for .get(), chords and status. Many tasks need none (ignore_result=True). Beat is a separate scheduler process; it sends tasks but does not run them.
- Redis: simple and fast. A message a worker took but did not acknowledge returns to the queue after the visibility timeout (1 hour by default), so a task running or waiting longer than that is delivered again. Raise visibility_timeout in broker_transport_options.
- Redis as a broker must not evict Celery keys: use a noeviction policy, turn on persistence if queued tasks matter, and use separate instances for cache and broker.
- RabbitMQ is a real broker with per-message acks, routing, priorities and replicated quorum queues; unacked messages return when the consumer channel closes. SQS is managed and also has a visibility timeout.
The red pen
Common traps: mistake → fix
Mistake: The task is deleted when the worker starts it
Fix: by default it is acknowledged then, so a crash loses it.
Mistake: acks_late guarantees exactly once
Fix: it gives at least once; tasks must be idempotent.
Mistake: Beat runs the tasks
Fix: it only sends them to the broker.
Mistake: One Redis for cache and broker is simpler and safe
Fix: a cache eviction or flush can drop queued tasks.
Easy to hard
Questions interviewers ask, easy to hard
- Explain how Celery works: broker, worker, result backend.
- What happens between .delay() and the task running?
- What does acks_late change?
- Redis or RabbitMQ as the broker?
- Why does a long task run again on Redis?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.