Sub topic 9
Dead-letter queues and redrive policies
This chapter assumes you know how to send, receive, and delete SQS messages, and how the visibility timeout hides a message while a consumer works on it. It also assumes you can read basic boto3 code.
The problem: poison messages
A consumer deletes a message only after it processes the message successfully. If processing keeps failing, the message becomes visible again after each visibility timeout and gets received again. A message that always fails is called a poison message. It wastes consumer capacity and never leaves the queue until the retention period ends.
A dead-letter queue (DLQ) is an ordinary SQS queue that holds messages another queue gave up on. You attach it to the source queue with a redrive policy.
How the redrive policy works
SQS counts how many times a message has been received. This count is the ReceiveCount. When it exceeds maxReceiveCount, SQS moves the message to the DLQ instead of delivering it again.
Creating a DLQ and redrive policy
Create the DLQ first and read its ARN. Then create the main queue with a RedrivePolicy attribute that names the DLQ.
import json
import boto3
sqs = boto3.client("sqs")
# Dead-letter queue for messages that keep failing
dlq_url = sqs.create_queue(QueueName="orders-dlq")["QueueUrl"]
dlq_arn = sqs.get_queue_attributes(
QueueUrl=dlq_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
# Main queue: 60 s visibility timeout, long polling by default,
# and move a message to the DLQ after 5 failed receives
queue_url = sqs.create_queue(
QueueName="orders",
Attributes={
"VisibilityTimeout": "60",
"ReceiveMessageWaitTimeSeconds": "20",
"RedrivePolicy": json.dumps(
{"deadLetterTargetArn": dlq_arn, "maxReceiveCount": "5"}
),
},
)["QueueUrl"]
print(queue_url)With these settings, a message that fails five times is moved to orders-dlq. Each attempt waits at least 60 seconds, so the message spends roughly five minutes in the source queue before it moves.
Rules and gotchas
- A FIFO queue needs a FIFO DLQ. A standard queue needs a standard DLQ.
- Create the DLQ in the same account and Region as the source queue.
- Set the DLQ retention period longer than the source queue's. The clock keeps the message's original enqueue timestamp when it moves, so a DLQ with equal retention can expire the message almost immediately.
- Choose
maxReceiveCountwith care. A value of 1 sends a message to the DLQ after one failure, even for a brief network error. A very high value delays isolation of real poison messages. - A message that is received but not deleted counts toward
ReceiveCounteven if your code crashed. Consumers that time out increase the count too.
Redriving messages
After you fix the bug, you can send DLQ messages back to the source queue. This is called redrive. The SQS console offers "Start DLQ redrive", and the API offers StartMessageMoveTask. You must fix the cause first, or the messages will fail again and return to the DLQ.
import boto3
sqs = boto3.client("sqs")
dlq_url = sqs.get_queue_url(QueueName="orders-dlq")["QueueUrl"]
dlq_arn = sqs.get_queue_attributes(
QueueUrl=dlq_url, AttributeNames=["QueueArn"]
)["Attributes"]["QueueArn"]
# With no DestinationArn, messages go back to their original source queue
task = sqs.start_message_move_task(SourceArn=dlq_arn)
print(task["TaskHandle"])Redriven messages can be processed twice if the first attempt partly succeeded. This is another reason to write idempotent consumers.
Exercise
A queue has a redrive policy with maxReceiveCount 3 and a visibility timeout of 30 seconds. A consumer receives a message and crashes each time before deleting it. What happens to the message, and why should the DLQ retention period be longer than the source queue's?
Show answer
After the third failed receive, the next receive attempt finds that ReceiveCount would exceed 3, so SQS moves the message to the DLQ instead of delivering it. The message keeps its original enqueue time, so a DLQ with the same or shorter retention could expire it before anyone investigates.