You are currently viewing How To Enable Long Polling In SQS Queue

How To Enable Long Polling In SQS Queue

  • Post author:
  • Post category:AWS / SQS

How to enable long polling in SQS queue

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed SQS queue default visibility timeout

https://cloudaffaire.com/sqs-queue-default-visibility-timeout/

In this blog post, we will discuss how to configure long polling in SQS queue.

How to enable long polling in SQS queue:

AWS SQS is a poll based service, meaning SQS will not push messages to your application but rather your application needs to poll messages from SQS queue. Further, AWS charges you on per request (SendMessage, ReceiveMessage and DeleteMessage) basis so if you poll your SQS queue and its returns an empty response, you will still be charged. The default polling for SQS is short polling, meaning the response is sent without any wait time even if the queue is empty or updated with new messages. Long polling helps reduce the cost of using AWS SQS by eliminating the number of empty responses (when there are no messages available for a ReceiveMessage request) and false empty responses (when messages are available but aren’t included in a response).

SQS is distributed by nature and messages are stored across different servers. In case of short polling, SQS only query a subset of these servers and returns the response. But in case of long polling, SQS tries to query the entire servers within 20 seconds and returns as many messages as possible. The polling behavior of a queue is controlled by ‘Receive Message Wait Time’ attributes and by default is set to 0 representing short polling.

Next, we are going to configure long polling for an SQS queue through AWS CLI

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.

How To Enable Long Polling In SQS Queue

Step 3: Configure the visibility timeout to 10 seconds for your queue.

Step 4: Get the current attributes for your queue.

How To Enable Long Polling In SQS Queue

Note: Currently the queue is set for short polling (ReceiveMessageWaitTimeSeconds = 0).

Step 5: Send a message to your queue.

Step 6: Try to receive message couple of times within 10 seconds.

How To Enable Long Polling In SQS Queue

Observation: Since the queue is set for short polling and visibility timeout is set to 10 seconds, once the message is received the second recevicemessage request returns an empty response. Next, we will enable long polling by defining Receive message wait time seconds value to 20.

Step 7: Configure long polling for the queue.

Step 8: Try to receive message a couple of times.

How To Enable Long Polling In SQS Queue

Observation: You will receive the message every time you send receive message request to the queue. Since the visibility timeout is set to 10 seconds and your receive message request wait for maximum 20 seconds the message is again visible to you within the polling time.

Hope you have enjoyed this article. In the next blog post, we will discuss the delay queue in SQS.

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

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