Sub topic 4
Visibility timeout: how SQS hides messages while you work
This chapter assumes you can send, receive, and delete SQS messages with boto3, as covered in the previous chapter.
What the visibility timeout does
SQS does not remove a message when a consumer receives it. The message stays in the queue, but SQS hides it from other consumers for a period called the visibility timeout. If the consumer deletes the message before the period ends, the work is done. If it does not, the message becomes visible again and another receive call can return it.
This is how SQS recovers from crashed consumers. The default is 30 seconds, and the range is 0 seconds to 12 hours.
Setting the timeout
You can set the timeout in three places:
- Queue default: the
VisibilityTimeoutattribute, as in the 60-second queue created earlier. - Per receive call: the
VisibilityTimeoutparameter onreceive_message, which overrides the queue default for the messages that call returns. - Per message, while working:
change_message_visibility, which resets the timer for one message.
The timeout should be longer than your typical processing time, with some margin. A common starting point is about six times the average processing time. AWS recommends a function timeout no longer than the queue's visibility timeout when Lambda consumes the queue.
Extending the timeout during long work
Some jobs take longer than expected. Instead of setting a very long queue default, extend the timeout for the one message that needs it.
import json
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="orders")["QueueUrl"]
resp = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=1,
WaitTimeSeconds=20,
VisibilityTimeout=30, # hide the message for 30 s at first
)
for msg in resp.get("Messages", []):
order = json.loads(msg["Body"])
print("starting", order)
# The job needs more time: hide the message for 120 s from now
sqs.change_message_visibility(
QueueUrl=queue_url,
ReceiptHandle=msg["ReceiptHandle"],
VisibilityTimeout=120,
)
# ... long-running work here ...
sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=msg["ReceiptHandle"])The new value counts from the moment of the call, not from the original receive. The total timeout for a single message cannot exceed 12 hours.
Common errors
- Timeout too short. The message reappears while the first consumer is still working. A second consumer processes it, and you get duplicates. Raise the timeout or extend it during processing.
- Timeout too long. After a crash, the message stays hidden for the full period, which delays the retry.
- Stale receipt handle. Each receive returns a new
ReceiptHandle. If the message was received again, deleting with the old handle can fail or have no effect. Always use the handle from the most recent receive. - Forgetting the delete. The message returns after every timeout, and its receive count rises until it reaches the
maxReceiveCountof a redrive policy.
Exercise
A consumer usually takes 20 seconds per message, but occasionally takes 3 minutes. The queue's visibility timeout is 30 seconds. What problem occurs on the slow messages, and how can you fix it without raising the queue default?
Show answer
After 30 seconds the message becomes visible again, so another consumer can receive and process it while the first is still working, which produces duplicates. Call changemessagevisibility with a longer value (for example 240 seconds) on the slow message before the first timeout expires, or pass a larger VisibilityTimeout on the receive call for that consumer.