Engineering notes
Reliable Background Jobs for Business Workflows
Design reports, imports and notifications with durable job states, safe retries and recovery that survives worker restarts.
Moving work into a queue makes a request return sooner, but it also moves failure somewhere less visible. A hypothetical reporting application illustrates the real requirement: the user needs to know whether a report is waiting, running, finished or blocked, even after a deployment restarts every worker.
Persist the user's intention
Create a durable job record with an owner, parameters, request time and operation identifier. Commit it with the business change that requires the work, or use a durable publication mechanism to bridge the database and queue. A broker message alone is a poor long-term support record.
Return an accepted state with a job reference. Avoid presenting queued as completed. Store a version of the parameters and relevant policy so a later code change does not silently reinterpret yesterday's request. Validate permissions again when execution can occur long after scheduling.
Generated editorial illustration; not a record of a live system.
Make repeated execution safe
A worker can fail after producing a result and before acknowledging a message. Design the business effect so the same operation can be recognized and resumed or skipped. A unique result association, guarded state transition or provider-supported idempotency key is more useful than assuming a job runs once.
Celery's acknowledgement and retry options have specific failure semantics; select them according to the task and test the configuration. Late acknowledgement alone is not an exactly-once guarantee. For external effects such as sending a document, preserve the request reference and define how an uncertain outcome is reconciled.
Bound time and resource consumption
Set deadlines for external calls and limit the size of each work unit. A large export can process stable batches and checkpoint progress, while a short notification should not wait behind an hours-long report. Separate queues or worker pools when their resource needs and priorities differ.
Cap retries, classify errors and use backoff. Bad input belongs in a correction state rather than an infinite loop. Limit concurrency against the database and downstream APIs. Adding workers without a connection budget can slow the entire application while the queue appears to drain faster.
Give operators a controlled recovery path
Expose job age, current phase, last error category and the next permitted action. Keep detailed sensitive payloads out of broad logs. A retry button should reuse the original operation identity and the same safeguards as normal execution. Cancellation needs a defined boundary: stopping future work cannot retract an email already delivered.
Retain failed jobs long enough to investigate and document when results expire. Alert on old pending work and repeated failures, not only worker process health. A healthy worker with an unprocessed business backlog is still an operational problem.
Test interruption at meaningful boundaries
Stop a worker before processing, after a checkpoint, after committing a result and before acknowledgement. Repeat the same job, deploy a compatible new worker while old work remains queued, and simulate an unavailable external service. Check the user-facing state after every interruption.
Acceptance means the job reaches a correct result or a visible, owned exception without repeating its business effect. Record recovery steps in the handover. Measure completion age and unresolved failures from actual workloads; a background queue does not itself establish a performance or cost improvement.
Worked example and implementation boundary
A fictional export job J-12 records a stable input snapshot and finishes batch 1 before its worker restarts. The next worker reads the committed checkpoint and continues at batch 2. A repeated message for J-12 finds the same job rather than creating another export. This requires the checkpoint and the corresponding local result to be consistent. If the task sends an external message, a local checkpoint alone cannot establish whether the receiver already accepted it; that effect needs separate identity and reconciliation.
Trade-offs and limitations
Smaller batches reduce restart cost but add checkpoint overhead. Separate worker pools protect short jobs at the cost of capacity planning and more queues. Select acknowledgement behaviour from the broker and worker's actual failure semantics, rather than treating one option as a guarantee.
Common pitfalls
Avoid storing only a broker task ID, acknowledging before a required durable boundary, or adding workers without a database connection budget. A cancellation flag cannot undo an external action already completed.
Key takeaways
Persist the intention, make repeated effects safe, bound work and expose a recovery owner. Test interruption after a result commit, not just before work begins. The user-facing state must remain understandable through every restart.
References and further reading
Related reading and implementation support
Related content
Backend Development, APIs and System Design
Backend systems, databases and APIs that connect your interfaces, apply business rules and keep information moving between the tools your team uses.
Payment Webhooks: Handle Duplicates and Late Events
Keep payment events from creating duplicate orders. A practical design for signature checks, durable inboxes, retries and visible exceptions.
Define the implementation boundary
Discuss reliable background jobs in the context of your current systems, decision owners and exception paths, then define a bounded first step.
Discuss your project
