Best strategies for removing duplicates from SQS queue

We have a FIFO queue that receives messages from a few different sources. The same message may enter the queue over a few hours. There is a separate background job that processes these messages although it may be 4-5 hours before this happens and we are trying to prevent this job from reprocessing the messages which are duplicates. Once we process, we could always delete any duplicates but that requires that we go through every message in the queue to find duplicates which seems highly inefficient. We could also prevent duplicates from entering the queue.

Thoughts on how we could attack this problem?

FIFO queues have a configuration option to do this automatically. It effectively does a hash of the message contents and uses that to detect dupes.

Exactly once messaging is not recommended cuz what happens if that single instance of a message is lost or malformed or whatever. at least once is more common.

If your managed dressed-up-Kafka service supports de-duping it’s just looking at local records of seen items and deciding to ignore them. Alternatively you keep a record yourself and detect dupes that way. Point is, efficiency here comes at the cost of reliability/consistency.

That’s just within 5 minute dedupe window though, but in OPs case they can get duplicates for a few hours, so doesn’t quite solve it for them.

Maybe OP can look into that, I don’t quite see why you would get duplicates over several hours, that sounds very strange.

You could write to some resilient data store that the message was already processed and in your processing logic check to see if that processed marker is present before doing the work.

Maybe OP should leverage a cache, either Redis or DynamoDB, and expire them after a specific number of minutes/hours?

It’s way easier to solve this on the consumer side by just making your messages idempotent. Have a uuid or something and just ignore if it’s already been processed. Enforcing uniqueness on the queue itself won’t really work — what happens if the duplicate is published after the original message was already consumed.

use redis. set an expiring key unique to that message. when the message executes, check for existence of the key. if exists, stop processing and drop that message. if it doesn’t exist process it and set the key in redis with an expiration of like 24 hours or whatever you want the max window to be.

Wasnt there an option for increasing the delay? The value was used to prevent duplicates from entering the queue when an identical key was present in queue already

https://segment.com/blog/exactly-once-delivery/

Look up idempotent consumer. That is what you want. There are plenty of ways to solve the problem.

+1. We have customers using the Idempotency feature for dedup on the consumer side — while we don’t call out in the docs yet, the second decorator works on any compute platform even locally.

PS: Available in Python, TypeScript, Java, and .NET.

https://docs.powertools.aws.dev/lambda/python/latest/utilities/idempotency/

You should use content based deduplication

Or use redis and cache entries with a TTL of however long you’d prefer to avoid processing dupes. When dequeuing check existence in redis. If not exists cache and process else skip

I used to work on a team at AWS, and this was actually a big sticking point. We promised at least once message delivering, and dupes affected less than 0.001% of all our communications, however it was by far the most complained about aspect of our service. A lot of effort went to finding effective ways to avoid duplications while still achieving 100% availability

Fix the problem, not the symptom. The best way to not have duplicates in your queue is to not send duplicates in the first place.

great post. I wonder if folks would do anything different nowadays (it’s 7 years old now)

Idempotency is the correct approach and it is about how the message is designed, but I wouldn’t call it a form of deduplication.

An idempotent system processes duplicate messages normally, while designing the content of the message to ensure that the outcome of processing any number of duplicates does not make a difference.

For example, the message should never instruct “add [incremental value] to [variable]”, but rather must be “set [variable] to [final value]”. In the latter, the outcome is the same no matter how many duplicate messages are processed.

The correct way to indicate the need to ignore a message is using time*. The target record should store a timestamp set to the most recent message timestamp value seen. Whenever a message with an older timestamp arrives, that message should be ignored.

* Or rarely, if appropriate, some form of versioning value, which has to be comparable in a trinary manner (< , > , ==) rather than binary (==, !=) like UUID. This is critical for avoiding the scaling issues of having to store UUIDs of processed messages.

Source: worked at an Amazon subsidiary building SNS/SQS-based pipeline services which got frequent duplicate messages from Alexa services.

PS- Enforcing uniqueness on the client side suffers from even more issues than on the queue, because of the need to synchronize the checking of duplicates across multiple clients, adding even more complexity to the design.

We did this with redis for certain AWS logs … uid gets cached and checked a d dropped if it matches a hit

I mean sure I can see how that can kind of work, but I’d call that a hack of last resort, if the message contents are set in stone and allow for no other possible solution.

Off the top of my head the weaknesses include:

  • It adds an otherwise unnecessary dependency/point-of-failure which incurs both latency and cost
  • It forces making an undesired choice between cost and latency via Redis/DynamoDB (and by undesired I mean it doesn’t eliminate either, it merely favors one)
  • The cost will grow as the system scales under either choice

See my other comment for the “correct” design approach.

* Also- the system can get messed up if Lamda is triggered when a DLQ is rerun after the TTL expires. In short, it’s bad architecture.