· 7 min read
Dead letter queues: how to handle poison messages
A dead letter queue moves messages that keep failing out of the way. How SQS and RabbitMQ count retries, pick limits, and replay failed messages safely.

A dead letter queue (DLQ) is a separate queue where a message broker moves messages that keep failing, so one bad message cannot block or slow down everything behind it. The broker counts how many times each message has been delivered, and once that count passes a limit you set, the message is moved aside instead of being retried forever. You then inspect the DLQ, fix the cause, and replay the messages.
That is the whole idea. The details that matter are how the count works, what limit to pick, and what to do with the messages once they land there.
What is a poison message?
A poison message is a message that fails every time it is processed, no matter how often you retry. A missing field, a payload from an old schema version, a reference to a row that was deleted, or a value that trips a bug in your handler are the usual causes.
Most queues give you at-least-once delivery: a message stays in the queue until a consumer explicitly acknowledges (deletes) it, so a crash mid-processing means the message is delivered again. That guarantee is exactly what makes a poison message dangerous. The consumer fails, never acknowledges, and the broker hands the message out again. Without a limit, it cycles forever.
The damage depends on the queue:
- In an ordered queue (a FIFO queue, or a partition in a log), a poison message at the head blocks every message behind it.
- In an unordered queue, it does not block, but it burns worker time on every cycle, fills your logs with the same stack trace, and hides real failures in the noise.
- If your error path also triggers alerts or side effects, each retry repeats them.
How does a dead letter queue work?
Every broker that supports DLQs follows the same shape: count deliveries, compare to a limit, move the message.
In Amazon SQS, a consumer that receives a message has a visibility timeout to delete it; the message is hidden from other consumers during that window. If the timeout expires without a delete, the message becomes visible again and its receive count goes up. The source queue has a redrive policy with a maxReceiveCount. When a message's receive count exceeds it, SQS moves the message to the DLQ you named. A value of 1 means a single failed receive is enough.
In RabbitMQ, quorum queues track a delivery count and expose it to consumers in the x-delivery-count header on redelivered messages. Since RabbitMQ 4.0, quorum queues have a default delivery limit of 20. When a message exceeds it, it is dead-lettered if the queue has a dead letter exchange (DLX) configured, and dropped if it does not. That last part is worth remembering: with no DLX, the limit protects your workers but silently discards the data.
A small simulation shows the flow. Three messages, one of them broken, a limit of three receives:
const MAX_RECEIVES = 3;
const queue = [
{ id: "a", body: { orderId: 1 }, receives: 0 },
{ id: "b", body: { orderId: null }, receives: 0 }, // poison
{ id: "c", body: { orderId: 3 }, receives: 0 },
];
const dlq = [];
function handle(body) {
if (body.orderId == null) throw new Error("missing orderId");
return `processed order ${body.orderId}`;
}
while (queue.length) {
const msg = queue.shift();
msg.receives++;
if (msg.receives > MAX_RECEIVES) {
dlq.push(msg);
console.log(`moved ${msg.id} to DLQ after ${msg.receives - 1} receives`);
continue;
}
try {
console.log(handle(msg.body));
} catch (err) {
console.log(`receive ${msg.receives} of ${msg.id} failed: ${err.message}`);
queue.push(msg); // visibility timeout expires, message comes back
}
}
console.log("DLQ:", dlq.map(m => m.id));
Running it with Node prints:
processed order 1
receive 1 of b failed: missing orderId
processed order 3
receive 2 of b failed: missing orderId
receive 3 of b failed: missing orderId
moved b to DLQ after 3 receives
DLQ: [ 'b' ]
The good messages go through, the bad one gets three chances, then it is out of the way.
How do you set up a DLQ in SQS and RabbitMQ?
In SQS you create a second queue and point the source queue's redrive policy at it:
aws sqs set-queue-attributes \
--queue-url "$SOURCE_QUEUE_URL" \
--attributes '{"RedrivePolicy":"{\"deadLetterTargetArn\":\"'"$DLQ_ARN"'\",\"maxReceiveCount\":\"5\"}"}'
A few rules apply. The DLQ must be in the same account and Region as the source. A standard queue needs a standard DLQ and a FIFO queue needs a FIFO DLQ. And retention behaves differently by type: for standard queues, a moved message keeps its original enqueue timestamp, so time already spent in the source queue counts against the DLQ's retention. A message that sat a day in the source and lands in a DLQ with four days of retention has three days left. FIFO queues reset the timestamp on the move. Either way, set the DLQ's retention longer than the source's, so failed messages are not expiring before anyone looks at them.
In RabbitMQ, the recommended way is a policy rather than queue arguments, because policies can be changed without redeclaring the queue:
rabbitmqctl set_policy DLX "^orders\." \
'{"dead-letter-exchange":"orders.dlx"}' \
--apply-to queues
You then bind a queue to orders.dlx to collect the dead-lettered messages. If the exchange named in the policy does not exist when a message needs it, RabbitMQ drops the message, so create the exchange and its queue first.
When should a message go to the DLQ?
Not every failure deserves retries. It helps to split errors into two kinds:
| Error type | Examples | What to do |
|---|---|---|
| Transient | Timeout, 503 from a dependency, lock contention | Let it retry, ideally with a delay |
| Permanent | Validation error, unknown schema version, missing required data | Send to the DLQ now, skip remaining retries |
Retrying a permanent error just delays the inevitable and wastes capacity. Many client libraries let you reject a message without requeueing it (in RabbitMQ, basic.reject or basic.nack with requeue=false), which dead-letters it immediately if a DLX is configured. In SQS you can send the message to the DLQ yourself and then delete the original.
For transient errors, immediate redelivery often fails again for the same reason. Spacing retries out with exponential backoff and jitter gives the dependency time to recover. In SQS, a consumer can extend or change a message's visibility timeout to push the next attempt further out.
For the limit itself, there is no universal number. Something between 3 and 10 covers most workloads: high enough that a brief outage does not dump healthy messages into the DLQ, low enough that a poison message is removed quickly. If a dependency outage can last longer than your total retry window, expect a burst of DLQ traffic during incidents and plan to replay it.
What do you do with messages in the DLQ?
A DLQ that nobody watches is just a slower way to lose data. Treat it as an alert source:
- Alarm on depth. Any message in the DLQ means something failed repeatedly. Alert on a count above zero, or on growth over time if you expect a trickle.
- Keep context. Log the error, the handler version, and the delivery count with each failure, so whoever opens the DLQ knows why the message is there. RabbitMQ adds an
x-deathheader with the reason and source queue; in other systems, add your own attributes. - Fix, then replay. Once the bug is fixed or the dependency is back, move the messages back to the source queue. SQS has a built-in DLQ redrive feature for this; in RabbitMQ, a small script or shovel republishes them.
- Make handlers safe to replay. A replayed message may have partly succeeded the first time. Handlers should be idempotent, so processing a message twice has the same effect as processing it once. Idempotency keys are the usual tool.
One caution for ordered queues: moving a message aside breaks strict ordering. If later messages depend on the failed one (an update arriving after a failed create), they may now fail too, or succeed against the wrong state. For flows where order is a correctness requirement, you may prefer to stop the consumer and page someone rather than skip ahead.
Key takeaways
- A dead letter queue catches messages that fail more than a set number of deliveries, so one poison message cannot stall or flood your workers.
- SQS uses
maxReceiveCountin a redrive policy; RabbitMQ quorum queues default to a delivery limit of 20 and need a dead letter exchange or failed messages are dropped. - Send permanent errors to the DLQ immediately and retry only transient ones, with backoff.
- Alert on DLQ depth, keep failure context, and replay through idempotent handlers.
FAQ
Is a dead letter queue the same as a retry queue?
No. A retry queue holds messages that will be tried again after a delay. A DLQ holds messages that have stopped being retried and need a person or a fix before they move again.
What happens to messages in a DLQ if nobody reads them?
They expire when the DLQ's retention period runs out. In SQS standard queues, that clock includes the time the message spent in the source queue, so set DLQ retention longer than the source's.
Should every queue have a DLQ?
Almost every queue with at-least-once delivery benefits from one. The exception is a strictly ordered flow where skipping a message is worse than stopping, in which case halting and alerting is the safer choice.