Topic 3 of 6 · 5 min read
Queues, background jobs and retries
Goal: Review a queue-based design for duplicate processing, lost messages and missing dead-letter handling.
A queue lets work happen later, but it does not let work happen exactly once, so every consumer must survive seeing the same message twice. A review of a queue design is mostly a review of what happens when something fails halfway.
Check yourself · 1 of 3
A queue offers at-least-once delivery. What must the consumer be able to do?
Why it matters
Queues such as SQS, RabbitMQ, Kafka or a Redis-backed job library smooth out spikes and keep slow work such as sending email off the request path. Most of them promise at-least-once delivery: a message is never dropped, but it may arrive more than once. If the consumer charges a card, a duplicate is a double charge.
How to review it
- Name the delivery guarantee. At-least-once is the normal case. "Exactly once" in a design deserves a question: where exactly, and how?
- Check idempotency. An idempotent operation gives the same result when repeated. Look for an idempotency key (such as the order id), a unique constraint, or a "processed" check done in the same transaction as the work.
- Follow the failure. The consumer crashes after the work but before the acknowledgement. The message returns to the queue. What happens then?
- Check retries. Are they limited, with growing delays (exponential backoff) and some random jitter? Unlimited immediate retries turn one broken dependency into a flood.
- Look for a dead-letter queue (DLQ). Messages that keep failing must go somewhere visible, with an alert, instead of blocking the queue or vanishing.
- Check ordering. Most queues do not guarantee global order. If order matters, say per customer, the design needs a partition key or sequence numbers.
- Check backpressure. If producers are faster than consumers, how deep may the queue grow, and what is the alert?
Before and after
Weak:
On order.paid, publish a message. The worker sends the receipt
and retries until it succeeds.
Better:
On order.paid, publish {orderId}. The worker inserts orderId into
receipts_sent (unique) before sending; a duplicate insert means
skip. Retry 5 times with backoff and jitter, then move to the
DLQ and alert when it holds any message.
What goes wrong
- Duplicate side effects. Two emails, two refunds, two shipments.
- Poison messages. One malformed message crashes the worker in a loop and blocks the rest.
- Silent loss. Failed jobs are deleted after the last retry with no record.
- Assumed order. "Cancelled" is processed before "created".
Examples
Crash before acknowledgement
A worker charges a card, then crashes before acknowledging the message. The queue redelivers it and the customer is charged twice. The fix is to send the charge with an idempotency key built from the order id, so the payment provider returns the first result.
The poison message
One job has a null customer id and throws on every attempt. With unlimited retries it is retried forever and the queue behind it stalls. After five attempts it should move to a dead-letter queue, with an alert for a person to inspect it.
Key points
- Assume at-least-once delivery: every consumer must handle the same message twice.
- Idempotency comes from a key and a unique constraint, not from hoping duplicates are rare.
- Retries need a limit, growing delays and jitter.
- A dead-letter queue with an alert stops poison messages from blocking or vanishing.
- Ordering and queue depth must be stated, not assumed.
Common mistakes
- Believing the queue gives exactly-once processing.
- Retrying immediately and forever, which can overload a recovering dependency.
- Having no dead-letter queue, or having one nobody watches.
- Assuming messages arrive in the order they were sent.
Check yourself · 2 of 3
Why would a queue design include a dead-letter queue?
Check yourself · 3 of 3
A worker retries a failing call every second with no limit. What is the main risk?
Practice items, not counted towards your mastery.
Start here
- The System Design Primer (opens in a new tab)ToolFree
A free, long-lived study guide covering scaling, caching, queues and back-of-envelope estimates.
- Site Reliability Engineering (Google SRE book) (opens in a new tab)BookFree
Google's free book on running reliable services: monitoring, overload, failure and release practice.
- Cloud design patterns (Azure Architecture Center) (opens in a new tab)DocsFree
A catalogue of named patterns such as cache-aside, retry, circuit breaker and queue-based load leveling.