How to Set Up an AWS SQS Dead-Letter Queue

A dead-letter queue catches messages that keep failing so they stop spinning in your main queue forever. You add a second SQS queue and a redrive policy. Here's the Terraform and how it works.

DevOps Engineerawssqsdlq

The problem a DLQ solves

If a consumer can't process a message, SQS makes it visible again and the consumer retries it - forever. One "poison" message can block the queue and burn your consumer in a loop. A dead-letter queue (DLQ) is where SQS moves a message after it's failed too many times, so the main queue keeps flowing and you can inspect the bad ones separately.

The setup: a second queue + a redrive policy

# The DLQ - just a normal queue
resource "aws_sqs_queue" "dlq" {
  name = "orders-dlq"
}

# The main queue points failed messages at the DLQ
resource "aws_sqs_queue" "orders" {
  name = "orders"

  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.dlq.arn
    maxReceiveCount     = 3      # after 3 failed receives -> DLQ
  })
}

How it behaves

  1. Consumer receives a message, fails, doesn't delete it.
  2. After the visibility timeout it reappears; repeat up to maxReceiveCount.
  3. On the next failure past the limit, SQS moves it to orders-dlq automatically.

Now the main queue isn't stuck, and every poison message is parked in the DLQ.

Operating it

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What is an SQS dead-letter queue?

A separate SQS queue where messages land after they've been received and failed too many times (maxReceiveCount). It stops poison messages from looping forever in the main queue and lets you inspect them.

How do I configure an SQS DLQ?

Create a second queue, then set a redrive_policy on the main queue with deadLetterTargetArn (the DLQ's ARN) and maxReceiveCount (e.g. 3). After that many failed receives SQS moves the message to the DLQ.

What is maxReceiveCount?

How many times a message can be received without being successfully deleted before SQS routes it to the dead-letter queue. Typical values are 3-5.

What is a dead-letter queue?

A dead-letter queue (DLQ) is a separate SQS queue that receives messages a consumer fails to process after a set number of attempts. It stops poison messages from blocking the main queue and lets you inspect failures separately.

What is the best practice for a dead-letter queue?

Set a sensible maxReceiveCount (commonly 3-5) so transient failures retry but poison messages move to the DLQ, alarm on DLQ depth, and replay messages once the bug is fixed.

Keep learning

Fix the Nginx 502 Bad GatewayDevOps projectRecover a Crashed Linux ServiceDevOps projectCorrect a Postgres Port MismatchDevOps projectDevOps roadmapStep by step to hiredDevOps interview questionsSTAR answersAll DevOps projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →