In this lesson · topic 5 of 7
Topic 5
Retry limits and the dead-letter queue
7 min read
A message that always fails
At-least-once delivery says the broker redelivers until the consumer acks. That is a promise about crashes and timeouts. It says nothing about a message the consumer can never handle: a JSON body with a missing field, a reference to a customer who was deleted, a bug triggered by one value. The consumer fails on it, the broker redelivers it, the consumer fails again. This is a poison message.
Left alone, one poison message can cost real capacity. If it fails in 50 milliseconds and the broker redelivers at once, that is 20 wasted attempts a second. With a strictly ordered queue it can also block every message behind it.
Count the attempts
Brokers track how many times a message was delivered. In SQS this is ApproximateReceiveCount, and a queue’s redrive policy has a maxReceiveCount. Once a message has been received more than that many times without being deleted, SQS moves it to the dead-letter queue (DLQ) you configured. RabbitMQ quorum queues have a delivery limit that works the same way, and can dead-letter the message after it is exceeded.
Pick the limit from the failure you expect to heal. A transient failure, such as a database failover that lasts 60 seconds, needs enough attempts, spaced far enough apart, to outlast it. If each attempt is 20 seconds apart, 5 attempts cover 80 seconds. A limit of 3 would have dead-lettered every message that arrived during the failover.
Predict first
What the dead-letter queue is for
The DLQ keeps a failing message out of the main flow and out of the way, but it does not delete it. Treat it as an inbox for a person or a repair job. A useful DLQ entry keeps the original body, the number of attempts and the last error. Without the error, the DLQ is a pile of mysteries.
Three habits make it work:
- Alarm on any message arriving. A DLQ nobody watches is data loss with extra steps. Use the age of the oldest message in it, as in the earlier topic.
- Set retention longer than the main queue. SQS retains a dead-lettered message by its original enqueue time, so a DLQ with the same retention period as the source queue can expire it almost at once. Give the DLQ a longer period.
- Separate DLQs by source. A shared one hides which consumer is failing.
Redrive after the fix
Once the bug is fixed or the dependency is back, the messages are valid and need to run again. This is a redrive: moving them from the DLQ to the source queue. SQS offers a console action and an API for it.
A redrive is a burst. If the DLQ holds 40,000 messages and you push them back at once, you get a 40,000-message spike on top of normal traffic. Redrive at a controlled rate, such as 50 a second, which clears 40,000 in 800 seconds. Because the messages have been through at-least-once delivery before, the consumer must already be idempotent, and it is worth checking that it is, since some of them may have partly run.
Sources
- Amazon Web Services, Using dead-letter queues in Amazon SQS.
- RabbitMQ documentation, Quorum Queues, the section on poison message handling.