Learn Labs
Failure Handling

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
rejectedA consumer nack/rejected it with requeue=False
delivery_limitIt exceeded the quorum queue's delivery limit
expiredIts TTL passed (x-message-ttl or per-message expiration)
maxlenThe 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=1

Rule 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.

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:

nack, no requeueTTL expiresgive upparking queueafter N attemptslab.workconsumer works herelab.retrylab.work's DLXlab.retry.waitTTL 2 s, no consumers
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.

What to do with dead letters

  • Alert when the dead-letter queue is non-empty (the RabbitMQDeadLettering alert watches the dead-letter rate).
  • Keep enough context to diagnose: the x-death header, 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

  1. Run dead_letters.py.
  2. Publish to the pre-loaded orders exchange with key order.created.eu, then consume from orders.billing and crash (close the connection without acking) 4 times. Find the message in orders.dead.
  3. Add 5 s and 30 s wait queues and implement exponential backoff with a parking queue after the third attempt.
  4. Watch rabbitmq_global_messages_dead_lettered_* in Prometheus while you run the lab.

On this page