Sub topic 5
Long Polling to Cut Empty Responses and Cost
This chapter assumes you can send and receive SQS messages with boto3 and understand the visibility timeout from the previous chapter. You also need basic AWS IAM knowledge.
Short polling and long polling
A consumer asks SQS for messages with ReceiveMessage. SQS can answer in two ways.
- Short polling queries a subset of the queue's servers and returns right away. The response can be empty even when messages exist, because the servers that hold them were not sampled.
- Long polling waits until a message arrives or the wait time ends, whichever comes first. It queries all servers, so it does not miss messages that are present.
Each ReceiveMessage call is a billed request. A consumer that loops on short polling during quiet periods makes many empty calls and pays for them. Long polling replaces many empty calls with one call that waits.
Turning on long polling
Long polling is on when the wait time is greater than zero. The maximum is 20 seconds. You can set it in two places.
- On the queue, with the
ReceiveMessageWaitTimeSecondsattribute. Every consumer then long polls by default. - On a single call, with the
WaitTimeSecondsparameter. This value overrides the queue setting for that call.
This code sets the queue default and then overrides it for one call.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
# Queue-level default: every receive waits up to 20 seconds
sqs.set_queue_attributes(
QueueUrl=queue_url,
Attributes={"ReceiveMessageWaitTimeSeconds": "20"},
)
# Call-level override: wait only 5 seconds for this request
resp = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=10,
WaitTimeSeconds=5,
)
print(len(resp.get("Messages", [])), "messages received")A queue-level default is the safer choice. A consumer that forgets to pass WaitTimeSeconds still long polls.
What to expect
A long poll returns as soon as at least one message is available. It does not wait to fill the batch up to MaxNumberOfMessages. When the wait ends with no messages, the response has no Messages key, so use resp.get("Messages", []).
Long polling also lowers latency for the first message after a quiet period. The open request receives the message the moment it arrives.
Common errors
- Client timeout shorter than the wait. The HTTP read timeout in your client must be longer than
WaitTimeSeconds. The boto3 default read timeout is 60 seconds, which is fine for 20. A custom timeout of 10 seconds makes the call fail with a read timeout. - A wait value above 20. SQS rejects it with an
InvalidParameterValueerror. - Many threads each long polling. Each waiting request is still a billed call when it ends empty. Run only as many pollers as you need.
- Assuming zero means long polling. A wait time of 0 is short polling.
Exercise
A consumer loop calls receive_message with no WaitTimeSeconds on a queue whose ReceiveMessageWaitTimeSeconds is 20. The queue is empty. Does each call return immediately, and why would the bill be lower than with a queue set to 0?
Show answer
No. The call uses the queue default, so it long polls and waits up to 20 seconds before returning an empty response. The consumer makes about three requests per minute instead of a continuous stream of immediate empty responses. Fewer billed requests means a lower cost.