Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to guarantee that Amazon SQS will receive a message only once?

Tags:

amazon-sqs

I'm using an Amazon SQS queue to send notifications to an external system.

If the HTTP request fails when using SQS' SendMessage, I don't know whether the message has been queued or not. My default policy would be to retry posting the message to the queue, but there's a risk to post the message twice, which might not be acceptable depending on the use case.

Is there a way to have SQS refuse the message if there is a duplicate on the message body (or some kind of message metadata, such as a unique ID we could provide) so that we could retry until the message is accepted, and be confident that there won't be a duplicate if the first request had been already queued, but the response had been lost?

like image 887
BenMorel Avatar asked Sep 09 '13 10:09

BenMorel


People also ask

Does SQS guarantee only once delivery?

Q: Does Amazon SQS guarantee delivery of messages? Standard queues provide at-least-once delivery, which means that each message is delivered at least once. FIFO queues provide exactly-once processing, which means that each message is delivered once and remains available until a consumer processes it and deletes it.

Which can be used to avoid duplicate messages in the SQS service?

FIFO queues help you avoid sending duplicates to a queue. If you retry the SendMessage action within the 5-minute deduplication interval, Amazon SQS doesn't introduce any duplicates into the queue. To configure deduplication, you must do one of the following: Enable content-based deduplication.

How can you ensure that your SQS messages arrive in the correct order?

To ensure that Amazon SQS maintains the order in which messages are sent and received, each producer should send all of its messages using a unique message group ID. Only one message group ID is affected by SQS FIFO queue logic.

Which of the 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.


2 Answers

No, there's no such mechanism in SQS. Going further, it is also possible that a message will be delivered twice or more (at-least-once delivery semantics). So even if such a mechanism existed, you wouldn't be able to guarantee that the message isn't delivered multiple times.

See: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/DistributedQueues.html

For exactly-once deliveries, you need some form of transactions (and HTTP isn't a transactional protocol) both on the sending and receiving end.

like image 52
adamw Avatar answered Sep 30 '22 14:09

adamw


AFAIK, right now SQS does support what was asked!

Please see the "What's new" post entitled Amazon SQS Introduces FIFO Queues with Exactly-Once Processing and Lower Prices for Standard Queues

According to SQS FAQ:

FIFO queues provide exactly-once processing, which means that each message is delivered once and remains available until a consumer processes it and deletes it. Duplicates are not introduced into the queue.

There's also an AWS Blog post with a bit more insight on the subject:

These queues are designed to guarantee that messages are processed exactly once, in the order that they are sent, and without duplicates.

......

Exactly-once processing applies to both single-consumer and multiple-consumer scenarios. If you use FIFO queues in a multiple-consumer environment, you can configure your queue to make messages visible to other consumers only after the current message has been deleted or the visibility timeout expires. In this scenario, at most one consumer will actively process messages; the other consumers will be waiting until the first consumer finishes or fails.

Duplicate messages can sometimes occur when a networking issue outside of SQS prevents the message sender from learning the status of an action and causes the sender to retry the call. FIFO queues use multiple strategies to detect and eliminate duplicate messages. In addition to content-based deduplication, you can include a MessageDeduplicationId when you call SendMessage for a FIFO queue. The ID can be up to 128 characters long, and, if present, takes higher precedence than content-based deduplication.

like image 27
gvasquez Avatar answered Sep 30 '22 13:09

gvasquez