Dead-Lettering & Retries
Dead-letter exchanges, delivery limits, poison messages, and delayed retries
Some messages can't be processed: malformed JSON, a reference to a deleted customer, a downstream API that's been down for an hour. Requeueing them forever blocks the queue and burns CPU; dropping them loses data. A dead-letter exchange (DLX) gives them somewhere to go.
When a message is dead-lettered
A queue with x-dead-letter-exchange (or a dead-letter-exchange policy)
republishes a message to that exchange when it's:
Reason (x-death.reason) | Cause |
|---|---|
rejected | A consumer nack/rejected it with requeue=False |
delivery_limit | It exceeded the quorum queue's delivery limit |
expired | Its TTL passed (x-message-ttl or per-message expiration) |
maxlen | The queue overflowed with overflow=drop-head |
The message keeps its body and properties and gains an x-death
header recording where it died, why, and how many times.
Rejecting a poison message
From labs/dead_letters.py:
ch.basic_publish("", "lab.work", body="not valid json {")
method, _, body = ch.basic_get("lab.work")
ch.basic_nack(method.delivery_tag, requeue=False) # don't retry: dead-letter it
1. rejected message dead-lettered:
body='not valid json {' reason=rejected from queue=lab.work count=1Rule of thumb: errors that won't get better on retry (bad input, failed validation) should be rejected without requeue immediately. Retry only what might succeed later.
Delivery limits, and what counts
Quorum queues track how many times a message has been delivered and
dead-letter it past x-delivery-limit (default 20 in RabbitMQ 4). The
pre-loaded orders.billing uses 3. But what counts as a delivery attempt
surprised this module during testing:
2. nack(requeue=True) five times: message is still in lab.work, delivery limit untouched
consumer crashed 4 times holding it; then it was dead-lettered: ['always fails']In RabbitMQ 4, the limit counts deliveries that failed: the consumer's
channel or connection closed while holding the message (a crash, or a
consumer timeout). A consumer that catches its exception and politely does
nack(requeue=True) doesn't use up the limit at all, and can loop on the
same poison message forever.
The first version of the lab looped forever on exactly this:
nack(requeue=True) in a loop, waiting for a delivery limit of 3 that
never triggered. In application code the symptom is a consumer at 100%
CPU, a redelivered rate on the dashboard that never drops, and the
queue behind the poison message stuck. Don't rely on the delivery limit
for errors you've caught. Decide in code: reject without requeue (to
the DLX), or schedule a delayed retry and count attempts yourself (in a
header).
Delayed retries
Immediate requeue retries in microseconds, which doesn't help a downstream service that's down. The classic pattern is a wait queue with a TTL whose own dead-letter target is the original queue:
3. message came back for retry after 2.1s;
x-death shows [('lab.retry.wait', 'expired'), ('lab.work', 'rejected')]For backoff, use several wait queues (retry.5s, retry.30s, retry.5m)
and pick one based on the attempt count (in x-death[...].count or your
own header). After the last attempt, send the message to a parking
queue for humans to inspect.
Per-queue TTLs expire messages in order from the head. If you use per-message TTLs in one shared wait queue, a message with a 5-minute TTL at the head blocks a 5-second one behind it: it won't expire until it reaches the head. Use one wait queue per delay.
What to do with dead letters
- Alert when the dead-letter queue is non-empty (the
RabbitMQDeadLetteringalert watches the dead-letter rate). - Keep enough context to diagnose: the
x-deathheader, plus your own headers with the error message and stack trace. - Have a replay tool: after fixing the bug, move messages from the parking queue back to the work queue (the management UI's Shovel plugin, or a small script).
Lab
- Run
dead_letters.py. - Publish to the pre-loaded
ordersexchange with keyorder.created.eu, then consume fromorders.billingand crash (close the connection without acking) 4 times. Find the message inorders.dead. - Add 5 s and 30 s wait queues and implement exponential backoff with a parking queue after the third attempt.
- Watch
rabbitmq_global_messages_dead_lettered_*in Prometheus while you run the lab.