Buildfest Learn System DesignRead and experiment freely · Chapter activities stay in this tab
Practice librarySystem DesignQueues and failure handling

LESSON 16 • ESSENTIAL

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.

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
  1. STEP 1WorkerStores successful result
  2. STEP 2AcknowledgementLost before broker sees it
  3. 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
  1. STEP 1CommitStore result for key K
  2. STEP 2Reply lostClient does not know outcome
  3. 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
MechanismPurposeLimit
QueueDelay work under a declared policyBacklog can outgrow capacity
RetryAttempt eligible work againMay duplicate effects or amplify overload
IdempotencyPreserve intended effect on repeatsNeeds correct boundary and retention
Backup restoreRecover historical stateMust 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.