6

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

8

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.

adamw
  • 8,038
  • 4
  • 28
  • 32
  • Here is more [suggested reading on why complete deduplication is not possible](http://stackoverflow.com/a/38290017/836214) – Krease Jul 10 '16 at 17:33
4

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.

Community
  • 1
  • 1
gvasquez
  • 1,919
  • 5
  • 27
  • 41
  • @ochi ok, will do so tomorrow, when back to my computer – gvasquez Mar 01 '18 at 00:06
  • 1
    This post doesn't look like an attempt to answer this question. Every post here is expected to be an explicit attempt to *answer* this question; if you have a critique or need a clarification of the question or another answer, you can [post a comment](//stackoverflow.com/help/privileges/comment) (like this one) directly below it. Please remove this answer and create either a comment or a new question. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Mogsdad Mar 01 '18 at 03:34
  • @Mogsdad I've just edited the answer to fully contain the specific part of the link I had previously provided and, also included other excerpts and links to improve the answer. Is it acceptable now? – gvasquez Mar 01 '18 at 11:48
  • @ochi is it better now? – gvasquez Mar 01 '18 at 11:49
  • 2
    Thank you for editing the question. It it self-contained now and it has the bits needed for a full answer. – blurfus Mar 01 '18 at 16:06