Celery and background jobs · mid
Idempotent tasks, safe retries and long tasks
A task can run more than once, so it must be safe to repeat. Retry only temporary errors, with backoff and jitter. A task that does a money step and a notification must keep them separate.
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
- A task runs twice because of: autoretry after a later step failed, acks_late and a worker crash or deploy, the Redis visibility timeout, or the producer queuing it twice.
- Idempotent means running it again for the same order_id changes nothing. Insert a ledger row with a unique constraint on (order_id, kind) inside transaction.atomic() first; an IntegrityError means it is already done.
- A balance update with balance -= x; save() loses updates. Use a conditional update with F(), or select_for_update().
- Retry only temporary errors (timeouts, connection errors, 429, 5xx) with retry_backoff=True, retry_jitter and a cap. Business errors and a 400 fail at once. autoretry_for=(Exception,) is too wide.
- Send the SMS in a separate task queued with transaction.on_commit after the debit, so a failed message never redoes the money step.
- OTP task: bind=True, max_retries, explicit connect and read timeouts, self.retry(exc=exc, countdown=...), expires=300 so a backlog does not send stale OTPs, soft_time_limit and time_limit, its own high-priority queue. Never log the OTP; store only a hash with an expiry.
- A 90-minute report with acks_late on Redis returns to the queue after an hour, and again and again. Tonight: acks_late=False for that task or a larger visibility_timeout. Proper fix: split into short chunks and add a unique report_runs(date) row so a second run exits early.
- A global change to acks_late needs a review of long tasks and idempotency first. Also use reject_on_worker_lost=True with acks_late and keep tasks short.
The red pen
Common traps: mistake → fix
Mistake: autoretry_for=(Exception,) is a safe default
Fix: it retries bugs and repeats completed steps.
Mistake: acks_late alone fixes lost tasks
Fix: it creates duplicates unless the task is idempotent.
Mistake: A failed SMS should retry the whole task
Fix: the money step must not run again.
Mistake: Retry forever with no delay
Fix: use backoff, jitter and a cap.
Easy to hard
Questions interviewers ask, easy to hard
- Why does this Celery task sometimes debit a wallet twice?
- How do you make it idempotent?
- Write an OTP SMS task that retries timeouts and 5xx, not 400.
- The nightly report starts again every hour after we enabled acks_late. Why?
- What do you change tonight and what do you fix properly?
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.