Sign in to keep reading
“Idempotency and Exactly-Once Semantics” is free with an account. Your progress and bookmarks follow you across devices.
Sign in to continueNEW: ML Mock & Coaching with a 20+ year experience coach. Save $20 on sessions for Premium.ML Mock & Coaching now available
NEW: ML Mock & Coaching with a 20+ year experience coach. Save $20 on sessions for Premium.ML Mock & Coaching now available
Prevent duplicate processing from retries using idempotency keys, deduplication tables, and database-level techniques like upserts and conditional updates.
“Idempotency and Exactly-Once Semantics” is free with an account. Your progress and bookmarks follow you across devices.
Sign in to continueIn distributed systems, things fail in the most inconvenient ways. Not clean failures where you know something went wrong -- partial failures where you have no idea what happened. A client sends a payment request, the server processes it successfully, but the response gets lost in the network. The client, seeing a timeout, does the sensible thing: retry. And now the customer has been charged twice.
This is not a hypothetical edge case. It happens constantly in production systems. Every time a mobile app hits a flaky cell connection, every time a load balancer times out, every time a TCP connection drops mid-response -- the client needs to retry. And without idempotency, every retry is a potential duplicate.
The fix is straightforward in concept: make your operations idempotent. An idempotent operation produces the same result whether you execute it once or ten times. GET requests are naturally idempotent -- fetching a page twice gives you the same page. But POST requests (create a payment, send an email, place an order) are not idempotent by default. You have to make them so.
The most common approach is the idempotency key pattern. The client generates a unique identifier (typically a UUID) and attaches it to every request. The server uses this key to detect retries and return the previously computed result instead of re-executing the operation.
Here is how it works step by step:
If the client retries with the same key, the server finds the cached result and returns it. The operation executes exactly once regardless of how many times the request is sent.
The dedup store entry should include the idempotency key, a hash of the request body (to detect misuse where different requests reuse the same key), the response body, the HTTP status code, and a creation timestamp. Set a TTL on entries -- 24 to 72 hours is typical -- so the store does not grow unbounded.
See it in practice: The Payment System question makes idempotency a first-class requirement -- every charge operation carries an idempotency key to prevent double billing when network timeouts trigger retries. The Webhook question requires the same pattern from the receiver's side: merchants must deduplicate incoming webhook notifications using the event ID. The Bookstore question uses idempotency keys to prevent duplicate order submissions during checkout.