Working with Dead-letter Queues in AWS SQS and C#

Search for a command to run...

No comments yet. Be the first to comment.
In this series, we delve into the captivating universe of cloud computing, utilizing AWS and C# programming language.
AWS S3 (Simple storage service) is an AWS service that provides users the ability to store and retrieve static data seamlessly and efficiently. In this article, we are exploring how to use AWS S3 in a .NET application. The code for this article is av...
Deploying a .NET application to a cloud environment like DigitalOcean is a powerful way to ensure scalability, reliability, and ease of management. In this post, we’ll walk through deploying a real-world .NET application—a simple ASP.NET Core MVC app...

Building a gRPC Service in C# and ASP.NET Core – Part 2

Building a gRPC Service in C# and ASP.NET Core – Part 1

Software architecture is the backbone of any application, and a well-thought-out architecture can make the difference between a maintainable, scalable application and a fragile, difficult-to-modify one. Clean Architecture, popularized by Robert C. Ma...

In software development, as systems grow more complex, objects start interacting with each other in increasingly intricate ways. Over time, this can lead to a tightly coupled, hard-to-maintain system where modifying or extending a particular feature ...

What happens when messages sent via a messaging queue fail to be processed? Is it skipped? Is the data or intention encapsulated in that message lost? This is the problem that dead-letter queues solve. Dead-letter queues are like any other, with the key differentiator being that they act as a "store" for unprocessed messages to be subsequently processed. After a specified number of retries (SQS defaults to 2), unsuccessfully processed messages are sent to the queue.
This article will explore implementing a dead-letter queue with AWS and C#.
Please note this article is a sequel to "Asynchronous Communication In Microservices Via C# and AWS SQS." Kindly go through that and return to proceed.
The code for this article is available here.
In the previous blog post, we created a messaging queue, and published and consumed messages from it. Right now, we would add a dead-letter queue to our code.
Let's get started!
Create a new queue on your AWS SQS dashboard.
Give it the name orders-dlq
As part of the configurations while creating the queue, enable "Redrive allow policy," and select your main (orders) queue from the dropdown as the source queue. 
orders queue. Enable "Dead-letter queue" and set the newly created orders-dlq as the dead-letter queue. 
And that's it!
To test that this works:
Publish a new message to our main orders queue by making a request to our Orders.Api


Simulate an error by throwing an exception that will be handled in the catch block. And subsequently placed in the dead-letter queue.
try
{
throw new ApplicationException("unable to process message");
await _mediator.Send(deserializedMessage, stoppingToken);
await _amazonSqs.DeleteMessageAsync(new DeleteMessageRequest
{
QueueUrl = queueUrl.QueueUrl,
ReceiptHandle = message.ReceiptHandle
}, stoppingToken);
}
Run the consumer to pull from the orders queue.
Confirm the presence of this message in our orders-dlq on the AWS SQS dashboard. As we can see, the unprocessed message gets inserted into our dead-letter queue.

Trigger a reprocessing by clicking the "start DLQ redrive" button. We can remove the exception we intentionally threw from our consumer app this time. The redrive would transport these unprocessed messages from orders-dlq queue to our main orders queue to be reprocessed.

Rerun the Consumer app. This time, we have no messages in our orders or orders-dlq meaning our messages are now being processed.
We can also verify this from the output on our console. 
And that's it! The code for this article is available here.