You are currently viewing SQS Queue Default Visibility Timeout

SQS Queue Default Visibility Timeout

SQS queue default visibility timeout

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed basic operations like sending, receiving and deleting a message to an SQS queue.

https://cloudaffaire.com/sqs-queue-operations/

In this blog post, we will discuss default visibility timeout for your SQS queue.

SQS queue default visibility timeout:

AWS SQS does not delete a message automatically, thus there is a chance that same message is consumed and processed by two different consumers. To prevent this, AWS SQS apply visibility timeout. Visibility timeout starts when a consumer consumes a message and during the visibility timeout no other consumer is allowed to consume the same message. This prevents duplicate processing of same message. Visibility timeout can be set to the entire queue or to individual messages. The default visibility timeout setting for a queue is 30 seconds.

You can change this setting for the entire queue. Typically, you should set the visibility timeout to the maximum time that it takes your application to process and delete a message from the queue. When receiving messages, you can also set a special visibility timeout (ChangeMessageVisibility) for the returned messages without changing the overall queue timeout.

SQS Queue Default Visibility Timeout

Note: For standard queues, the visibility timeout isn’t a guarantee against receiving a message twice.

Next, we are going to explain visibility timeout through a demo.

Prerequisite for this demo:

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

Step 1: Configure AWS CLI.

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

SQS Queue Default Visibility Timeout

Step 3: Get the current queue attributes.

SQS Queue Default Visibility Timeout

Note: Current queue visibility timeout is set to default 30 seconds.

Step 3: Send a message to the queue.

SQS Queue Default Visibility Timeout

Step 4: Now try to receive the message. Execute below command couple of times within 30 seconds.

SQS Queue Default Visibility Timeout

Observation: The 1st time a consumer receives the message, the visibility timeout starts and during the next 30 seconds, no other consumer can receive the same message (highlighted above). Only after 30 seconds is passed and provided no other consumer consumes your message, you can receive the message again.

Next, we are going to reduce the visibility timeout value to 1 second which in turn will reduce this timeout gap.

Step 5: Change the default visibility timeout value to 1 second for the entire queue and try to receive the message a couple of times.

SQS Queue Default Visibility Timeout

Note: Since we have changed the visibility timeout to 1 second, the message is available and can be re-consume instantaneously once consumed.

Hope you have enjoyed this article. In the next blog post, we will discuss the SQS queue long polling option.

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

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

 

Leave a Reply