Visibility Timeout, Polling & Retention
How a message moves through a queue, and why it doesn't get processed twice at once.
1 min read
A message's life
- Send: the producer calls
SendMessage. - Receive: a consumer calls
ReceiveMessage. The message becomes invisible to other consumers for the visibility timeout (30 seconds by default). - Delete: after successful processing, the consumer calls
DeleteMessage. Done. - Didn't delete in time? The message becomes visible again and another consumer can retry it.
Watch out
Set the visibility timeout longer than your processing time. If processing takes 60 seconds but the timeout is 30, another consumer will pick up the same message while the first is still working on it.
Other key settings
| Setting | Meaning |
|---|---|
| Retention period | How long unprocessed messages are kept: 4 days by default, up to 14 days |
Long polling (WaitTimeSeconds up to 20) | ReceiveMessage waits for messages instead of returning empty right away, which means fewer empty, billed calls |
| Delay queues | Hide new messages for up to 15 minutes before they can be received |
| Message size | Messages are limited in size, so put large data in S3 and send its key |
- 1SendMessage
Message in queue
- 2ReceiveMessage
Hidden for visibility timeout
- 3Process
Your code does the work
- 4DeleteMessage
Gone for good
Key takeaways
- Received messages are hidden for the visibility timeout, then reappear if not deleted.
- Always delete messages after successful processing.
- Use long polling, and store large payloads in S3.
Check your understanding
Pick an answer to see the explanation.
1. Processing takes ~2 minutes, but messages are being handled twice. What's the likely cause?