Microservices Dead Letter Queues
Some messages can never be processed successfully. A malformed payload, a missing record, or a bug in the consumer makes the same message fail again and again. Without a plan, that single message blocks the line or loops forever. A dead letter queue, shortened to DLQ, parks problem messages safely so healthy traffic keeps moving.
Layman Explanation
A post office sorts thousands of letters daily. A letter with an unreadable address cannot go anywhere. Staff place it in a special tray for later review rather than leaving it jammed in the machine. The DLQ is that special tray.
Diagram: The Path of a Failing Message
Producer --> Main queue --> Consumer
^ |
| | fails
+--- retry ---+ (attempt 1, 2, 3)
|
after max attempts
v
Dead letter queue --> Alert + manual review
Why Messages Fail
Temporary Failures
A database restarts or a network call times out. A retry a few seconds later succeeds. Retries handle these cases.
Permanent Failures
The message holds invalid data, refers to a deleted customer, or triggers a bug. No number of retries helps. These messages belong in the DLQ.
Retry Policy
Define a clear policy before messages reach the DLQ:
- Set a maximum number of attempts, such as five.
- Wait longer between attempts: 1 second, 5 seconds, 30 seconds, 2 minutes.
- Add random jitter so many failing messages do not retry in lockstep.
- Move the message to the DLQ after the last failed attempt.
What a DLQ Entry Should Contain
| Field | Why It Helps |
|---|---|
| Original message body | Allows replay after the fix |
| Error text and stack trace | Shows the cause |
| Attempt count | Confirms retry policy worked |
| Source queue and timestamp | Helps trace the origin |
| Correlation or trace id | Links to logs and traces |
Handling the DLQ
Monitor and Alert
A growing DLQ signals trouble. Set an alert when the depth rises above zero for more than a few minutes. Silent DLQs turn into forgotten graveyards.
Investigate
Engineers open an entry, read the error, and find the root cause. A single bug often explains hundreds of entries.
Fix and Redrive
After the fix, a redrive tool moves messages back to the main queue for another try. Confirm that the consumer is idempotent before a mass replay.
Discard with Care
Some messages are junk. Record the decision, then delete them. Keep an audit note for compliance needs.
Poison Messages
A poison message crashes the consumer every time it appears. Without a DLQ, the consumer restarts, reads the same message, and crashes again in an endless loop. The attempt counter and DLQ break that loop.
Platform Support
- Amazon SQS offers a redrive policy with a maximum receive count.
- RabbitMQ routes rejected or expired messages through dead letter exchanges.
- Kafka teams usually create a separate topic for failed records.
- Azure Service Bus gives every queue a built-in dead letter sub-queue.
Example: Order Message with Bad Data
An inventory consumer receives an order message that references product id 0, which does not exist. Three retries fail with the same error. The message moves to the DLQ with its error details. An engineer reviews the entry, finds a bug in the checkout form, ships the fix, and redrives the stuck messages. Healthy orders flowed the whole time.
Key Takeaways
A DLQ isolates messages that fail permanently. Combine it with a clear retry policy, rich error details, monitoring, and a safe redrive process.
