Dead letter queue
Dead Letter Queue
A separate queue for messages that could not be delivered or processed under the system’s rules. It keeps failed messages out of the normal flow so a team can investigate and retry them when appropriate.
Also known as: DLQ
What a dead letter queue is for
A dead letter queue (DLQ) is where a messaging system sets aside messages it has stopped trying to deliver or process normally. Imagine a worker that generates invoices from order events. If one event contains an unsupported schema version, retrying it with the same code will not help. Sending it to a DLQ lets the worker move on while preserving the event for investigation.
What sends a message there?
A consumer usually acknowledges a message once it has finished the work. For temporary failures, such as an unavailable dependency, the system can retry with a delay and a limit. Persistent failures can go to a DLQ when the limit is reached, while known permanent failures can go there immediately. Some brokers also dead-letter messages that expire in the source queue. The exact triggers depend on the broker and its configuration.
The broker can handle the routing, or the consumer can publish the failed message itself. In RabbitMQ, for example, a configured dead-letter exchange routes dead-lettered messages to a bound queue. In Kafka-based systems, a separate topic often serves the same purpose, with the consumer or its framework responsible for publishing to it.
Example: a timed-out invoice request
Suppose an invoice worker calls a billing service but gets no response. It retries with increasing delays, then sends the event to a DLQ after exhausting its retry budget. The team checks the original event, the error details, and its event ID before deciding what to replay. The timeout does not prove the invoice was never created: the billing service may have completed the request before the response was lost. Reusing a stable idempotency key lets the billing service recognize the same operation instead of creating another invoice.
What can go wrong in production?
- Using a DLQ as a retry queue. A retry queue schedules another attempt. A DLQ holds messages out of normal processing until someone investigates or a controlled replay procedure runs.
- Letting failures pile up unnoticed. Monitor the rate of new entries and the age of the oldest message. Set a retention policy and make sure a team owns the queue and responds to alerts.
- Replaying everything at once. Fix the underlying problem first, replay a small batch, and check the result before increasing the rate. If later events have already been processed, verify that replaying an older event is still valid.
How it comes up in interviews
A common question is how to avoid losing a message when the consumer publishes to the DLQ itself. If it acknowledges the original before the DLQ publish succeeds, a crash can lose the message. If it publishes first and crashes before acknowledging, the original can be redelivered and a duplicate DLQ entry can result. Explain what the broker actually guarantees and how you would handle duplicates rather than assuming that a DLQ provides exactly-once delivery.