Message deduplication is the technique of recognizing repeated deliveries of the same message and ensuring that the business effect of that message is applied only once. In the reliability of automation systems, it compensates for at-least-once delivery, the guarantee most queues, event buses and webhook senders actually provide, under which retries and redeliveries inevitably produce duplicates.
Deduplication does not prevent duplicates from being sent. It makes the receiving side tolerant of them by keeping a record of what it has already processed and by comparing each incoming message with that record before acting.
Why duplicate messages occur
Duplicate messages occur because distributed systems cannot distinguish a lost acknowledgment from a lost message. When a producer sends a message and the confirmation times out, the safe choice is to send again, even though the first attempt may already have succeeded.
The same logic applies on the consumer side. A worker that processes a message but crashes before acknowledging it causes the broker to redeliver after the visibility timeout, and a webhook provider that receives a slow response will usually retry the call with the same payload.
| Source | Mechanism | Typical setting |
|---|---|---|
| Producer retry | Send repeated after a timeout | API clients, event publishers |
| Consumer crash | Message processed but not acknowledged | Queue workers, stream consumers |
| Visibility timeout expiry | Slow processing exceeds lease time | Cloud queues such as Amazon SQS |
| Webhook redelivery | Sender retries on slow or failed response | Payment and SaaS integrations |
| Rebalancing or replay | Offsets reset after failover | Kafka consumer groups, workflow replay |
| User double submission | Same action triggered twice | Forms and button clicks |
How message deduplication works
Message deduplication works by assigning each logical message a stable deduplication key and storing the keys that have already been processed in a deduplication store. Before processing, the consumer checks the store; if the key is present, it skips the work and acknowledges the message.
The key is either an explicit message identifier set by the producer, such as an event ID or idempotency key, or a content hash computed from the payload. Explicit identifiers are preferred, because two legitimately different messages can share identical content, while a retried message may differ in metadata such as timestamps.
Correctness depends on atomicity between the check, the business effect and the record. If the key is written before the effect and the process then fails, the message is lost; if it is written after and the process fails in between, the effect repeats. The robust solution writes the key and the business change in the same transaction.
| Key strategy | Advantage | Risk |
|---|---|---|
| Producer-assigned ID | Exact and cheap to compare | Producer must reuse it on retries |
| Content hash | Works without producer changes | Collapses genuinely repeated events |
| Business key | Matches domain meaning, such as order number | Needs careful scope per operation |
| Composite key | Combines source, type and ID | More storage and design effort |
Deduplication windows and storage
Because storing every key forever is expensive, most implementations keep keys for a deduplication window, a period long enough to cover all realistic retries. Duplicates arriving after the window expires are not detected, so the window must exceed the longest retry schedule plus any replay horizon.
Built-in broker features illustrate the trade-off. Amazon SQS FIFO queues use a five-minute deduplication interval based on a deduplication ID or content hash, while Kafka idempotent producers remove duplicates caused by producer retries within a partition but do not protect against consumer-side reprocessing.
| Layer | Example | Coverage |
|---|---|---|
| Broker | FIFO queue deduplication ID | Short window, producer duplicates only |
| Producer protocol | Idempotent producer sequence numbers | Retries within one session |
| Consumer store | Processed-keys table or cache | Any duplicate within retention |
| Business operation | Unique constraint on order number | Permanent, domain-level protection |
Storage choice affects reliability. An in-memory cache is fast but loses keys on restart, a key-value store with time-to-live balances speed and durability, and a relational table with a unique constraint offers the strongest guarantee when it shares a transaction with the business data.
Message deduplication and related reliability patterns
Message deduplication is closely related to idempotency but not identical. Idempotency is a property of an operation that yields the same result when repeated, while deduplication is a mechanism that filters repeats so that non-idempotent operations, such as sending an email or charging a card, are executed once.
- Transactional outbox prevents lost messages on the producer side, but it still emits duplicates, so consumers need deduplication.
- Dead letter queue isolates messages that repeatedly fail, which is a different problem from successful messages delivered twice.
- Retry policy and exponential backoff determine how many duplicates appear and over what period, which sets the minimum window.
- Exactly-once processing claims in brokers usually mean at-least-once delivery combined with deduplication or transactions.
Pitfalls in practice
Most deduplication failures come from choosing the wrong key or separating the record from the effect. Teams that hash full payloads find that retries with new timestamps slip through, while teams that mark a message as processed in a cache before committing the database change lose messages after crashes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Customers charged twice | Key excludes retried requests | Use a producer-assigned idempotency key |
| Valid events dropped | Content hash too coarse | Include event ID or sequence number |
| Duplicates after restart | Keys held only in memory | Persist keys with a defined retention |
| Late duplicates processed | Window shorter than retry horizon | Extend window beyond maximum backoff |
| Lost messages after crash | Key saved before business commit | Commit key and effect atomically |
Concurrency is another trap. Two workers can receive the same message at nearly the same moment, both see no stored key and both proceed. A unique constraint or conditional insert resolves this race, because only one writer can succeed.
A non-technical view
Message deduplication is like a receptionist who keeps a list of reference numbers for parcels already signed for. If the courier brings the same parcel again by mistake, the receptionist checks the list and politely declines it instead of paying for it twice.
The list only needs to cover the period in which the courier might realistically return, which keeps it small but still effective.
A concrete example
An online store connects its payment provider to an order automation workflow through webhooks. During a database slowdown, the workflow takes more than ten seconds to answer, and the provider retries the same payment-succeeded event three times within a minute.
Without protection, the workflow would create three shipments and three invoices. The team stores each provider event ID in a processed-events table with a unique constraint, written in the same transaction as the order update, and keeps entries for seven days, well beyond the provider retry schedule. Repeated deliveries now return success immediately without side effects, and duplicate shipments drop to zero while no legitimate payments are lost.