AWS in 1 Min · Episode 127

SQS dead-letter queues explained

Quarantine for bad messages. Watch the one-minute video, then read the details.

David Gomez1:14 video · 3 min read

"A bad message just fails and goes away." In a queue, it's worse. A message that can't be processed comes back: the worker fails, the visibility timeout ends, the message reappears, and the next worker fails on it too. It burns compute, fills your logs and can hold up the work behind it. This is called a poison message, and the cure is a dead-letter queue (DLQ).

What a dead-letter queue is

A dead-letter queue is an ordinary SQS queue that another queue (the source queue) uses as a quarantine. Messages that keep failing are moved there, so the rest of the traffic flows normally and you can look at the bad ones calmly.

How messages get there

You attach a DLQ to the source queue with a redrive policy, which has one key number: maxReceiveCount. It is how many times a message can be received without being deleted before SQS moves it to the DLQ.

  • Set it to 5, and a message that has been received five times and never deleted moves aside.
  • Set it too low, like 1, and a single timeout or temporary glitch sends good messages to the DLQ. Leave room for a few retries.

On the other side, a redrive allow policy on the DLQ controls which source queues may use it.

The rules people trip over

  • Same account and same Region as the source queue.
  • Same type: the DLQ of a FIFO queue must be a FIFO queue.
  • Retention: for standard queues, a message's expiry is based on when it was first sent, not when it moved to the DLQ. If a message spent three days in a source queue with four-day retention and the DLQ also keeps four days, it disappears after one more day. AWS recommends giving the DLQ a longer retention than the source; the maximum is 14 days.
  • FIFO order: moving a message aside breaks strict ordering for its group. If the order of every step matters, think twice before adding a DLQ to a FIFO queue.

Alarm, fix, redrive

A DLQ nobody watches is just a slower way to lose data. Create a CloudWatch alarm on the DLQ's ApproximateNumberOfMessagesVisible metric so that any message landing there pages someone.

When the bug is fixed, use DLQ redrive: from the console or the StartMessageMoveTask API, SQS moves the messages back to their source queue (or another queue of the same type) for another try. You can cap the speed, so a big backlog doesn't swamp the workers. Redriven messages count as new messages, with a fresh retention period.

With Lambda reading from SQS, also turn on partial batch responses (ReportBatchItemFailures), so one bad message in a batch doesn't send the good ones back to the queue with it.

Where AI fits

Queues in front of AI models see their own kind of poison message: a prompt that will always fail. A document too long for the model's context window, a malformed request, an input the model rejects. Without a DLQ, it is retried again and again, wasting time and invocations. With one, it lands in quarantine after a few tries. You inspect it, fix the cause (split the document, fix the template, pick another model), and redrive it.

Quick recap

  • A redrive policy with maxReceiveCount moves repeated failures to the DLQ.
  • Same account, Region and queue type; longer retention than the source.
  • Alarm on the DLQ, fix, then redrive.
AWS in 1 Min #127 · 1:14

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS