Dead Lettering
What is a dead letter exchange
Messages can be “dead-lettered” which means they can be re-published to an exchange (Dead Letter eXchange) when:
- A message is “negatively acknoledged” or NACK’ed
- The message expires on its TTL
- A message is dropped because the queue exceeded a length limit
- The message is returned more times to a quorum queue than the delivery limit
Messages are NOT dead lettered when an entire queue expires
How to configure
You specify the DLX via polices linked to a queue
dead-letter-exchange: The name of the DLX to use
dead-letter-routing-key: The routing key to use when dead-lettering messages. IF NOT SPECIFIED, then the routing key originally used will be used instead.
You can also specify the these values via arguments in application code, and these will take presidence over polices.
Please consult the docs on how to do these. LINK
Routing Dead-Lettered Messages
Deal-letter loop
It is possible to loop rabbit messages, via passing them to the dead letter exchange and then back to the original exchange with the same routing key. If this happens rabbit will kill the message if no rejection was ever detected.
Safety
It is possible for messages to become lost.
Publisher Confirms is turned off by default. Thus using DLX is not always safe. Messages are removed from the original queue after the letter has arrived at the target DLX queue.
Dead-Lettered Effects on Messages
Dead-lettering a message will modify its headers:
- Exchange name is replaced to the one of the DLX
- Routing Key is replaced to the DL routing key
- If this occurs then the CC will also be removed
- BCC header will be removed as per some scenario specified in the docs
Within the header a {Queue, Reason} pair specifies the history of the letter.
A x-death header holds an array of objects specifying information about previous dead lettering events. The most recent element in the array is the most recent dead-lettering event.
Table specifying all the fields
| AMQP 1.0 key | AMQP 1.0 value type | AMQP 0.9.1 key | AMQP 0.9.1 value type | Description |
|---|---|---|---|---|
| queue | string | queue | longstr | The name of the queue this message was dead lettered from. |
| reason | symbol | reason | longstr | Why this message was dead lettered (described below). |
| count | ulong | count | long | How many times this message was dead lettered from this queue for this reason. |
| first-time | timestamp | When this message was dead lettered the first time from this queue for this reason. | ||
| last-time | timestamp | When this message was dead lettered the last time from this queue for this reason. | ||
| time | timestamp | When this message was dead lettered the first time from this queue for this reason. | ||
| exchange | string | exchange | longstr | The exchange this message was published to before this message got dead lettered for the first time from this queue for this reason. |
| routing-keys | array of string | routing-keys | array of longstr | The routing keys (including CC but excluding BCC) of this message before it got dead lettered for the first time from this queue for this reason. |
| ttl | uint | AMQP 1.0 header's ttl (time to live in milliseconds) before this message got dead lettered for the first time from this queue for this reason. | ||
| original-expiration | longstr | The original expiration property of this message before it got dead lettered for the first time from this queue for this reason. |