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

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**](https://devwithjosh.com/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](https://github.com/JoshPhronesis/AsynchronousCommunication).

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!

1. Create a new queue on your AWS SQS dashboard.
    
    Give it the name `orders-dlq` ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693152847659/098e3321-2b25-45d5-b670-aeed8f37d8f4.png align="center") 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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693153081804/e513cb18-5ea5-4f0f-a738-d6a1f31f7d75.png align="right")
2. Edit your main `orders` queue. Enable "**Dead-letter queue**" and set the newly created `orders-dlq` as the dead-letter queue. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693153278044/2554ba16-abba-4e5e-ad47-1e63f4fac985.png align="center")

And that's it!

---

To test that this works:

1. Publish a new message to our main `orders` queue by making a request to our `Orders.Api`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154430989/61d63eac-bd2c-4bb9-9845-7aa6cb0c3a65.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154463867/d001a0fa-b0ed-4770-a802-c6ad87fb001d.png align="center")
    
2. Simulate an error by throwing an exception that will be handled in the catch block. And subsequently placed in the dead-letter queue.
    
    ```csharp
    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);
    }
    ```
    
3. Run the consumer to pull from the `orders` queue.
    
4. 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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693155078331/b1687f2b-d404-47e6-b78e-b73322bd26bd.png align="center")
    
5. 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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693155231867/3af25c0a-6842-4cad-bbe1-c350fc579419.png align="center") ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693155441110/ae2a24e7-8bf3-4c28-a2b6-41853b14a660.png align="center")

6. Rerun the Consumer app. This time, we have no messages in our `orders` or `orders-dlq` meaning our messages are now being processed. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693156483117/79523ffc-6214-4bda-84b7-c06ed5e0857e.png align="center")
We can also verify this from the output on our console. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693156547220/c6fdf2e1-9b2f-48b1-a905-1e4b8e6063cc.png align="center")

And that's it! The code for this article is available [here](https://github.com/JoshPhronesis/AsynchronousCommunication).
