Skip to the page

Celery and background jobs · mid

Beat, chords and tasks that hang

Only one beat may run for a schedule. Canvas (chain, group, chord) builds workflows but needs care with failures and result size. A hung task holds a worker slot, and prefetched tasks wait behind it, so every network call needs a timeout.

The notes

What you must know

  • Beat is a scheduler, not a worker. Each beat process keeps its own schedule and sends every due task, so two beats send each task twice. Run exactly one, with the Recreate strategy in Kubernetes, or use RedBeat (a Redis lock) or Kubernetes CronJobs with concurrencyPolicy: Forbid.
  • Make scheduled jobs idempotent: a statement_sent(user_id, period) row with a unique constraint, with a fan-out of per-user tasks keyed by user and date.
  • Celery uses UTC unless timezone is set. crontab(hour=3, minute=30) in UTC is 9:00 IST. Set timezone = "Asia/Kolkata" if you mean local time.
  • Chain: tasks in order. Group: tasks in parallel. Chord: a group plus a callback after all finish; it needs a result backend. Use chunks (100 tasks of 100 invoices), not 10,000 tiny tasks.
  • By default a failing header task makes the chord fail and the callback never runs. Better: header tasks catch their own errors, record them on a batch row and return. Keep results small (S3 keys).
  • Never call result.get() inside a task: workers wait on tasks that need the same slots and can deadlock. A unique (customer, month) constraint on the batch row prevents double starts.
  • Stuck tasks: celery inspect active and reserved, worker logs, py-spy dump on the child process. Revoke with --terminate or restart; with early ack the tasks are lost and must be re-queued from the database, with acks_late they return.
  • Prevent: a timeout on every network call, task_soft_time_limit (raises SoftTimeLimitExceeded) and task_time_limit (kills the child), separate queues for slow partners, prefetch multiplier 1 for long tasks, and alerts on queue length and age of the oldest message.

The red pen

Common traps: mistake → fix

  • Mistake: Two beat replicas give high availability

    Fix: they send every task twice.

  • Mistake: result.get() inside a task is fine

    Fix: it can deadlock the pool.

  • Mistake: Chord callback always runs

    Fix: not if a header task fails.

  • Mistake: Workers are online, so the system is healthy

    Fix: all slots may hold hung tasks.

Easy to hard

Questions interviewers ask, easy to hard

  1. Someone scaled beat to 2 replicas and every customer got the statement twice. What do you do?
  2. How do you get high availability for beat without duplicates?
  3. How would you build 10,000 invoice PDFs, zip them and email one link?
  4. Flower shows the same 16 tasks active for two hours and 12,000 waiting. What do you do?
  5. What stops it next time?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview