We can set Retention time.
After this time, the message is automatically deleted.
It's very convenient.
So,
Why do we delete the SQS message using ReceiptHandle? What reason is it?
We can set Retention time.
After this time, the message is automatically deleted.
It's very convenient.
So,
Why do we delete the SQS message using ReceiptHandle? What reason is it?
Because a message should generally only be processed once (excluding retries due to errors of course). The flow is: a service receives the message from the queue, processes it and then deletes it.
If it did not delete the message it would receive the exact same message during the next ReceiveMessage
(after the VisibilityTimeout
expires) call and work on the same message again, and again, and again, and again...
If you want a message to be processed multiple times, e.g. once by different consumers, you should use SNS and subscribe multiple queues to that SNS topic, one for each consumer and then each consumer consumes their respective queue and deletes a message from their queue if they are finished processing it.
The general flow of messages in Amazon SQS is:
SendMessage()
ReceiveMessage()
to request up to 10 messages from the queue:
DeleteMessage()
, passing the Receipt Handle. This will delete the message from the queue.ChangeMessageVisibility()
to extend the invisibility period to indicate that a message is still being processed.To answer your question, the Retention Period is used to remove messages that have not been successfully processed. This is useful if messages are only worth processing within a given period of time. For example, a request to trade on the Stock Market might only be valid for 5 minutes, after which the message should be removed if it has not been processed.
If a worker does not delete a message, it will reappear on the queue and will be processed again by another worker.