3
votes

I am taking an AWS course and trying to understand how the AWS SQS (Simple Queue Service) works. I am a little bit confused by the visibility timeout. I know that visibility timeout is the duration for a message to be processed within.

This is the model

enter image description here

As you can see in the diagram, the consumer will pull the message or the messages and process the message or the messages within the visibility timeout. If the message or the messages are not processed within the visibility timeout, the consumer will not receive or process the message or the messages and the messages will remain in the queue.

The followings are my confusions:

  1. Is the visibility timeout valid for all the messages together in one poll? Or the timeout is applied to each message separately? For example, the consumer polls 5 messages at a time. If 2 of them are not processed within the timeout, 2 of them are sent back to the queue and the other three are deleted. Or all the 5 messages are processed within a timeout and if any of them failed, all of them get sent back to the queue.

  2. This is my second confusion. How is the visibility timeout valid? Is that for the time the consumer starts polling messages and receive the messages? Or is that valid for the time the consumer starts polling messages and finish processing messages on the consumer end (for example, saving messages into the database)?

1

1 Answers

7
votes

The visibility timeout begins when you receive the message. If you receive multiple messages in a batch, then the visibility timeout for all of them begins at that time.

enter image description here

To tell SQS that you have completely processed an SQS message, you delete the message from SQS using DeleteMessage. If you finish processing just 3 of 5 messages, then you delete just those 3 messages from SQS.

In-flight messages become visible again to other consumers if you do not delete them from SQS before the visibility timeout expires, or you explicitly make them visible again by setting their visibility timeout to zero (which is effectively you telling SQS "I am not going to process this message, let someone else"). You can also increase the visibility timeout dynamically, once you are processing an in-flight message.