You’re waiting for a lift. You press the button. Nothing seems to happen, so you press it again, and again. Does the building send three lifts? No. The first press called a lift; every press after that changed nothing.
Now imagine paying for a train ticket in an app. You tap Pay, the screen spins, the network drops, and you tap Pay again. Did you pay twice? With a well-designed system, no. The reason is a property called idempotency.
This guide explains what idempotency means, why distributed systems need it everywhere, and the simple trick (the idempotency key) that makes payments safe to retry.
What is idempotency?
An operation is idempotent if doing it once or many times has the same effect. After the first time, repeats change nothing.
Everyday examples:
| Idempotent (repeat is harmless) | Not idempotent (repeat changes things) |
|---|---|
| Pressing a lift call button | Pressing “buy” in a vending machine |
| Setting the AC to 24 °C | Pressing “temperature +1” |
| “Set my phone number to 98xxxxxx01” | “Add ₹500 to my wallet” |
| Deleting email #42 | “Delete my oldest email” |
Notice the pattern. “Set to a value” is idempotent; “change by an amount” is not. Deleting one specific thing is idempotent; deleting “whatever is oldest right now” is not, because a second call deletes something else.
Why systems need it: retries are everywhere
On a single computer, a function either runs or it doesn’t. Across a network, there’s a third, nasty outcome: you don’t know.
Say your app sends “charge ₹500” to a payment server and the reply never comes. Three things could have happened:
- The request never reached the server.
- The server charged the card, but the reply got lost on the way back.
- The server is still working on it.
From the app’s side, all three look the same: a timeout. The safe-looking move is to retry. But in case 2, a retry charges the customer twice.
Retries happen at many layers without anyone deciding them: the user taps again, the app retries, a load balancer retries, a message queue redelivers a message. Most queues promise at-least-once delivery, which means duplicates are normal, not rare. (Our guide to message delivery semantics goes deeper.)
So the rule is simple: if something can be retried, it must be safe to receive twice.
Idempotent HTTP methods
The HTTP standard says some methods should be idempotent:
| Method | Idempotent? | Why |
|---|---|---|
GET |
Yes | Reading doesn’t change anything |
PUT |
Yes | “Replace this thing with exactly this” gives the same result each time |
DELETE |
Yes | Once deleted, deleting again leaves it deleted (the reply may differ, like 404, but the state is the same) |
POST |
No | Usually means “create a new one”, so each call can create another |
PATCH |
Not guaranteed | Depends on the change: “set name to Ravi” is, “add 1 to count” isn’t |
These are promises your API should keep, not something HTTP enforces. A badly written GET that changes data breaks the promise. POST is the tricky one, and it’s exactly what payments and orders use. That’s where idempotency keys come in.
The idempotency key
An idempotency key is a unique ID the client creates once per action and sends with every attempt of that action. The server remembers the keys it has already handled.
Here’s how it works:
- When you tap Pay, the app generates a random ID, say
abc-123, and keeps it for this payment. - It sends
POST /paymentswith the headerIdempotency-Key: abc-123. - The server checks: have I seen
abc-123? No. It charges the card, then saves the key together with the result. - The reply is lost. The app retries with the same key.
- The server sees
abc-123already done and simply returns the saved result. No second charge.
Payment providers such as Stripe offer exactly this header for their APIs. The same idea works inside your own systems: give every message or command a unique ID, and have the receiver skip IDs it has already processed.
Other ways to make operations idempotent
Keys aren’t the only tool:
- Use “set” instead of “add”.
status = 'shipped'is safe to repeat;stock = stock - 1isn’t. - Use natural unique IDs. If each order has one ID, a database unique constraint on that ID makes a second insert fail harmlessly.
- Check the current state first. “Ship the order only if it’s still in
paidstate.” A repeat finds it already shipped and does nothing. - Version numbers. “Update this row only if it’s still version 7.” A repeat sees version 8 and stops.
Common misconceptions
“Idempotent means the response is always the same.” It’s about the effect on the system. A second DELETE might return 404 instead of 200, and that’s fine, as long as nothing else changed.
“Exactly-once delivery means I don’t need idempotency.” In practice, “exactly-once” in messaging systems usually means at-least-once delivery plus de-duplication, which is idempotency. Build your receivers to handle duplicates anyway.
“Only payments need this.” Anything that sends an email, books a seat, adds points or creates a record can do damage when repeated.
Key takeaways
- Idempotent means doing it many times has the same effect as doing it once.
- Networks make retries unavoidable, so duplicates will happen.
GET,PUTandDELETEshould be idempotent;POSTusually isn’t.- An idempotency key, made once per action and saved with the result, makes
POSTsafe to retry. - Prefer “set” over “add”, and use unique IDs and state checks.
Frequently asked questions
What does idempotent mean in simple words?
Idempotent means you can do something again and again and the result stays the same as doing it once. Pressing a lift button repeatedly still calls one lift.
Why is idempotency important in APIs?
Because networks are unreliable, clients retry requests when they don’t get a reply. Idempotency makes sure a retried request doesn’t create a second order or a second payment.
Is POST idempotent?
Not by default. POST usually creates something new, so repeating it can create duplicates. You can make a POST endpoint idempotent by accepting an idempotency key and remembering which keys you’ve already handled.
Is PUT idempotent?
Yes, PUT should be idempotent. It means “replace this resource with exactly this data”, so sending the same PUT twice leaves the same result.
What is an idempotency key?
It’s a unique ID that the client creates for one action and sends with every attempt. The server stores the keys it has processed and returns the saved result for a repeat, instead of doing the work again.
Comments