Home > other >  Why do we delete the SQS message in queue?
Why do we delete the SQS message in queue?

Time:02-18

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?

CodePudding user response:

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.

CodePudding user response:

The general flow of messages in Amazon SQS is:

  • Something sends a message to the queue using SendMessage()
  • The message sits in the queue. If its retention period has passed, the message 'disappears' from the queue.
  • A worker calls ReceiveMessage() to request up to 10 messages from the queue:
    • The messages are provided to the worker and the messages in the queue become 'invisible' for a period
    • The worker processes the message(s). When it has fully-processed a message, it calls DeleteMessage(), passing the Receipt Handle. This will delete the message from the queue.
    • If the invisibility period of a message expires, then the message 'reappear' on the queue. The worker can use 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.

  • Related