Background Jobs: Make Work Durable Beyond the Request
Reason about durable background work through explicit lifecycle state, ownership, acknowledgement, redelivery, bounded retries, idempotent effects, cancellation, poison jobs, and operational backlog signals.
Personal learning atlas by Tran Trong Thuc · About this Atlas · Atlas last updated Nov 27, 2026
Background Jobs: Make Work Durable Beyond the Request
Constructed example, not a documented incident: A campaign worker is supposed to email 100 customers. It has sent emails to 60 recipients when its container exceeds the configured memory limit and stops; Kubernetes may report container termination reason OOMKilled (a status reason, not a Unix signal). In a delivery setup that requeues unfinished work, another worker receives the logical job and begins from the first recipient. Without durable checkpoints and duplicate-safe notification identity, the first 60 recipients could get another email. This outcome depends on the broker acknowledgement/recovery policy and email provider; it is not evidence of a real mass-mailing outage.
A background job is not simply a function moved off the HTTP request. If the business needs durable completion, the design must define persistence, claim ownership, retry/recovery, safe checkpoints and external-effect semantics. Not all task runners use expiring leases or make the same delivery guarantees.
TL;DR
💡 Rule of thumb: For work promised to survive request/worker failure, treat the job as durable state plus a repeatable processing protocol. Persist accepted intent before reporting durable acceptance, checkpoint at safe milestones, and protect or reconcile irreversible effects when attempts overlap or recur. A local job record alone cannot guarantee an external provider never repeats an action.
- Durable lifecycle state machine: Model jobs with explicit state transitions (
queued,running,succeeded,failed,cancelled); the job's identity and progress must live in durable storage that outlives ephemeral container pods or worker crashes. - Logical identity vs. execution attempts: Separate the permanent business identity of the job from transient attempt counters. When a worker dies, redelivery must resume or reconcile existing work rather than creating a second uncoordinated business effect.
- Claiming and recovery are transport-specific: Some job stores use expiring leases and heartbeat renewals; others rely on broker acknowledgements, channel-loss requeue or framework-specific time limits. Define who can claim work, how stale owners are fenced or reconciled, and when failed work becomes eligible again.
- Progress and cancellation contracts: When a batch must resume without repeating effects, persist checkpoints at safe boundaries, and define cooperative cancellation where workers can observe it. Neither cancellation nor a progress counter automatically rolls back committed external work.
- High-risk boundary: A process-only task or early ACK can lose accepted work if there is no other recovery record. A restarted batch may also repeat non-idempotent emails/payments; the exact result depends on broker configuration and external-provider protection.
Do not model a job as “call this function later.” Model it as durable state plus a repeatable processing protocol.
1. Persist intent before returning success
Consider an API that starts an invoice export:
POST /exports
start async task in process memory
return 202 AcceptedIf the process crashes after returning 202 but before the task reaches durable storage, the user has an accepted request with no work left to execute.
A stronger shape is:
request
-> validate + authorize
-> create durable job/business record
-> commit
-> make work available to workers
-> return stable job identityThe exact transport can be a database-backed job table, a managed task service, or a message broker. The invariant is the same: once the system tells the caller that work was accepted, durable state must exist from which the work can be recovered or reconciled.
For workflows that must update business state and publish work atomically, a transactional outbox or equivalent pattern may be needed. This lesson does not replace that deeper cross-system delivery topic; it establishes why volatile “fire-and-forget” work is not enough.
2. Give every logical job a stable identity
A useful job record usually separates logical identity from execution attempt:
job_id: job_01K...
job_type: generate_invoice_pdf
subject_id: invoice_1842
state: queued
attempt_count: 0
max_attempts: 5
next_attempt_at: now
created_at: ...The logical job is “generate the PDF for invoice 1842.” An attempt is one worker's try at that job.
This distinction matters because one logical job may have several attempts after timeouts or worker crashes. If every attempt creates a brand-new business effect, retries become duplication.
Useful durable fields depend on the system, but often include:
- stable job identifier;
- job type and schema/version;
- subject or idempotency identity;
- lifecycle state;
- attempt count and retry budget;
- next eligible time;
- claim/lease owner and expiry when the mechanism exposes them;
- cancellation request;
- bounded progress metadata;
- terminal result or failure category;
- timestamps for queue age and execution duration.
Avoid putting arbitrary large payloads or secrets in a job record just because it is convenient. Prefer stable references to durable source data, and authorize the worker to fetch only what it needs.
3. Claiming work is an ownership protocol
Multiple workers can poll the same backlog. They need a rule for who may act on one job at a time.
A generic claim/lease model looks like:
Different systems expose this differently. A database-backed worker may atomically transition queued -> running or use row locking. A queue may hide a delivered message for a visibility timeout. A task framework may reserve a task and acknowledge it before or after execution.
The portable questions are:
- Is claiming atomic?
- Is ownership permanent or lease-based?
- Can a long-running worker renew ownership?
- What happens if the lease expires while the original worker is still running?
- What durable check prevents two stale/current owners from committing conflicting effects?
A lease reduces simultaneous work; it does not automatically make the business effect exactly once.
4. Acknowledge only at the boundary your reliability model intends
Acknowledgement tells the job transport that a delivery no longer needs to be offered for processing.
The timing changes failure behavior:
ack before work
-> fewer duplicate executions after worker loss
-> greater risk of losing unfinished work
ack after work
-> unfinished work can be redelivered after worker loss
-> duplicate execution becomes possibleCelery's task configuration makes this trade-off explicit: task_acks_late moves ACK after execution, but does not automatically guarantee redelivery when a worker child process dies. Celery normally acknowledges abruptly lost child work even with late ACK; enabling task_reject_on_worker_lost can instead requeue it, with a risk of repeated failure loops. Failure/retry and acknowledgement options must be inspected together. Amazon SQS hides received work temporarily but can redeliver when visibility expires, and standard queues can deliver a duplicate even during visibility.
Those are concrete implementations of a general rule: reliable recovery usually implies that a job may be delivered or executed more than once.
Do not infer exactly-once business effects from “the queue has deduplication” or “the worker hides the message.” Delivery behavior and effect semantics are different layers.
5. Design for at-least-once/redelivery at the effect boundary
Suppose a worker performs:
1. charge customer
2. mark job succeeded
3. acknowledge deliveryIf it crashes after step 1 but before steps 2–3, the job may be redelivered. Re-running the charge blindly can bill twice.
The safe question is not “how do we prevent the worker from ever running twice?” It is:
If this logical job runs again after an ambiguous failure, how does it reach the same correct durable outcome without repeating an irreversible effect?
Patterns include:
- send a stable idempotency key to a remote API that supports it;
- use a unique constraint or conditional transition so one logical effect can commit once;
- record an operation identity before publishing downstream work;
- make output paths deterministic/versioned and publish one durable pointer;
- reconcile ambiguous outcomes by querying the source of truth before repeating the effect.
6. Retry transient failure; classify permanent failure
Retries are for failures that may succeed later, such as:
- a temporary network error;
- a dependency returning overload/temporary-unavailable signals;
- a short-lived database conflict;
- a lease lost before an effect committed;
- a provider rate limit with a meaningful retry window.
Retries are usually wrong for deterministic failures such as:
- malformed input that will not change;
- a permanently missing required resource;
- unsupported file format;
- failed authorization that should not become valid by waiting;
- a business rule rejection;
- code that crashes identically on every attempt.
A bounded retry policy needs explicit limits:
attempt 1 -> fail transiently
wait with backoff + jitter
attempt 2 -> fail transiently
wait longer
attempt 3 -> succeedGoogle Cloud Tasks exposes controls such as maxAttempts (counting the initial attempt), maxRetryDuration, and backoff intervals. Its API does not necessarily stop when the first of the attempt-count and duration limits is reached: retry stops after both conditions hold or the task succeeds, subject to maximum task retention. Choose effective finite bounds for the application's failure policy rather than assuming the mere presence of a maxAttempts field is a hard cap in every implementation.
Unbounded immediate retries create a hot loop that can amplify an outage and starve healthy work.
7. Poison jobs need a terminal path
A poison job is work that repeatedly fails in a way ordinary retries cannot repair. Leaving it in the normal retry cycle forever creates backlog churn, noisy alerts, and wasted capacity.
After the retry budget is exhausted, transition to a durable terminal state or dead-letter path with enough context to diagnose and safely replay the work later.
Useful failure metadata may include:
failure_class: unsupported_payload_version
last_error_code: PAYLOAD_SCHEMA_OLD
attempt_count: 5
first_failed_at: ...
last_failed_at: ...
subject_id: invoice_1842Avoid storing unbounded stack traces, credentials, or sensitive payloads. Keep operational evidence useful but appropriately scoped.
A dead-letter queue is one implementation. A failed table/state plus an operator replay command may be simpler. The architectural requirement is visible terminal ownership, not a specific broker feature.
8. Cancellation is a state transition, not process killing
Cancellation can arrive while a job is queued or running.
For queued work, the system may transition directly to cancelled if execution has not begun. Running work is harder: the worker may already have produced partial effects.
Prefer cooperative cancellation:
worker starts attempt
-> periodically checks durable cancellation signal at safe boundaries
-> stops before next irreversible effect
-> cleans up temporary resources
-> records cancelled terminal stateHard-killing a worker process is an operational last resort, not a general cancellation protocol. Current Celery worker documentation explicitly warns that terminating a task can terminate the process executing it and should not be used programmatically as normal task cancellation.
Define cancellation semantics per job type:
- Is cancellation best effort or guaranteed before a named commit point?
- Which effects can already have happened?
- Is compensation required?
- Can the caller distinguish
cancel_requested,cancelled, andalready_succeeded?
9. Progress is durable evidence, not a chatty counter
Long jobs often need progress reporting, but 47% is meaningful only when the job has a measurable denominator.
Prefer progress tied to durable milestones:
state: running
phase: rendering_pages
completed_units: 47
total_units: 120
updated_at: ...Update progress at a bounded frequency. Writing one database row per processed byte or item can make progress reporting more expensive than the work itself.
If exact percentage is impossible, expose phases such as queued, fetching, processing, publishing, succeeded rather than inventing precision.
The client-facing API should read durable job state. It should not need a direct connection to whichever worker happens to own the current attempt.
10. Worker crashes are normal design inputs
Ask what happens if a worker crash occurs at every boundary:
before claim
immediately after claim
mid-computation
just after a remote effect
just after local commit
just before acknowledgementFor each boundary, the result should be one of:
- safe redelivery/retry;
- durable success already visible;
- reconciliation of an ambiguous external effect;
- terminal failure requiring operator action.
If the answer is “the worker remembers what happened in RAM,” the recovery model is incomplete.
A robust worker should also have explicit execution limits. Network calls need timeouts; long jobs may need heartbeats or lease renewal; memory/CPU-heavy work may need resource limits. A wedged worker that holds ownership forever is a reliability failure even when it has not crashed.
11. Backlog is part of user-visible latency
Moving work off the request path changes the latency model:
job completion latency
= queue wait
+ execution time
+ retry/backoff delay
+ downstream waitThat makes queue age or oldest-job age especially valuable. A queue with 10,000 tiny jobs may be healthy; 100 jobs that have waited six hours may not be.
Track signals such as:
- enqueue rate and completion rate;
- backlog depth;
- queue age / oldest eligible job age;
- claim-to-start delay;
- execution duration by job type;
- success, retry, and terminal-failure rates;
- attempts per completed job;
- lease expiry/redelivery rate;
- cancellation latency;
- worker utilization and saturation.
Scale workers from downstream capacity, not just backlog panic. Adding workers can overload the database or remote dependency the jobs call.
12. Separate job semantics from queue topology
A background-job system needs a way to persist and deliver work, but that does not mean every lesson about messaging belongs here.
This lesson owns these questions:
- What does one logical job mean?
- What is its durable lifecycle?
- How is an attempt claimed and completed?
- How are duplicates, retries, cancellation, progress, and poison work handled?
The Message Queues lesson owns broader transport questions such as competing consumers, partitioning, ordering, broker retention, fan-out, and queue-specific capacity behavior.
Keeping the boundary clear prevents a common design mistake: choosing a broker first and only later discovering that nobody defined the job's durable correctness semantics.
Hypothetical production scenario: thumbnail worker sends duplicate notifications
Constructed example, not a reported incident: A media service creates one durable GENERATE_THUMBNAILS job per upload. The worker does this:
1. render thumbnails
2. send "media ready" email
3. mark job succeeded
4. acknowledge deliveryDuring a deployment, a worker crash happens after step 2 but before steps 3–4. The delivery becomes eligible again and another worker reruns the job. Thumbnails are overwritten harmlessly, but the user receives a second email. The same failure repeats for several jobs, and support sees duplicate notifications without obvious failed jobs.
Impact: users may receive duplicate notifications and incur extra email delivery cost, while operators may be unable to distinguish retry recovery from fresh work. This is a possible effect, not a measured incident.
Root cause: the job transport was designed for redelivery, but the email side effect had no stable idempotency identity. The system treated “one delivery” as equivalent to “one business effect,” and acknowledgement happened after an irreversible effect without a duplicate-safe boundary.
Correct pattern: give the logical notification a durable identity such as (job_id, notification_type) and atomically reserve/schedule the notification. If the email provider supports an idempotency key, reuse it; otherwise a successful send followed by a crash before local confirmation can still be ambiguous, and a local unique key alone cannot guarantee exactly one delivered email. Choose explicit reconciliation or at-least-once notification semantics when necessary. Keep rendering outputs deterministic, record completion durably before the chosen acknowledgement boundary, and monitor redelivery and attempts per job.
The deeper rule is: a background job is reliable only when retrying the whole protocol is safe at every crash boundary that the delivery system can expose.
Common mistakes
“We returned 202, so the job exists”
An HTTP response is not durable work. Persist the accepted intent before claiming success to the caller, or use a workflow that can reconcile a publish gap.
“The queue prevents duplicates”
Some transports offer deduplication features, but worker loss, visibility expiry, acknowledgement timing, producer retries, or downstream ambiguity can still create repeated execution/effects. Design logical effects to tolerate redelivery.
“Retry every exception”
Classify failure. Permanent errors should terminate visibly instead of consuming the retry budget forever.
“More workers always drain backlog faster”
Workers consume downstream resources. Scale within database, API, CPU, memory, and connection budgets.
“Cancellation means kill the worker”
Process termination can leave partial effects and may affect unrelated work depending on the runtime. Prefer cooperative cancellation at explicit safe boundaries.
“Progress belongs in worker memory”
Workers restart and ownership moves. Persist bounded progress if users or operators need it.
Self-check
A job charges a partner API and then updates its local state to succeeded. The partner call times out, so the worker cannot tell whether the charge happened. The job is configured for three automatic retries.
Should the worker immediately retry the charge?
Show the reasoning
Not until the ambiguous external effect is resolved. A timeout says the worker did not receive a response; it does not prove the partner did nothing.
Use a stable operation/idempotency key if the partner supports one, or query/reconcile the partner's source of truth using the logical operation identity before issuing another irreversible charge. Automatic retry is safe only when the effect boundary is idempotent or the ambiguous outcome can be reconciled.
Background-job review checklist
- Durability: Does accepted work have durable state before the request reports success?
- Identity: Is there one stable logical job identity separate from execution attempts?
- Lifecycle: Are queued, running/retrying, succeeded, failed, and cancelled states explicit enough for recovery?
- Claim: Is worker ownership acquired atomically, and is lease/visibility expiry behavior understood?
- Acknowledgement: Is the ack boundary chosen deliberately relative to the durable effect?
- Redelivery: Can the same logical job execute again after worker loss without corrupting business state?
- Idempotency: Do irreversible or remote effects have stable duplicate protection or reconciliation?
- Retry: Are transient and permanent failures classified, with bounded attempts and backoff?
- Poison work: Does exhausted work reach a visible terminal/dead-letter path rather than loop forever?
- Cancellation: Are safe cancellation boundaries and already-committed effects defined?
- Progress: Is progress durable, meaningful, and updated at a bounded rate?
- Crash recovery: Is every worker crash boundary recoverable without relying on process memory?
- Operations: Can we observe queue age, backlog, attempt counts, redelivery, failures, duration, and worker saturation?
- Capacity: Is worker concurrency bounded by downstream capacity rather than only queue depth?
Agent rule
When reviewing background-job code, do not approve “enqueue and retry” as a reliability design. Trace one logical job from durable creation through claim, effect, acknowledgement, worker crash, redelivery, retry exhaustion, cancellation, and terminal state. Verify that every irreversible effect has an idempotency or reconciliation story and that operational evidence can distinguish waiting, running, retrying, failed, and stuck work.
Related concepts
- Backend Concurrency — worker pools introduce overlapping execution and ownership races.
- Message Queues — transports work to competing consumers and define delivery/ordering behavior.
- Idempotency — makes repeated attempts converge on one logical effect.
- Retries & Backoff — controls when transient failure should be tried again without amplifying outages.
- Delivery Semantics — separates message delivery guarantees from business-effect guarantees.
- Logs, Metrics & Traces — provides evidence for queue age, attempts, latency, failures, and saturation.
Continue through the Backend Systems path toward caching, message queues, rate limiting, idempotency, and service resilience.
Sources
Primary references verified on 2026-11-18:
- Celery documentation — Tasks
- Celery documentation — Configuration:
task_acks_late/task_reject_on_worker_lost - Celery documentation — Workers Guide
- Amazon SQS documentation — Visibility timeout
- Google Cloud Tasks documentation — Configure retries
- Google Cloud Tasks — RetryConfig — both limit conditions and task retention
- Kubernetes — Assign memory resources — OOMKilled container termination reason
This lesson is evolving with a 180-day review target because worker runtimes, task frameworks, and managed queue behavior change, even though the durable lifecycle and crash-boundary reasoning model is stable.
Backend Concurrency: Preserve Invariants When Requests Overlap
Reason about overlapping backend work by identifying invariants, race conditions, atomic boundaries, critical sections, coordination scope, optimistic and pessimistic strategies, contention, and deadlocks.
Application Caching: Trade Freshness for Less Work Deliberately
Reason about application caches as derived state with explicit keys, freshness budgets, invalidation, stampede control, negative caching, failure behavior, and production evidence.