0
0
AwsConceptBeginner · 3 min read

Visibility Timeout in SQS: What It Is and How It Works

In AWS SQS, visibility timeout is the time period during which a message is hidden from other consumers after being received by one consumer. It prevents multiple consumers from processing the same message simultaneously by temporarily making the message invisible until processing is complete or the timeout expires.
⚙️

How It Works

Imagine you are at a library checkout desk where only one person can handle a book at a time. When you pick a book to check out, the librarian temporarily hides it from others so no one else tries to grab the same book. In SQS, the visibility timeout works similarly for messages.

When a consumer receives a message from the queue, SQS hides that message from other consumers for the duration of the visibility timeout. This gives the consumer time to process and delete the message without interference. If the consumer finishes processing and deletes the message before the timeout ends, the message is removed permanently.

If the consumer fails to delete the message within the visibility timeout (for example, due to a crash or delay), the message becomes visible again in the queue. This allows another consumer to receive and process it, ensuring no message is lost.

💻

Example

This example shows how to receive a message from an SQS queue with a visibility timeout of 30 seconds using AWS SDK for Python (boto3). The message is processed and then deleted to prevent it from reappearing.

python
import boto3

sqs = boto3.client('sqs')
queue_url = 'https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue'

# Receive a message with visibility timeout of 30 seconds
response = sqs.receive_message(
    QueueUrl=queue_url,
    MaxNumberOfMessages=1,
    VisibilityTimeout=30,
    WaitTimeSeconds=10
)

messages = response.get('Messages', [])
if messages:
    message = messages[0]
    receipt_handle = message['ReceiptHandle']
    print('Received message:', message['Body'])

    # Process the message here

    # Delete the message after processing
    sqs.delete_message(QueueUrl=queue_url, ReceiptHandle=receipt_handle)
    print('Message deleted from queue')
else:
    print('No messages received')
Output
Received message: Hello from SQS! Message deleted from queue
🎯

When to Use

Use visibility timeout whenever you want to ensure that messages are processed only once at a time, avoiding duplicate work. It is especially useful in distributed systems where multiple workers consume messages concurrently.

For example, if you have a task queue for processing orders, setting a visibility timeout prevents two workers from processing the same order simultaneously. If a worker crashes or takes too long, the message becomes visible again for another worker to retry.

Adjust the visibility timeout based on how long your processing usually takes. Setting it too short may cause duplicate processing, while too long may delay retries for failed tasks.

Key Points

  • Visibility timeout hides a message after it is received to prevent other consumers from seeing it.
  • If the message is deleted before the timeout ends, it is removed permanently.
  • If not deleted, the message becomes visible again for reprocessing.
  • Set the timeout based on expected processing time to balance between duplicate processing and retry delay.
  • Helps build reliable, fault-tolerant message processing systems.

Key Takeaways

Visibility timeout temporarily hides a message after receipt to avoid duplicate processing.
If processing finishes successfully, delete the message before the timeout expires.
If processing fails or times out, the message becomes visible again for retry.
Set visibility timeout duration based on your task's typical processing time.
It ensures reliable and safe message handling in distributed systems.