Idempotency and Deduplication
A client sends a request to charge a card. The network drops the response. Did the charge happen? The client has no way to know. If it gives up, a customer may have paid for an order that the system thinks failed. If it retries, the customer may be charged twice.
This situation isn't rare or exotic. It's the normal state of any system that talks over a network, and it shows up everywhere: mobile apps on flaky connections, load balancers retrying, message queues redelivering, webhooks being resent, stream processors replaying after a crash. Because the only safe way to make sure something happens is to retry until it's acknowledged, duplicates are unavoidable. The question is whether they're harmless.
An operation is idempotent if doing it twice has the same effect as doing it once. Making the important operations in a design idempotent is what lets every other part of the system retry freely, and it's one of the most frequently probed topics in interviews about payments, orders, bookings, notifications and anything built on queues.
We'll build this up in three steps:
- Where duplicates come from, and which operations are already safe to repeat.
- Idempotency keys: how an API turns an unsafe operation like "charge this card" into a safe one, including the failure cases.
- Deduplication in messaging and streaming, what "exactly once" really means, and deduplicating data itself.
Where Duplicates Come From
The root cause is always the same: a sender can't tell the difference between "my request was lost" and "the reply was lost." In both cases it sees a timeout. Some common sources:
- Client retries. A mobile app retries after a timeout. A user double-clicks "Pay" or refreshes the page during checkout.
- Infrastructure retries. Load balancers, API gateways, service meshes and SDKs often retry failed or timed-out requests automatically.
- Message redelivery. SQS makes a message visible again if the consumer doesn't delete it before the visibility timeout; Kafka consumers re-read messages after a crash if their offsets weren't committed; RabbitMQ redelivers unacknowledged messages.
- Producer retries. A service publishes an event, the broker stores it, the acknowledgement is lost, and the producer sends it again.
- Webhooks. Providers like payment processors resend events until your endpoint returns
2xx, and they state plainly that you may receive the same event more than once (see /system-design/webhooks). - Replays. A stream processor restoring from a checkpoint, a CDC connector restarting from its last saved position, or an operator replaying a topic to fix a bug.
- Workflow retries. A saga orchestrator retrying a step that timed out (see /system-design/transactions).
That last group matters: even if your own code never retries, the systems around it will.