Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQS delivering a message only once

Tags:

Let's say I have 100 threads reading from the same sqs -- is it guaranteed that each message is going to be delivered at-most once? Is it at all possible to deliver the same message more than once? I can't find any clear documentation on this issue. s

like image 640
Vladimir Avatar asked Jan 23 '15 14:01

Vladimir


People also ask

Can SQS message be received twice?

First of all, SQS by design guarantees at least one delivery. It means that the same message can be delivered several times. For exactly-once delivery, SQS has FIFO Queues with Exactly-Once Processing.

Why is SQS at least once?

At-least-once delivery Amazon SQS stores copies of your messages on multiple servers for redundancy and high availability. On rare occasions, one of the servers that stores a copy of a message might be unavailable when you receive or delete a message.

Which type of queue ensures the delivery of a message exactly once?

FIFO queues are designed to ensure that the order in which messages are sent and received is strictly preserved and that each message is processed exactly once.

How many times will SQS Retry?

You can have maximum of 2 retries with lambda for asynchronous invocations and up to 1000 SQS retries.


2 Answers

Update Nov 17, 2016: FIFO queueing with guaranteed-once delivery was just released today (almost 2 years after this answer was originally posted) and SQS now supports exactly-once delivery. Here are the details:

  • You need to create a FIFO Queue.
  • Deduplication checks for MessageDeduplicationId as an attribute of the queue message, and prevents duplicate messages from being both sent and received in the 5-minute deduplication interval following either action by checking the MessageDeduplicationId.
  • If Content-based deduplication is explicitly enabled on the FIFO queue, MessageDeduplicationId will be automatically generated using a SHA-256 hash of the message body (content only, not attributes).
  • If Content-based deduplication is not enabled, you must explicitly set your own arbitrary value for MessageDeduplicationId on send. Otherwise SendMessage will fail with an error.

Content-based deduplication can be enabled when creating a queue or updating the queue' attributes.

More details on MessageDeduplicationId in SendMessage and ReceiveMessage documentation.


After Nov 17, 2016, this still applies to standard (not-FIFO) queues:

Due to the distributed nature of standard (not-FIFO) queues in SQS, the guarantee is instead "at least" once.

From the FAQ:

Q: How many times will I receive each message?

Amazon SQS is engineered to provide “at least once” delivery of all messages in its queues. Although most of the time each message will be delivered to your application exactly once, you should design your system so that processing a message more than once does not create any errors or inconsistencies.

More information on at least once delivery:

Amazon SQS stores copies of your messages on multiple servers for redundancy and high availability. On rare occasions, one of the servers storing a copy of a message might be unavailable when you receive or delete the message. If that occurs, the copy of the message will not be deleted on that unavailable server, and you might get that message copy again when you receive messages. Because of this, you must design your application to be idempotent (i.e., it must not be adversely affected if it processes the same message more than once).

If you really need to guarantee "at most once" processing in your application, you may want your application to check the unique identifiers of SQS messages and not process message IDs that you have processed before, or are currently processing.

like image 93
Anthony Neace Avatar answered Sep 26 '22 05:09

Anthony Neace


You need to use FIFO queues but still you need to generate a number that guarantees no duplications of your messages, you can set MessageDeduplicationId with your generated number in the request before you send it to SQS.

Easier, You can turn on Content-Based-Deduplication for the FIFO queue and let AWS itself manage duplication for you by creating a hash based on the content of the sent message to avoid its deduplication.

  • From the console, Go to SQS service, choose the queue and from Actions choose Configure queue

enter image description here

  • Switch on Content-Based Deduplication

enter image description here

like image 31
Muhammad Soliman Avatar answered Sep 23 '22 05:09

Muhammad Soliman