Sub topic 11
Avoiding duplicate notifications
This chapter assumes you know how message queues work, can read short Python snippets, and have seen retries with backoff. Queues and retries make delivery reliable, but they also create duplicates.
Why duplicates happen
Most queues promise at-least-once delivery. A worker may send a push notification, then crash before it acknowledges the message. The queue redelivers the message, and the user gets the same alert twice. Retries add to this. A provider can accept a request but time out before replying, so your retry sends it again.
Exactly-once delivery is not realistic across a network and a third-party provider. The practical goal is at-least-once delivery plus idempotency. An operation is idempotent when running it several times has the same effect as running it once.
Building the idempotency key
The key must identify one intended send. Combine the event ID, the user ID, and the channel:
evt-1001:user-42:push
Including the channel matters. One event may legitimately go to push and email, and those are two separate sends. The event ID must come from the producer and stay the same across retries. If the ingestion API generates a fresh ID on every request, a producer that retries its own call creates duplicates you cannot detect.
Claiming a key
A worker claims the key before sending. If the claim fails, another attempt already owns the send.
class Deduplicator:
def __init__(self):
self._seen = set()
def claim(self, key):
"""Return True the first time a key is seen, False afterwards."""
if key in self._seen:
return False
self._seen.add(key)
return True
dedup = Deduplicator()
key = "evt-1001:user-42:push"
if dedup.claim(key):
print("send")
else:
print("skip duplicate")This version lives in one process's memory. In production, keep keys in a shared store such as Redis, and use an atomic set-if-absent operation (for example SET key value NX EX 86400). A separate check followed by a write is a race: two workers can both see "not seen" and both send. Set an expiry that outlasts your longest retry and redelivery window, so the store does not grow without limit.
Gotchas
- Claim before or after sending? Claiming first prevents duplicates, but a crash after the claim and before the send loses the notification. Claiming after the send risks a duplicate. Many teams claim first, mark the key as "sent" once the provider accepts it, and let a stale "in progress" claim expire so a later attempt can retry.
- Provider keys. Some providers accept their own idempotency key. Pass yours through when they do, since it covers the case where your worker cannot tell whether a timed-out request succeeded.
- Deliberate repeats. A user who triggers the same action twice may need two notifications. Give each real occurrence its own event ID.
Exercise
A worker uses a plain "check if key exists, then insert it" against a shared store. Two workers receive the same message at once. What can go wrong, and how do you fix it?
Show answer
Both workers can check before either inserts, both see the key as absent, and both send. Replace the two steps with one atomic set-if-absent operation (such as Redis SET with NX and an expiry), so only one worker succeeds in claiming the key.