Park poison messages
One malformed message fails forever and blocks or wastes a worker.
Say it like this
A message that fails every retry must not block the queue or vanish, so after N attempts the broker moves it to a dead-letter queue that pages someone and can be replayed once the bug is fixed.
Basic solution
After N failed attempts move it to a dead-letter queue, alert, and replay after fixing.
Retry, then park, then replay
Transient failures go back to the queue with growing delays. Permanent errors and exhausted retries land in the DLQ, which alerts and can be replayed in batches.
How it works
- The worker receives a message and fails to process it: it throws an exception, times out on a provider call, or explicitly rejects (nacks) the message.
- The broker increments the message's receive count and makes it visible again for another attempt, ideally after a delay that grows with each attempt so a struggling provider gets a breather.
- When the receive count passes the maximum (say 5), the broker stops redelivering: it moves the message, with its original body and attributes, to a separate dead-letter queue. The main queue keeps flowing.
- An alert fires on the dead-letter queue when its depth is above zero or when its oldest message is older than some threshold, so a human knows work is stuck.
- An engineer inspects the message, fixes the handler bug or the bad data, and uses a replay tool to move messages back to the source queue (or to a repair queue) in a controlled batch, so the work finally completes.
Separate permanent from transient first
- A permanent error (an invalid phone number, an unknown template) will fail the same way every time, so the worker should send it straight to the dead-letter queue on the first attempt. Retrying it five times wastes the attempts and delays the alert by the whole backoff schedule.
Worked example
Notification SMS senders
- 10% of 250M/day are SMS ≈ 25M/day. Provider error rate on a bad day: 0.5% transient (5xx, timeouts), 0.05% permanent (invalid number, opted out).
- Transient: 125K/day, retried with delays 10 s, 20 s, 40 s, 80 s, 160 s; 99% succeed by the third attempt, so about 1,250 exhaust retries and land in the DLQ.
- Permanent: 12,500/day sent straight to the DLQ with the provider error code attached; a nightly job marks those deliveries failed and flags the numbers.
- DLQ retention 14 days ≈ 200K messages × 1 KB ≈ 200 MB. Trivial.
- Alert threshold: more than 100 dead letters in 5 minutes means the provider or a deploy broke, not individual bad numbers.
Variants and when each wins
Broker-managed DLQ (SQS redrive policy, RabbitMQ dead-letter exchange)
- The broker counts attempts and moves the message itself, and moving messages back to the source queue (redrive) is a button. It wins for zero code in the worker, so it is the default wherever the broker offers it.
Retry topics with escalating delay (Kafka)
Parking table in the database
- The worker writes the failed job and its error into a table and marks the original message done. It wins for job systems that already live in the database, because operators can query and edit the parked payload before replaying it.
In-handler retry only
- The worker loops a few times inside the same invocation and then gives up. It wins only for blips of a few hundred milliseconds; anything longer than the worker is willing to wait is lost.
Use the broker's DLQ if it has one; build retry topics on Kafka; keep a parking table when operators need to edit payloads before replay.
Failure modes
Unwatched DLQ
- A month later someone discovers two million messages nobody ever processed. It happens because the dead-letter queue had no alert and no owning team, so messages went in and nobody looked. Alert on depth and on age, name an owner, and review the queue in a regular ops meeting.
Replay storm
- Replaying the dead-letter queue takes the service down again. It happens because all 200K parked messages were pushed back into the source queue at once, recreating the overload that parked them. Replay in rate-limited batches and watch the error rate before releasing the next batch.
Replaying stale messages
- Users receive a ten-day-old notification. It happens because the replay sent everything back without checking whether each message was still relevant. Carry an expires_at in every message and drop anything past it during replay.
Lost context
- The dead letter has a body but nothing that says why it failed. It happens because the error was logged on the worker and not attached to the message. Before dead-lettering, attach the last error, the attempt count and a trace id as message attributes.
Retrying permanent errors
- Five minutes of retries pass before an obviously invalid address gives up. It happens because the worker treated every error the same and had no notion of a permanent failure. Classify 4xx-style errors as permanent and dead-letter them on the first attempt.
Interviewer follow-ups
Answer out loud before you open each one.
How many attempts before dead-lettering?
Enough to ride out a short outage of the dependency, which with exponential backoff is usually five to eight attempts over ten to thirty minutes. Beyond that, something is actually broken and a human should look. Permanent errors get zero retries.
What do you do with the DLQ during a provider outage?
Pause the consumers instead of letting everything dead-letter, or raise the max receive count temporarily. If messages did land in the DLQ, replay them in rate-limited batches once the provider recovers, dropping any past their expiry.
Should the DLQ be per queue or shared?
Per source queue. A shared DLQ mixes payload shapes and owners, so nobody can replay safely. The one-to-one mapping also makes redrive a single click in most brokers.
How is this different from just logging the failure?
A log entry cannot be replayed; the work is gone. The DLQ keeps the full message so the work can be completed after the fix, which is what makes at-least-once delivery a real guarantee instead of a hope.
Connections
- In study paths
- Making retries safe
- Pairs with
- Retry transient failures carefully · Decouple producer from consumer · Make retries safe · Measure what users feel
- Used in
- Notification System
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon SQS | sms-transactional, Delay queue, Dead-letter queue | Pub/Sub, Cloud Tasks | Service Bus queues, Queue Storage | RabbitMQ, ActiveMQ |
| Amazon EC2 Auto Scaling | SMS sender, Alert + replay tool | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |