Lesson 6 of 18
22. Message Queues

22.5 What are retries and dead-letter queues?

3 min read+10 XP

At the bank: Some customers can't be served at the counter: a missing document, a strange request. The clerk tries, then tries again. After three tries, the token goes to the manager's desk, so the counters keep moving. Someone looks at those cases by hand.

In the system: Failures come in two kinds:

  • Transient: the SMS gateway timed out, the database was briefly overloaded. Retrying later usually works.
  • Permanent: the message is malformed, the phone number doesn't exist, the code has a bug for this input. Retrying never works. These are poison messages.

Retries should back off, so a struggling downstream gets breathing room: wait 2 s, 4 s, 8 s, 16 s, 32 s, up to a cap, with jitter (Chapter 21, Chapter 29). Brokers do this with delayed redelivery, or you re-enqueue with a delay.

A dead-letter queue (DLQ) catches messages that fail too many times. In SQS, a redrive policy moves a message to the DLQ after maxReceiveCount receives; RabbitMQ has dead-letter exchanges. In the chapter's code, a message with a malformed address is tried 3 times and then moved to the DLQ, and the main queue keeps flowing.

What to do with a DLQ:

  • Alert when it's not empty. A DLQ nobody watches is a slow data-loss bug.
  • Inspect the messages, fix the bug or the data.
  • Redrive: move them back to the main queue once fixed.
  • Keep enough retention to fix things (SQS keeps messages 4 days by default, up to 14).

Memory code: "Try again with growing pauses; after a few, send it to the manager's desk."