SQS DLQ

Difficulty: Intermediate

Question

What is a Dead Letter Queue (DLQ) in Amazon SQS, how does it work, and how do you handle messages that land in it?

Answer

A Dead Letter Queue is a separate SQS queue where messages are moved automatically after they fail processing a set number of times. You configure a redrive policy on the source queue with a maxReceiveCount; once a message has been received that many times without being deleted, SQS moves it to the DLQ. This stops a single bad "poison pill" message from being retried forever, blocking consumers and wasting compute, while keeping it safe for investigation and replay.

How a message ends up in the DLQ

  1. A consumer receives a message. SQS hides it for the visibility timeout and increments its receive count.
  2. If the consumer processes it successfully, it deletes the message.
  3. If the consumer crashes, throws, or times out, the message is not deleted. When the visibility timeout expires it becomes visible again and is retried.
  4. When the receive count exceeds maxReceiveCount, SQS moves the message to the DLQ instead of making it visible again.

Configuring it correctly

  • The DLQ must be the same type as the source: a FIFO queue needs a FIFO DLQ, and a standard queue needs a standard DLQ. It must also be in the same account and Region.
  • Pick maxReceiveCount deliberately. A value of 1 sends messages to the DLQ on the first transient error; something like 3 to 5 gives transient failures (a timeout calling a payment gateway) a chance to succeed.
  • Set the visibility timeout longer than the processing time. For Lambda consumers, AWS recommends at least six times the function timeout, otherwise messages reappear while still being processed and burn through receive counts.
  • Set the DLQ's retention period longer than the source queue's. For standard queues, a message's age is counted from its original enqueue time, so a message that spent days in the source queue can expire soon after arriving in the DLQ. Retention can be up to 14 days.
  • A redrive allow policy on the DLQ can restrict which source queues may use it.

What to do with messages in the DLQ

  • Alarm on it. A CloudWatch alarm on the DLQ's ApproximateNumberOfMessagesVisible greater than 0 is one of the most valuable alarms you can have. A DLQ nobody watches is just a slower way to lose data.
  • Inspect the message body and attributes to find the cause: bad data, a bug, a downstream outage.
  • Fix, then redrive. SQS supports moving messages from the DLQ back to the source queue (or another queue) from the console or via the StartMessageMoveTask API, with an optional rate limit so you do not overload the recovering system.

DLQs with Lambda

  • For SQS-triggered Lambda, the DLQ is configured on the SQS queue, not on Lambda. Enable ReportBatchItemFailures and return the IDs of failed messages, so one bad message does not cause the whole batch to be retried.
  • For asynchronous Lambda invocations (S3, SNS, EventBridge), Lambda has its own DLQ setting and, preferably, on-failure destinations. That is a different mechanism from the SQS redrive policy.

Gotchas

  • Messages in a DLQ are not retried automatically. Without alerting and a redrive process they simply expire.
  • Consumers must be idempotent, because a message may be processed more than once before reaching the DLQ, or again after redrive.
  • On FIFO queues, a message stuck retrying blocks later messages in the same message group until it succeeds or moves to the DLQ.
A poison message moving to the DLQ. After maxReceiveCount failed attempts, SQS moves the message to the DLQ and an alarm fires.
  1. Orders queue → Consumer: Deliver message (receive count 1)
  2. Consumer → Orders queue: Fails, no delete; visibility timeout expires
  3. Orders queue → Consumer: Redeliver (receive counts 2 and 3)
  4. Consumer → Orders queue: Fails again each time
  5. Orders queue → DLQ: maxReceiveCount exceeded: move message
  6. DLQ → CloudWatch: Messages visible above 0
  7. CloudWatch → Consumer: Alarm notifies team to fix and redrive

Code examples

Attach a DLQ and redrive messages after a fix

# Point the source queue at its DLQ after 5 failed receives
aws sqs set-queue-attributes \
  --queue-url https://sqs.ap-south-1.amazonaws.com/111122223333/orders \
  --attributes '{
    "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:ap-south-1:111122223333:orders-dlq\",\"maxReceiveCount\":\"5\"}",
    "VisibilityTimeout": "180"
  }'

# Keep DLQ messages for the maximum 14 days
aws sqs set-queue-attributes \
  --queue-url https://sqs.ap-south-1.amazonaws.com/111122223333/orders-dlq \
  --attributes MessageRetentionPeriod=1209600

# After fixing the bug, move messages back to the source queue
aws sqs start-message-move-task \
  --source-arn arn:aws:sqs:ap-south-1:111122223333:orders-dlq \
  --max-number-of-messages-per-second 50

Without a destination ARN, start-message-move-task sends messages back to their original source queue, rate-limited so the consumer is not flooded.

Key points

  • A DLQ receives messages that exceed maxReceiveCount on the source queue
  • DLQ type must match the source (FIFO with FIFO) and live in the same account and Region
  • Visibility timeout must exceed processing time (at least 6x the Lambda timeout for Lambda consumers)
  • Give the DLQ a longer retention than the source queue
  • Alarm on DLQ depth and redrive with StartMessageMoveTask after fixing the cause

Common follow-up questions

  • How do you choose a good maxReceiveCount?
  • What is the visibility timeout and how does it interact with retries?
  • How would you replay messages from a DLQ safely?
  • How does partial batch failure reporting work with SQS and Lambda?

Interview tips

  • Explain the receive count and visibility timeout mechanics, not just 'failed messages go there'
  • Always mention monitoring and redrive; an unmonitored DLQ is a red flag to interviewers
  • Distinguish the SQS redrive policy from Lambda's async DLQ and on-failure destinations

Concepts covered

Amazon SQS, Dead Letter Queue, maxReceiveCount, Visibility Timeout, Redrive

More AWS & Cloud Fundamentals questions