How To Configure Dead Letter Queue In SQS

How To Configure Dead Letter Queue In SQS

How to configure dead letter queue in SQS

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to enable delay queue in SQS.

https://cloudaffaire.com/how-to-configure-delay-queue-in-sqs/

In this blog post, we will discuss how to configure a dead letter queue in SQS.

What is dead letter queue?

A dead letter queue is used to debug your messaging application. Dead letter queue is used with the source queue to debug messages in source queue that for some reason (for example network issue) cannot be processed by your application. When you configure a dead letter queue for a source queue, you need to provide a redrive policy defining your source queue, dead letter queue, and the conditions under which AWS SQS will move the message from your source queue to dead letter queue. By default, the dead letter queue is not created when you create an SQS queue.

The expiration of a message is always based on its original enqueue timestamp. When a message is moved to a dead-letter queue, the enqueue timestamp remains unchanged. For example, if a message spends 1 day in the original queue before being moved to a dead-letter queue, and the retention period of the dead-letter queue is set to 4 days, the message is deleted from the dead-letter queue after 3 days. Thus, it is a best practice to always set the retention period of a dead-letter queue to be longer than the retention period of the original queue.

Note: The dead-letter queue of a FIFO queue must also be a FIFO queue. Similarly, the dead-letter queue of a standard queue must also be a standard queue. Also, the dead letter queue needs to be created in the same region as your source queue.

Next, we are going to configure dead letter queue in SQS through AWS CLI.

How to configure dead letter queue in SQS:

Prerequisite for this demo:

  • AWS SQS FIFO queue.
  • AWS CLI configured with required access.

Step 1: Configure AWS CLI.

Step 2: Create a FIFO queue that will be used as a dead letter queue for your source FIFO queue.

Step 3: Query the list of queues available and copy the queue URL.

How To Configure Dead Letter Queue In SQS

Note: ‘myqueue’ will be our source queue and ‘mydeadletterqueue’ will serve as dead letter queue to myqueue.

Step 4: Get Queue ARN for your dead letter queue.

How To Configure Dead Letter Queue In SQS

Step 5: Configure your source queue to enable dead letter queue.

How To Configure Dead Letter Queue In SQS

Note: Max Receive Count is set to 3, meaning if we consume a message from source queue more than 3 times without deleting it, the message will be moved from source queue to dead letter queue.

Step 6: Send a message to the queue and then try to consume the message more than 3 times.

How To Configure Dead Letter Queue In SQS

Observation: Since ‘MaxReceiveCount’ is set to 3 for our dead letter queue ‘RedrivePolicy’, the message is sent to dead letter queue after 3 ‘ReceiveMessage’ request.

Hope you have enjoyed this article. In the next blog post, we will discuss how to enable encryption in SQS queue.

To get more details on AWS SQS, please refer below AWS documentation

https://docs.aws.amazon.com/sqs/index.html

 

Leave a Reply

Close Menu