Amazon SQS Long Polling
Amazon SQS uses short polling by default, querying only a subset of the servers (based on a weighted random distribution) to determine whether any messages are available for inclusion in the response.
Short polling occurs when the
WaitTimeSeconds parameter of a
ReceiveMessage call is set to
0 in one of two ways:
ReceiveMessagecall doesn’t set
WaitTimeSecondsand the queue attribute
ReceiveMessageWaitTimeSecondsis set to
WaitTimeSeconds parameter of
a value set between
20 has priority over any
value set for the queue attribute
Benefits of Long Polling
Long polling helps reduce your cost of using Amazon SQS by
reducing the number of empty responses (when there are no messages available to
return in reply to a
ReceiveMessage request sent to an Amazon SQS queue) and
eliminating false empty responses (when messages are available in the queue but
aren't included in the response):
Long polling reduces the number of empty responses by allowing Amazon SQS to wait until a message is available in the queue before sending a response. Unless the connection times out, the response to the
ReceiveMessagerequest contains at least one of the available messages, up to the maximum number of messages specified in the
Long polling eliminates false empty responses by querying all (rather than a limited number) of the servers.
Long polling returns messages as soon any message becomes available.
Enabling Long Polling with the AWS Management Console
You can enable long polling using the AWS Management Console by setting a Receive Message Wait
Time to a value greater than
To enable long polling with the AWS Management Console for a new queue
Sign in to the AWS Management Console and open the Amazon SQS console at https://console.aws.amazon.com/sqs/.
Select Create New Queue.
In the Create New Queue dialog box, type the Queue Name.
For Receive Message Wait Time, type a positive integer value, from
Choose Create Queue.
You can use the AWS Management Console to change the Receive Message Wait Time setting for an existing queue.
To set a new Receive Message Wait Time value for an existing queue
Select a queue.
From the Queue Actions drop-down list, select Configure Queue.
For Receive Message Wait Time, type a positive integer value.
Choose Save Changes.
Enabling Long Polling Using the API
The following table lists the API actions to use.
If you decide to implement long polling with multiple queues, we recommend using one thread for each queue instead of trying to use a single thread for polling all of the queues.
When you use one thread for each queue, your application can process the messages in each of the queues as they become available. A single thread for multiple queues might cause your application to become blocked from processing available messages in the other queues while waiting (up to 20 seconds) for a queue that doesn't have any available messages.
In most cases, when using long polling, set the timeout value to a maximum of 20 seconds. If the 20-second maximum doesn't work for your application, set a shorter timeout for long polling (the minimum is 1 second). If you don't use an AWS SDK to access Amazon SQS, or if you configure an AWS SDK to have a shorter timeout, you may need to modify your Amazon SQS client to allow for longer requests or to use a shorter timeout for long polling.
Enabling Long Polling Using the Query API
The following example enables long polling by calling the
action with the
WaitTimeSeconds parameter set to 10 seconds.
How you structure
AUTHPARAMS depends on how you sign your API request. For
AUTHPARAMS in Signature Version 4, see Examples of Signed Signature Version 4 Requests in the
Amazon Web Services General Reference.
http://sqs.us-east-2.amazonaws.com/123456789012/testQueue/ ?Action=ReceiveMessage &WaitTimeSeconds=10 &MaxNumberOfMessages=5 &VisibilityTimeout=15 &AttributeName=All; &Version=2012-11-05 &Expires=2013-10-25T22%3A52%3A43PST &AUTHPARAMS
The following example shows another way to enable long polling. Here, the
ReceiveMessageWaitTimeSeconds attribute for the
SetQueueAttributes action is set to 20 seconds.
http://sqs.us-east-2.amazonaws.com/123456789012/testQueue/ ?Action=SetQueueAttributes &Attribute.Name=ReceiveMessageWaitTimeSeconds &Attribute.Value=20 &Version=2012-11-05 &Expires=2013-10-25T22%3A52%3A43PST &AUTHPARAMS