Networks fail in the worst possible place: after the server has done the work but before the client hears back. The client can't tell whether the request succeeded, so it retries. If the operation is idempotent, the retry is harmless. If it isn't, the customer is charged twice, two orders ship or a ledger posts the same entry again.
Idempotency in HTTP and REST APIs
The HTTP specification (RFC 9110, section 9.2.2) calls a method idempotent when several identical requests have the same intended effect on the server as one. GET, HEAD, OPTIONS, PUT and DELETE are idempotent. POST is not, and PATCH isn't guaranteed to be.
PUT /users/42 {"email": "a@b.com"}sets the email. Sending it five times leaves the same email.DELETE /orders/9removes the order. The second call finds nothing to delete, but the state is the same.POST /payments {"amount": 500}creates a new payment each time. This is where duplicates come from.
Idempotent doesn't mean the response is identical. The first DELETE may return 200 and the second 404. What matters is the state of the system.
Idempotency keys
To make a POST safe to retry, the client sends a unique value with the request, usually in an Idempotency-Key header. Stripe popularised this pattern, and an IETF draft standard describes the same header.
The server's job:
- Before doing any work, look up the key in a table with a unique constraint on it.
- If the key is new, store it with a "processing" status, do the work and save the response.
- If the key exists, return the stored response instead of doing the work again.
- If the same key arrives with a different request body, reject it as an error.
Keys need an expiry. Stripe, for example, can remove keys once they are at least 24 hours old, so a key is a guard against retries, not a permanent duplicate check.
Example: a wallet top-up
A user taps "Add ₹500" on a weak mobile connection. The app generates a key (topup-7f3c…) and sends the request. The server credits the wallet, but the response is lost and the app retries with the same key. The server finds the key, sees the credit already exists and returns the original success response. The wallet shows ₹500, not ₹1,000.
The same idea applies inside a double-entry ledger: each posting carries a unique reference, so replaying a batch can't post the same entry twice.
Where else it matters
- Webhooks and queues. Most webhook senders and message queues deliver at least once, so consumers must handle the same event twice. Store processed event IDs and skip repeats.
- Data pipelines. A job that writes with
MERGEor upsert on a natural key can be re-run after a failure. A job that blindly appends can't. - Distributed transactions. Each step and each compensating step in a saga should be idempotent, because the coordinator may retry them.
Common mistakes
- Checking for the key and then inserting it in two separate steps. Two concurrent retries both pass the check. Use the database's unique constraint instead.
- Generating a new key on every retry, which defeats the purpose. Generate it once per user action.
- Making the payment call idempotent but not the side effects after it, such as the confirmation email or the stock update.
We design payment flows, ledgers and integrations with retries in mind. See how to design a payments ledger that reconciles or our product engineering work.