Bird
Raised Fist0
AWScloud~5 mins

Dead letter queues in AWS - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes messages sent to a queue cannot be processed correctly. Dead letter queues help by storing these problem messages separately so they can be checked and fixed later.
When your application receives messages that cause errors and you want to keep them for later review.
When you want to avoid losing messages that fail processing multiple times.
When you want to monitor and analyze why some messages fail in your queue system.
When you want to separate bad messages from good ones to keep your main queue clean.
When you want to retry processing failed messages after fixing the underlying issue.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_sqs_queue" "dlq_queue" {
  name = "example-dlq-queue"
}

resource "aws_sqs_queue" "main_queue" {
  name           = "example-main-queue"
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.dlq_queue.arn
    maxReceiveCount     = 3
  })
}

This Terraform file creates two AWS SQS queues:

  • example-main-queue: The main queue where messages are sent.
  • example-dlq-queue: The dead letter queue that stores messages that fail processing.

The redrive_policy on the main queue tells AWS to move messages to the dead letter queue after 3 failed processing attempts.

Commands
This command initializes Terraform in the current directory, downloading necessary plugins and preparing to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command creates the AWS SQS queues as defined in the Terraform file without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_sqs_queue.dlq_queue: Creating... aws_sqs_queue.dlq_queue: Creation complete after 2s [id=https://sqs.us-east-1.amazonaws.com/123456789012/example-dlq-queue] aws_sqs_queue.main_queue: Creating... aws_sqs_queue.main_queue: Creation complete after 2s [id=https://sqs.us-east-1.amazonaws.com/123456789012/example-main-queue] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
→
-auto-approve - Skip interactive approval before applying changes
This command checks the redrive policy of the main queue to confirm it is linked to the dead letter queue.
Terminal
aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/123456789012/example-main-queue --attribute-names RedrivePolicy
Expected OutputExpected
{ "Attributes": { "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:123456789012:example-dlq-queue\",\"maxReceiveCount\":3}" } }
→
--queue-url - Specifies the URL of the queue to check
→
--attribute-names - Specifies which attributes to retrieve
This command lists all SQS queues in your AWS account to verify both main and dead letter queues exist.
Terminal
aws sqs list-queues
Expected OutputExpected
{ "QueueUrls": [ "https://sqs.us-east-1.amazonaws.com/123456789012/example-main-queue", "https://sqs.us-east-1.amazonaws.com/123456789012/example-dlq-queue" ] }
Key Concept

If a message fails processing multiple times, it moves automatically to the dead letter queue for later inspection.

Common Mistakes
Not setting the redrive policy on the main queue.
Without the redrive policy, messages will never move to the dead letter queue, so failed messages get lost or block processing.
Always configure the redrive policy with the dead letter queue ARN and maxReceiveCount on the main queue.
Using the wrong ARN or queue URL in the redrive policy.
If the ARN is incorrect, AWS cannot move messages to the dead letter queue, causing errors or message loss.
Copy the exact ARN of the dead letter queue and use it in the redrive policy.
Setting maxReceiveCount too high or too low without testing.
Too low causes messages to move to DLQ too quickly; too high delays error detection.
Choose a reasonable maxReceiveCount like 3 to balance retries and error handling.
Summary
Create a dead letter queue and a main queue with a redrive policy linking them.
Use Terraform to define and deploy these queues in AWS.
Verify the redrive policy and queue existence with AWS CLI commands.

Practice

(1/5)
1. What is the main purpose of a Dead Letter Queue (DLQ) in AWS SQS?
easy
A. To speed up message delivery by duplicating messages
B. To store messages that failed processing to keep the main queue clean
C. To permanently delete messages after processing
D. To encrypt messages for security

Solution

  1. Step 1: Understand the role of DLQ

    A Dead Letter Queue is designed to hold messages that cannot be processed successfully after multiple attempts.
  2. Step 2: Compare options with DLQ purpose

    Only To store messages that failed processing to keep the main queue clean correctly describes storing failed messages to keep the main queue clean.
  3. Final Answer:

    To store messages that failed processing to keep the main queue clean -> Option B
  4. Quick Check:

    DLQ stores failed messages [OK]
Hint: DLQ holds failed messages, not speeding or deleting [OK]
Common Mistakes:
  • Thinking DLQ speeds up delivery
  • Confusing DLQ with message deletion
  • Assuming DLQ encrypts messages
2. Which of the following is the correct way to configure a Dead Letter Queue in AWS SQS using RedrivePolicy?
easy
A. {"deadLetterTargetArn":"arn:aws:sqs:region:account-id:DLQ","maxReceiveCount":5}
B. {"maxReceiveCount":5,"deadLetterQueueArn":"arn:aws:sqs:region:account-id:DLQ"}
C. {"deadLetterQueue":"arn:aws:sqs:region:account-id:DLQ","maxRetries":5}
D. {"targetArn":"arn:aws:sqs:region:account-id:DLQ","maxReceiveCount":10}

Solution

  1. Step 1: Identify correct RedrivePolicy keys

    The correct keys are "deadLetterTargetArn" and "maxReceiveCount" as per AWS documentation.
  2. Step 2: Match options with correct keys

    Only {"deadLetterTargetArn":"arn:aws:sqs:region:account-id:DLQ","maxReceiveCount":5} uses "deadLetterTargetArn" and "maxReceiveCount" correctly.
  3. Final Answer:

    {"deadLetterTargetArn":"arn:aws:sqs:region:account-id:DLQ","maxReceiveCount":5} -> Option A
  4. Quick Check:

    Correct keys = deadLetterTargetArn + maxReceiveCount [OK]
Hint: Look for deadLetterTargetArn and maxReceiveCount keys [OK]
Common Mistakes:
  • Using wrong key names like deadLetterQueueArn
  • Confusing maxRetries with maxReceiveCount
  • Swapping key order incorrectly
3. Given the following SQS queue configuration with a DLQ and maxReceiveCount set to 3, what happens when a message fails processing 4 times?
{"RedrivePolicy": {"deadLetterTargetArn": "arn:aws:sqs:region:account-id:DLQ", "maxReceiveCount": 3}}
medium
A. The message is moved to the Dead Letter Queue after the 4th failure
B. The message is deleted immediately after the 3rd failure
C. The message stays in the main queue indefinitely
D. The message is moved to the DLQ after the 3rd failure

Solution

  1. Step 1: Understand maxReceiveCount meaning

    maxReceiveCount defines how many times a message can be received before moving to DLQ.
  2. Step 2: Apply maxReceiveCount to message failures

    After 3 failed receives, the message is moved to the DLQ, not after the 4th.
  3. Final Answer:

    The message is moved to the DLQ after the 3rd failure -> Option D
  4. Quick Check:

    maxReceiveCount = 3 means move to DLQ after 3 failures [OK]
Hint: Message moves to DLQ exactly at maxReceiveCount failures [OK]
Common Mistakes:
  • Thinking message moves after 4th failure
  • Assuming message is deleted after failures
  • Believing message stays in main queue forever
4. You configured a DLQ with maxReceiveCount set to 5, but messages are not moving to the DLQ after 5 failed processing attempts. What is the most likely cause?
medium
A. maxReceiveCount must be set to 1 for DLQ to work
B. The DLQ ARN is invalid but messages still move to DLQ
C. The RedrivePolicy is missing or incorrectly attached to the main queue
D. Messages are deleted automatically after 3 failures regardless of DLQ

Solution

  1. Step 1: Check RedrivePolicy attachment

    If RedrivePolicy is missing or not attached properly, messages won't move to DLQ.
  2. Step 2: Validate other options

    Invalid ARN would cause errors, maxReceiveCount can be any positive number, and messages are not deleted automatically without DLQ.
  3. Final Answer:

    The RedrivePolicy is missing or incorrectly attached to the main queue -> Option C
  4. Quick Check:

    DLQ requires correct RedrivePolicy attachment [OK]
Hint: Verify RedrivePolicy is attached to main queue [OK]
Common Mistakes:
  • Ignoring RedrivePolicy attachment
  • Assuming invalid ARN still works
  • Believing maxReceiveCount must be 1
5. You have an SQS queue with a DLQ configured and maxReceiveCount set to 4. You want to ensure that messages that fail processing due to a temporary downstream service outage are retried multiple times before moving to the DLQ. Which approach best achieves this?
hard
A. Set maxReceiveCount to 4 and implement exponential backoff in the message consumer
B. Set maxReceiveCount to 1 and rely on DLQ for retries
C. Remove the DLQ and increase message visibility timeout
D. Set maxReceiveCount to 10 and disable retries in the consumer

Solution

  1. Step 1: Understand retry and DLQ roles

    maxReceiveCount controls how many times a message is retried before DLQ. Exponential backoff helps avoid flooding retries.
  2. Step 2: Evaluate options for retry strategy

    Set maxReceiveCount to 4 and implement exponential backoff in the message consumer combines proper maxReceiveCount with backoff to handle temporary failures effectively.
  3. Step 3: Reject other options

    Removing the DLQ is counterproductive as it's needed for persistent failures. Setting maxReceiveCount to 1 limits retries too much. Setting maxReceiveCount to 10 and disabling retries in the consumer is not effective for temporary outages.
  4. Final Answer:

    Set maxReceiveCount to 4 and implement exponential backoff in the message consumer -> Option A
  5. Quick Check:

    Retries + backoff before DLQ = best practice [OK]
Hint: Use maxReceiveCount with backoff for retries before DLQ [OK]
Common Mistakes:
  • Setting maxReceiveCount too low
  • Removing DLQ mistakenly
  • Disabling retries in consumer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard