Queues and failure handlingWork can fail or repeat. Make the next step safe.
Queues and failure handling. Learn the concepts, explore a model and defend your design.
16SYSTEM DESIGN
DEFINITION
A queue separates acceptance from later completion; retry and recovery rules handle work whose outcome can become uncertain. Stable operation identity and bounded demand are necessary because repeating a message or adding workers can multiply side effects and overload.
Why a worker can see the same message again
STEP 1WorkerStores successful result
STEP 2AcknowledgementLost before broker sees it
STEP 3BrokerMay redeliver the message
Make the business effect safe to repeat; a broker acknowledgement is a separate step.
CONCEPT 1
Handle delayed work and failures
Queues make timing explicit
A producer puts a job in a queue and a consumer processes it later. This can move email off the order-response path. At arrivals 120 jobs/s, four workers doing 30/s each only keep up. Five have 30/s spare and ideally drain 1,800 jobs in 60 seconds. If a worker sends before acknowledgment and crashes, delivery can repeat. Deduplicate at the effect boundary and investigate dead-letter messages.
Retries must preserve intent
A timeout bounds waiting and a deadline bounds the full operation. Retry selected failures with limits, backoff and jitter. Three layers making three total attempts each can cause 27 deepest-layer calls. A stable idempotency key lets a retry look up the existing outcome. Save the key and business result consistently; uncertain external effects may need reconciliation instead of another charge.
Operate the whole system
Rate limits bound admission, concurrency limits bound in-flight work and backpressure slows producers. Metrics summarize, logs record events and traces connect an operation's steps. Keep secrets and private data out of diagnostics. Test restore procedures, not just backup jobs. Deploy compatible schema changes before removing old fields so rollback remains possible. Use the next three worked solutions to see these concepts combined.
An operation key resolves an uncertain reply
STEP 1CommitStore result for key K
STEP 2Reply lostClient does not know outcome
STEP 3Retry key KReturn the stored outcome
Keep the operation identity stable. A new key may represent a second purchase.
Key learning points
Measure spare consumer capacity, not only consumer count.
Expect loss or duplicate-delivery windows at different acknowledgment timings.
Use finite retries, a deadline and a safe operation identity.
Compare the important ideas
Queues and failure handling: concepts and trade-offs
Mechanism
Purpose
Limit
Queue
Delay work under a declared policy
Backlog can outgrow capacity
Retry
Attempt eligible work again
May duplicate effects or amplify overload
Idempotency
Preserve intended effect on repeats
Needs correct boundary and retention
Backup restore
Recover historical state
Must be tested with usable keys and data
WORKED EXAMPLE
Account for arrivals during recovery
At 120 new jobs/s, four consumers at 30/s each have no spare rate to drain an old backlog. Five have 30/s spare and ideally drain 1,800 jobs in 60 seconds. This constant-rate calculation excludes retries and downstream limits.
Further reading
Original teaching explanations by Buildfest Learn. Check these primary references for the underlying concepts and implementation details.
Find a worker count that drains the queue while new jobs keep arriving.
1120 arrivals/s
290 completions/s
330/s backlog growth
The backlog grows; it cannot drain under this load.
Subtract ongoing arrivals from completion capacity before dividing backlog by spare capacity. Do not divide by the full worker rate while new jobs are still arriving.
Chapter review: two questions
Attempt both questions before opening the model answers.
Question 1
Jobs arrive at 120/s and six ideal consumers process 30/s each. How long does an old backlog of 1,800 jobs take to drain?
Reveal model answer
Capacity is 180/s, spare capacity is 60/s and the ideal drain time is 30 seconds. Work variation, failures and downstream quotas can make the real result worse.
Question 2
A payment request times out after a possible commit. Why should a retry preserve its original operation key?
Reveal model answer
The effect may already exist. A stable user-scoped key lets the service retrieve or reconcile that existing outcome; a fresh key may be interpreted as another purchase. Unknown outcome is not definite failure.
Answers stay in this tab. Download before leaving. This practice is not graded or saved to your account.