CodePark/ questions / RabbitMQ: acks, prefetch & dead-lettering
START PRACTICINGPRACTICE
Backend question · Message queues & streaming › RabbitMQ: acks, prefetch & dead-lettering

How do dead-letter exchanges work in RabbitMQ? What causes a message to be dead-lettered, and how can you use them to build delayed retries?

A dead-letter exchange (DLX) is an exchange where RabbitMQ sends messages that a queue could not deliver. You set it on a queue, preferably through a policy. You can also use the x-dead-letter-exchange queue argument, plus an optional x-dead-letter-routing-key.

A message is dead-lettered when:

  • it is rejected or nacked with requeue set to false
  • its TTL (time to live) expires
  • the queue hits its length limit and drops it
  • it goes over a quorum queue's delivery limit

RabbitMQ then publishes it again to the DLX. It adds an x-death header that records the reason, the source queue and a count.

Delayed retries work like this:
1. The consumer nacks a failing message without requeue.
2. The DLX routes it to a retry queue. This queue has a message TTL, say 30 seconds, and no consumers.
3. When the TTL expires, the retry queue dead-letters the message back to the work exchange.
4. The consumer reads x-death to count the attempts.
5. After a limit, the consumer sends the message to a final parking queue, where people can inspect it.

  • Set a dead-letter exchange through a policy or the x-dead-letter-exchange argument.
  • Messages are dead-lettered on reject or nack without requeue, TTL expiry, the length limit, or the quorum delivery limit.
  • The x-death header records the reason, the queue and a count.
  • A retry queue with a TTL and no consumers dead-letters messages back to the work queue.
  • Cap the number of attempts, then park the message for inspection.