Background Jobs: Separate Work Reliably from Request Handling
A background job runs later or on a schedule, separately from a user request. A short in-process queue can be simple, but durable queues and separate workers are safer when work must survive restarts or absorb bursts. Design status tracking, duplicate handling, and recovery together.
A job is not the same as a queue
A background job is work that continues separately after a request, such as generating a large report, converting an image, sending email, or routine cleanup. A queue is a mechanism for delivering and retaining work, not the work itself. Scheduled jobs may run without a queue; queued jobs are performed by a worker.
An in-memory queue inside a web process, such as an ASP.NET Core hosted service, is easy to build and can have low latency. But queued work can disappear when the process stops, and each web instance has its own queue. If work must survive restarts or be shared among instances, consider a durable external queue and worker.
Trace submission to completion
Suppose a user requests a large report. The API can accept the job, create an identifier, enqueue a durable message, and return HTTP 202 Accepted with a status URL without waiting for completion. A worker reads the message, validates inputs, creates the report, and stores progress, completion, or failure. The user can poll the status URL or subscribe to a completion notification.
This separates scaling for user requests from resource-intensive work. A queue can buffer a sudden burst while workers process at a controlled rate, but longer backlog delays results. Define expiration, priority, cancellation, and status-retention behavior for work items.
Handle schedules and duplicate execution
A scheduler can start a new run before the previous one finishes if a job exceeds the interval. Multiple application instances can also run the same timer. Decide whether only one run is allowed and whether a distributed lock or leader election is needed. Make jobs idempotent because retries and manual replay may happen: running the same job identifier twice should produce the same final result as one run.
Plan failures, retries, and progress
Limited exponential retries can recover from transient network failures, but infinite retries for invalid input or permanent permission errors can block a queue. Set a maximum attempt count and delay; isolate persistent failures in a dead-letter queue (DLQ) with diagnostics and replay history. For multi-step work, record completed and next steps, and decide whether to compensate earlier effects if a later step fails.
Choose how callers see progress: stored status and a query endpoint, reply queue or event, or webhook. A 202 response does not mean the job is complete. Track job ID, step, timestamps, error code, and correlation ID so users can see progress and operators can find delays and failures.
Choose where it runs
An in-process queue can be simplest for short, infrequent work that can be lost or reconstructed. Use a durable queue and separate worker when persistence, burst buffering, independent worker scaling, or separate network and permission boundaries matter. A workflow engine can fit long-running jobs with stateful steps and coordination. Work that shares CPU and memory with the web handler can slow responses, so measure whether separation helps.
Before rollout, trace a small job through submission, acceptance, worker execution, and result lookup. Stop a worker mid-job and simulate a downstream failure. Verify work survives restart, duplicate messages do not create duplicate outcomes, and DLQ recovery is safe before increasing volume.