|« PreviousNext »|
|Did this page help you? Yes | No | Tell us about it...|
The API Version 2012-11-05 of Amazon SQS provides support for long polling.
One benefit of long polling with Amazon SQS is the reduction of 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. Long polling allows the Amazon SQS service to wait until a message is available
in the queue before sending a response. So unless the connection times out, the response to
ReceiveMessage request will contain at least one of the available messages (if any)
and up to the maximum number requested in the
Another benefit is helping to eliminate false empty responses, where messages are available in the queue but are not included in the response. This happens when Amazon SQS uses short (standard) polling, the default behavior, where only a subset of the servers (based on a weighted random distribution) are queried to see if any messages are available to include in the response. On the other hand, when long polling in enabled, Amazon SQS queries all of the servers.
Reducing the number of empty responses and false empty responses also helps reduce your cost of using Amazon SQS.
Short polling occurs when the
parameter of a
ReceiveMessage call is set to 0. This happens in one of two ways –
ReceiveMessage call sets
WaitTimeSeconds to 0,
call doesn’t set
WaitTimeSeconds and the
ReceiveMessageWaitTimeSeconds is 0.
A value set between 1 to 20 for the
ReceiveMessage has priority over any value set for the
There are three different API action calls you can use to enable long polling in Amazon SQS,
ReceiveMessage, you configure the
SetQueueAttributes, you configure the
If you decide to implement long polling with multiple queues then it is recommended that you use one thread for each queue, instead of trying to use one single thread for polling all of the queues. When using one thread for each queue your application is able to process the messages in each of the queues as they become available, as opposed to one single thread for multiple queues where your application could be blocked from processing available messages in the other queues while waiting (up to 20 seconds) on a queue that does not have any available messages.
For most use cases when using long polling, you should set the timeout value to the maximum of 20 seconds. If the 20 second maximum does not work for your application, you can choose a shorter long poll timeout, down to as low as 1 second. If you are not using an AWS SDK to access SQS, or if you’ve specially configured your AWS SDK to have a shorter timeout, you may need to modify your SQS client to allow for longer requests or use a shorter long poll timeout.
You can enable long polling using the AWS Management Console by setting a Receive Message Wait Time value that is greater than 0.
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/.
Click Create New Queue.
In the Create New Queue dialog box,
enter a name for your queue (e.g.,
MyQueue) in the Queue Name
Enter a positive integer value, from 1 to 20 seconds, for Receive Message Wait Time.
You can leave the default value settings for the remaining fields or enter new values.
Click Create Queue.
You can use the AWS Management Console to change the Receive Message Wait Time setting for an existing queue by selecting the Configure Queue action with an existing queue highlighted.
The following Query API example enables long polling by calling the
ReceiveMessage action with the
parameter set to 10 seconds. For more information, go to ReceiveMessage in the Amazon Simple Queue Service API Reference.
http://sqs.us-east-1.amazonaws.com/123456789012/testQueue/ ?Action=ReceiveMessage &WaitTimeSeconds=10 &MaxNumberOfMessages=5 &VisibilityTimeout=15 &AttributeName=All; &Version=2012-11-05 &SignatureMethod=HmacSHA256 &Expires=2013-10-25T22%3A52%3A43PST &AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE &SignatureVersion=2 &Signature=Dqlp3Sd6ljTUA9Uf6SGtEExwUQEXAMPLE
The following example shows another option for enabling long polling. Here, the
ReceiveMessageWaitTimeSeconds attribute for the
SetQueueAttributes action is set to 20 seconds. For more
information, go to SetQueueAttributes in the Amazon Simple Queue Service API Reference.
http://sqs.us-east-1.amazonaws.com/123456789012/testQueue/ ?Action=SetQueueAttributes &Attribute.Name=ReceiveMessageWaitTimeSeconds &Attribute.Value=20 &Version=2012-11-05 &SignatureMethod=HmacSHA256 &Expires=2013-10-25T22%3A52%3A43PST &AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE &SignatureVersion=2 &Signature=Dqlp3Sd6ljTUA9Uf6SGtEExwUQEXAMPLE