Skip to content

Lesson 5 of 9 · 28m

Dead Letters, Retry Topologies, and Poison Messages

Design controlled failure paths using dead-letter exchanges, TTL-based retry queues, delivery limits, and explicit poison-message handling.

Every production system eventually receives a message that cannot be processed: malformed JSON, a deleted user, a downstream schema change, a bug in one worker build. The difference between a healthy messaging system and a distributed boot loop is whether failure has somewhere intelligent to go. RabbitMQ gives you dead-letter exchanges, TTL, and delivery limits. Your job is to combine them into a deliberate failure lane instead of an accidental retry storm.

What causes dead lettering

A message is dead-lettered when it is negatively acknowledged with requeue=False, rejected with requeue=False, expires because of TTL, or is dropped because queue length limits are exceeded. Configure the target explicitly:

channel.queue_declare(
    queue="orders.invoice",
    durable=True,
    arguments={
        "x-queue-type": "quorum",
        "x-dead-letter-exchange": "orders.dlx",
        "x-dead-letter-routing-key": "invoice.failed",
        "x-delivery-limit": 5,
    },
)
channel.exchange_declare(exchange="orders.dlx", exchange_type="topic", durable=True)
channel.queue_declare(queue="orders.invoice.failed", durable=True)
channel.queue_bind("orders.invoice.failed", "orders.dlx", routing_key="invoice.failed")

The failed queue is evidence, not a trash can. Someone owns triage, replay, and schema correction.

Immediate requeue is usually the wrong retry

If a dependency is down for thirty seconds, requeueing immediately means the message comes right back while the dependency is still down. Exponential backoff is better. RabbitMQ commonly implements delayed retry with a retry queue that has a TTL and dead-letters back to the original exchange:

retry_args = {
    "x-message-ttl": 30000,
    "x-dead-letter-exchange": "orders",
    "x-dead-letter-routing-key": "order.created.eu",
}
channel.queue_declare(queue="orders.invoice.retry.30s", durable=True, arguments=retry_args)

The failure path becomes:

work queue failure -> retry.30s -> wait -> original exchange -> work queue

Use separate retry lanes for separate delays if needed: 30 seconds, 5 minutes, 30 minutes. Do not build a single black box nobody can inspect.

TTL behavior has sharp edges

Per-queue TTL applies to messages in that queue. Per-message TTL can interact unintuitively because expired messages may not be removed until they reach the head of the queue. A retry topology is fine precisely because every message in the retry lane usually has the same delay. Random per-message TTL inside one queue is where operators get surprised.

Delivery limits stop infinite loops

Quorum queues can count redeliveries with x-delivery-limit. When the limit is exceeded, the message is dead-lettered or dropped depending on configuration. This is the safety net you want for poison messages. Without it, a consumer crash loop can redeliver the same payload forever.

A practical policy is:

attempt 1-3     immediate or short retry for transient dependency failures
attempt 4-5     delayed retry
after limit     dead-letter queue, alert owner, require human or automated triage

Do not confuse delivery count with application-level retry count. They overlap, but RabbitMQ only knows what the broker observed.

Poison-message queues need operational affordances

A failed message should preserve the original body, routing key, headers, exception class, worker version, and failure time where possible. RabbitMQ adds x-death headers with death counts and original exchange information, but application metadata helps. Include enough to debug without publishing sensitive payloads into logs.

When replaying, fix the cause first. Replaying a thousand poison messages into the same broken worker is just automated self-harm.

Field notes

  • Dead-letter queues accumulate real business work; alarm on them like production queues, not like debug logs.
  • Never log full message bodies by default if they may contain personal data or credentials.
  • A retry storm can be worse than a clean outage because it also overloads the downstream dependency that is already failing.
  • Test the failure lane before launch. Many topologies look correct until the first x-death header appears.

Was this lesson helpful?

Sign in to rate