This blog post is written by Mark Richman, a Senior Solutions Architect for SMB.
Today AWS is launching a new capability to enhance the dead-letter queue (DLQ) management experience for Amazon Simple Queue Service (SQS). DLQ redrive to source queues allows SQS to manage the lifecycle of unconsumed messages stored in DLQs.
SQS is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. Using Amazon SQS, you can send, store, and receive messages between software components at any volume without losing messages or requiring other services to be available.
To use SQS, a producer sends messages to an SQS queue, and a consumer pulls the messages from the queue. Sometimes, messages can’t be processed due to a number of possible issues. These can include logic errors in consumers that cause message processing to fail, network connectivity issues, or downstream service failures. This can result in unconsumed messages remaining in the queue.
Understanding SQS dead-letter queues (DLQs)
SQS allows you to manage the life cycle of the unconsumed messages using dead-letter queues (DLQs).
A DLQ is a separate SQS queue that one or many source queues can send messages that can’t be processed or consumed. DLQs allow you to debug your application by letting you isolate messages that can’t be processed correctly to determine why their processing didn’t succeed. Use a DLQ to handle message consumption failures gracefully.
When you create a source queue, you can specify a DLQ and the condition under which SQS moves messages from the source queue to the DLQ. This is called the redrive policy. The redrive policy condition specifies the
maxReceiveCount. When a producer places messages on an SQS queue, the
ReceiveCount tracks the number of times a consumer tries to process the message. When the
ReceiveCount for a message exceeds the
maxReceiveCount for a queue, SQS moves the message to the DLQ. The original message ID is retained.
For example, a source queue has a redrive policy with
maxReceiveCount set to 5. If the consumer of the source queue receives a message 6, without successfully consuming it, SQS moves the message to the dead-letter queue.
You can configure an alarm to alert you when any messages are delivered to a DLQ. You can then examine logs for exceptions that might have caused them to be delivered to the DLQ. You can analyze the message contents to diagnose consumer application issues. Once the issue has been resolved and the consumer application recovers, these messages can be redriven from the DLQ back to the source queue to process them successfully.
Previously, this required dedicated operational cycles to review and redrive these messages back to their source queue.
DLQ redrive to source queues
DLQ redrive to source queues enables SQS to manage the second part of the lifecycle of unconsumed messages that are stored in DLQs. Once the consumer application is available to consume the failed messages, you can now redrive the messages from the DLQ back to the source queue. You can optionally review a sample of the available messages in the DLQ. You redrive the messages using the Amazon SQS console. This allows you to more easily recover from application failures.
Using redrive to source queues
To show how to use the new functionality there is an existing standard source SQS queue called MySourceQueue.
SQS does not create DLQs automatically. You must first create an SQS queue and then use it as a DLQ. The DLQ must be in the same region as the source queue.
- Navigate to the SQS Management Console and create a standard SQS queue for the DLQ called MyDLQ. Use the default configuration. Refer to the SQS documentation for instructions on creating a queue.
- Navigate to MySourceQueue and choose Edit.
- Navigate to the Dead-letter queue section and choose Enabled.
- Select the Amazon Resource Name (ARN) of the MyDLQ queue you created previously.
- You can configure the number of times that a message can be received before being sent to a DLQ by setting Set Maximum receives to a value between 1 and 1,000. For this demo enter a value of 1 to immediately drive messages to the DLQ.
- Choose Save.
The console displays the Details page for the queue. Within the Dead-letter queue tab, you can see the Maximum receives value and DLQ ARN.
Send and receive test messages
You can send messages to test the functionality in the SQS console.
- Navigate to MySourceQueue and choose Send and receive messages
- Send a number of test messages by entering the message content in Message body and choosing Send message.
- Navigate to the Receive messages section where you can see the number of messages available.
- Choose Poll for messages. The Maximum message count is set to 10 by default If you sent more than 10 test messages, poll multiple times to receive all the messages.
All the received messages are sent to the DLQ because the maxReceiveCount is set to 1. At this stage you would normally review the messages. You would determine why their processing didn’t succeed and resolve the issue.
Redrive messages to source queue
Navigate to the list of all queues and filter if required to view the DLQ. The queue displays the approximate number of messages available in the DLQ. For standard queues, the result is approximate because of the distributed architecture of SQS. In most cases, the count should be close to the actual number of messages in the queue.
- Select the DLQ and choose Start DLQ redrive.
- Choose to Redrive to source queue(s), which is the default.
SQS allows you to redrive messages either to their source queue(s) or to a custom destination queue.
Redrive has two velocity control settings.
- System optimized sends messages back to the source queue as fast as possible
- Custom max velocity allows SQS to redrive messages with a custom maximum rate of messages per second. This feature is useful for minimizing the impact to normal processing of messages in the source queue.
You can optionally inspect messages prior to redrive.
The Dead-letter queue redrive status panel shows the status of the redrive and percentage processed. You can refresh the display or cancel the redrive.
Once the redrive is complete, which takes a few seconds in this example, the status reads Successfully completed.
Dead-letter queue redrive to source queues allows you to effectively manage the life cycle of unconsumed messages stored in dead-letter queues. You can build applications with the confidence that you can easily examine unconsumed messages, recover from errors, and reprocess failed messages.
You can redrive messages from their DLQs to their source queues using the Amazon SQS console.
Dead-letter queue redrive to source queues is available in all commercial regions, and coming soon to GovCloud.
To get started, visit https://aws.amazon.com/sqs/
For more serverless learning resources, visit Serverless Land.