
Where invalid or unprocessable messages go to be analyzed by hand.
Etymology
The term "dead letter" comes from postal systems where dead letter mail is mail that both cannot be delivered to the addressee and cannot be returned to the sender. Undeliverable mail is invalid in some way: it doesn't follow postal regulations, has an incomplete address, or some other reason.
The U.S. Smithsonian National Postal Museum as more information about dead letter offices .
Message queues
Postal mail is often used as an analogy for message queueing in computer science. A sender or producer places a message into a queue, and then a receiver or consumer takes it and processes it.
When a message queueing service fails to deliver a message, or when a consumer repeatably fails to process it, messages can (and should) be moved to a "dead letter queue" (DLQ). Some message queueing services can manage this automatically based on a configured policy, while others require the consumer to implement the functionality.
Moving a failing message to the DLQ is important for a few reasons:
- It prevents the message from being consumed again, reducing wasted consumer effort and decreasing the latency to process other messages.
- It alerts a human to a systemic problem that needs manual intervention.
DLQs temporarily hold failing messages until they can be manually analyzed. Some messages may be entirely invalid and can be discarded or purged, but others may require a code change to the consumer before the messages can be "redriven" back to the main queue. It generally does not make sense to programmatically consume from a DLQ (i.e. treating it like a retry queue). Unlike retry queues, DLQs are for unprocessable messages that require manual intervention.
Some example reasons why you might want to route a failing message to a DLQ are:
- The message payload exceeds the allowed limit. You may want to or need to raise the limit.
- The message adheres to the correct schema but still cannot be deserialized. You may have an issue with your deserialization code.
- The message exceeds its TTL. You may need to manually determine if it is safe to redrive the message.
- The message has been retried its maximum number of times. There may have been a transient issue with a downstream system.
Because messages in a DLQ require manual analysis, it is best to keep messages that are invalid (and will remain invalid forever) out of the DLQ. Some example scenarios:
- The message does not adhere to the correct schema or serialization format.
- The message contains stale data or a stale request.
- The message contains invalid values or requests an invalid operation.
- The message has been tampered with (using HMAC or similar for validation).
Monitoring
You should create automated alerts to alert an on-call engineer to DLQ queue depth and/or oldest message age. Not every DLQ will warrant paging an engineer overnight, but a timely response is needed before DLQed messages are expired by any configured retention period.
If you have a latency SLO or SLA guarantee to process messages within a certain time after production, routing messages to a DLQ may cause you to break it because of the time required for a human to analyze them.
SLIs, SLOs, and SLAs: What Are They?
May 11, 2020 · 5 min read
You might have heard these terms in reference to commitments with vendors or customers, but what are they, and why should you care?
Poison messages
For message queues that preserve ordering, an unprocessable message at the head of the queue may block all other messages from being processed. These are called a "poison message " or "poison pill."
For entirely invalid messages, you will want to discard them so they don't block everything else. For other, possibly-valid messages, you can only use a DLQ if you're willing to let those messages process out of order.
Example: Amazon SQS
With a DLQ enabled, Amazon SQS will move messages that reach the visibility timeout a configured number of times to the DLQ. SQS DLQs are the same as any other SQS queue, so you can inspect their messages in the web console, consume them programmatically (though you shouldn't), and they adhere to the DLQ's retention period.
Here is an example where a message is retried only once before being DLQed:
Note that there is only one consumer here. If you need multiple consumers to each have their own copy of messages, then you will need to use Amazon SNS with multiple SQS queues .
Example: Apache Kafka
Apache Kafka does not have a native concept of a DLQ. Instead, consumers have to implement DLQ logic themselves, routing to retry or DLQ topics when appropriate.
Kafka does not track the attempt or retry count of each message, retries are instead typically handled by the consumer not acknowledging the failed message. If you want to DLQ messages after a certain number of failed attempts, you will need to implement retry count tracking yourself in each consumer. Count tracking can be accomplished by wrapping the original payload in an "envelope" object that tracks the count, or by setting a header on a new Kafka record. Both strategies require the message to be newly published with its new data to a topic, so retry count tracking will break strict ordering guarantees.
Here is an example where a message is retried only once before being DLQed, maintaining strict ordering:
If you don't have to maintain strict ordering of messages, then you can use a retry queue, where the published records hold the current retry count:
Other event streaming systems such as Apache Pulsar can also use this methodology.
Example: AMQP
RabbitMQ , Apache ActiveMQ , and Apache Artemis are popular technologies that support AMQP 1.0 (and some also support 0-9-1).
AMQP 1.0 does not define a standard for DLQs, so different brokers require different solutions:
- RabbitMQ's dead letter exchanges can route rejected, TTL-expired, and max-
delivery-limit-reached (with quorum queues) messages to a DLQ. - Apache ActiveMQ's dead letter handling can route rejected and TTL-expired messages to a DLQ. ActiveMQ tracks the message delivery count, and retry limits are enforced by the broker redelivery plugin or manually by the consumer.
- Apache Artemis' dead letter handling can route
max-delivery-attempts-reached messages to a dead letter address. TTL-expired messages are handled by an expiry address .
Summary
- DLQs temporarily hold messages that failed to be processed, likely multiple times.
- DLQs help unblock or reduce wasted effort on the main queue.
- Messages in the DLQ have to be manually analyzed.
- DLQs should never contain entirely invalid messages; those should be discarded instead.
- DLQs should be monitored for queue depth and/or oldest message age, alerting an on-call engineer.
- Messages in a DLQ might need to be manually "redriven" to the main queue after a code change, if safe.
Additional reading
- Enterprise Integration Patterns: Dead Letter Channel
- AWS: What is a Dead-Letter Queue (DLQ)?
- Confluent: What is Kafka Dead Letter Queue?
- Microsoft Azure: Overview of Service Bus dead-letter queues





