Using Amazon SQS Dead Letter Queues - AWS SDK for .NET (version 3)

This documentation is for version 3.0 of the AWS SDK for .NET, which is mostly centered around .NET Framework and ASP.NET 4.x, Windows, and Visual Studio.

The latest version of the documentation at is mostly centered around .NET Core and ASP.NET Core. In addition to Windows and Visual Studio, it gives equal consideration to cross-platform development.

Using Amazon SQS Dead Letter Queues

This example shows you how to use a queue to receive and hold messages from other queues that the queues can’t process.

A dead letter queue is one that other (source) queues can target for messages that can’t be processed successfully. You can set aside and isolate these messages in the dead letter queue to determine why their processing did not succeed. You must individually configure each source queue that sends messages to a dead letter queue. Multiple queues can target a single dead letter queue.

In this example, an AmazonSQSClient object uses the SetQueueAttributesRequest method to configure a source queue to use a dead letter queue.

For more information about Amazon SQS dead letter queues, see Using Amazon SQS Dead Letter Queues in the Amazon Simple Queue Service Developer Guide.

Configure a Source Queue

This code example assumes you have created a queue to act as a dead letter queue. See Creating an Amazon SQS Queue for information about creating a queue. After creating the dead letter queue, you must configure the other queues to route unprocessed messages to the dead letter queue. To do this, specify a redrive policy that identifies the queue to use as a dead letter queue and the maximum number of receives by individual messages before they are routed to the dead letter queue.

Create an AmazonSQSClient object to set the queue attributes. Create a SetQueueAttributesRequest object containing the properties needed to update queue attributes, including the RedrivePolicy property that specifies both the Amazon Resource Name (ARN) of the dead letter queue, and the value of maxReceiveCount. Also specify the URL source queue you want to configure. Call the SetQueueAttributes method.

AmazonSQSClient client = new AmazonSQSClient(); var setQueueAttributeRequest = new SetQueueAttributesRequest { Attributes = new Dictionary<string, string> { {"RedrivePolicy", @"{ ""deadLetterTargetArn"" : ""DEAD_LETTER_QUEUE_ARN"", ""maxReceiveCount"" : ""10""}" } }, QueueUrl = "SOURCE_QUEUE_URL" }; client.SetQueueAttributes(setQueueAttributeRequest)