Understand what idempotency is, why systems repeat operations all the time and the two ways to make a repetition safe, with everyday examples.


Ana cancels an order in the app and asks for a refund. The screen keeps loading and then shows an error. She taps “Try again”. Another error, another attempt.
Days later, her card statement shows three $100 refunds for a $100 purchase.
None of the three attempts was wrong. All three asked for exactly the same thing: cancel that order and return that money. It was the system that treated each repetition as a new refund.
The first idea is usually to disable the button while the screen loads. That prevents a few taps, but it doesn't answer the question that matters: how do you let Ana try again without authorizing another refund?
Besides, not every repetition comes from the button. Apps, queues, webhooks and scheduled jobs repeat operations all the time: that's how a system reacts to a response that never arrived or to a machine that went down in the middle of the work. That's where idempotency comes in, the property that makes this repetition safe.
TL;DR
An operation is idempotent when running it several times has the same effect as running it once. Repetitions are normal: whoever gets no response doesn't know whether the operation happened, and almost always tries again.
There are two ways to make repetition safe: by nature, by describing a final state (“the order is cancelled”), or by identity, by storing an identifier (“refund 9f3c”) together with the effect. Idempotency protects the effect, not the order of operations or the load, and it only holds while the identity is remembered.
An operation is idempotent when running it several times has the same effect as running it once. Think of an elevator button: pressing it five times doesn't call five elevators. The first press registers the call, and the others change nothing.
The name comes from mathematics, where a function is idempotent when applying it to its own result changes nothing further, which is written f(f(x)) = f(x).
Rounding works this way: 2.7 becomes 3, and rounding 3 still gives 3.
Adding 10 doesn't: 0 becomes 10, and 10 becomes 20, because each repetition changes the result again.
In software the same reasoning applies, with the operation in place of the function and the effect in place of the result.
The simplest way to tell whether an operation is idempotent is to repeat it and compare. Imagine three actions on Ana's order that, for some reason, reach the system three times:
| Action | After 1 run | After 3 runs | Idempotent? |
|---|---|---|---|
| Mark the order as shipped | shipped | shipped | Yes |
| Apply a $10 discount to a $100 order | $90 | $70 | No |
| Cancel the order with a refund | cancelled, 1 refund | cancelled, 3 refunds | No |
The third case is deceptive: the status stays the same, but the refund repeats. That's why the right question isn't “is the result on the screen the same?” but “did any effect happen again?”
The money returned, the stock put back and the email that reaches Ana are effects; the status on display is only one part of them.
Inside, a lot can run again: validations, logs and metrics run on every attempt without trouble. What can't repeat is the effect someone notices. And you can't call an effect a log: a financial entry doesn't become “just a record” because it was written to a table called history.
Idempotency doesn't require the same response, either. Cancelling an order that's already cancelled may answer “already cancelled” instead of “cancelled successfully”. The response changed, not the effect.
Whoever sends an operation over the network and gets no response doesn't know what happened. In Ana's cancellation, the same error can hide three very different situations:
| What went wrong | What happened in the system | What the app sees |
|---|---|---|
| The cancellation request got lost on the way there | Nothing: the cancellation never arrived | An error |
| The system went down in the middle of the cancellation | Order cancelled, money not yet returned | An error |
| The response got lost on the way back | Order cancelled and refund issued | An error |
Nothing, half or everything: for the app, all three end the same way, with an error or a timeout. The observation is the same; the reality isn't.
Faced with this doubt, there are only two ways out. Give up, and risk Ana never getting her money back. Or try again, and risk her getting it twice. Checking first whether the cancellation happened helps, but it doesn't remove the choice: the check goes through the same network and can go unanswered just the same.
Nearly every system chooses to try again, because a repetition can be recognized and neutralized by whoever receives it, and an operation that never arrived can't: on the other side, nobody knows it existed. This new attempt is the retry, and it doesn't only come from whoever is looking at the screen:
In queues, this same choice has a name: “at-least-once” delivery. Between losing a message and delivering it again, the queue delivers it again. It isn't a configuration defect, it's the normal way of operating, and it's the rest of the system that has to handle the repetition.
And handling it doesn't mean guessing. With idempotency, the same new attempt works for all three rows of the table: in the first, it does the cancellation; in the second, it finishes what was missing; in the third, it only finds out that everything was already done. This holds even when it arrives while the first attempt is still running.
Idempotency doesn't reveal what happened. It guarantees that trying again, to find out or to finish, doesn't produce another refund.
There are two ways to make an operation safe to repeat. The first changes how the operation is written; the second changes what the system remembers.
Cancelling Ana's order touches three things: the order status, the stock of product 42, which gets back the 2 reserved units, and the money. The first two already show the difference:
order.status = CANCELLED, repeated three times, leaves the order cancelled, just like the first time.stock = stock + 2, repeated three times, returns 6 units from a reservation of 2.The first write says how things should end up; the second says what to do starting from what exists. Repeating “leave it like this” changes nothing, and repeating “change this” changes it again. The same contrast shows up in the shopping cart (“add one unit” versus “set the quantity to 3”) and outside the application: CREATE TABLE IF NOT EXISTS can run on every install, and tools like Terraform and Kubernetes take the desired state and apply only the difference.
This kind of idempotency has a price: whoever writes needs to know the final state. If the stock was at 10, replacing the addition with “set the stock to 12” is only correct if nobody touched it between the read and the write. If another sale took a unit in the meantime, the write erases that sale.
The way out is the conditional write, known in the jargon as optimistic concurrency control: write the new value only if the old one is still there.
UPDATE stock SET quantity = 12 WHERE product_id = 42 AND quantity = 10;Repeating is still safe: if the first attempt already wrote 12, the condition quantity = 10 doesn't match on the second, and no row changes.
But a failed condition doesn't say who changed the stock. It may have been the cancellation's own earlier attempt or another sale. The conditional write avoids erasing other people's work, but it doesn't recognize a repeated intention. That's what the second form is for.
The refund doesn't fit the first mold. Returning money is a new transfer every time, not a state that gets reaffirmed. The question changes: is this attempt asking for a new refund, or trying to finish what has already started? To answer, the system gives the intention an identity and remembers it:
In Ana's case, the refund gets an identifier when she taps “Cancel”. The second and third attempts arrive with that same identifier, the system recognizes a refund it has already made, and the card receives only one.
The identity belongs to the intention, not to the content. If, instead of cancelling, Ana returned the 2 units on different days, there would be two $50 refunds for the same order: same content, different intentions, each with its own identifier.
Step 3 is the most delicate. Storing the identifier first and the effect afterwards, in separate operations, leaves a window: if the system goes down in between, you're left with an identifier without an effect. In the reverse order, you're left with an effect without an identifier, and the repetition does it again. That's why both go in the same transaction, and the identifier must be unique, so that two simultaneous attempts don't both get through.
In a web API, this identifier usually travels in a header called Idempotency-Key; in a queue, it's the event ID.
Prefer idempotency by nature whenever you can: it doesn't require storing identifiers and it has no expiration date. Use identity when whoever writes doesn't know the final state, as with the stock, or when the operation creates something new or causes an effect outside your control, like a charge. The two often live side by side in the same operation: in Ana's cancellation, the status is written as a final state, and the stock and the refund are protected by the same identifier.
Three terms show up together and are often used as if they were the same thing:
order.status = CANCELLED, doesn't need it.Taken literally, that promise can't be kept: you can't guarantee that a message is delivered exactly once over a network that can lose messages. It's the same doubt as in Ana's cancellation: whoever sends and gets no confirmation doesn't know whether the message arrived, so they either risk losing it or risk repeating it.
What systems actually offer is a combination: “at-least-once” delivery plus idempotent processing. The message may arrive twice, but the effect happens only once. Some people call this, more honestly, “effectively-once”.
When a tool advertises “exactly-once”, ask: exactly once where? A messaging platform's guarantee usually covers what is read and written inside it. On its own, it doesn't reach your database or the return of Ana's money. Outside the platform, it's still idempotency that handles the repetition.
Idempotency prevents the same operation from producing its effect twice. Five neighboring problems remain outside of it:
Before wrapping up, five questions on effect, timeout, the two forms, “exactly-once” and load:
Ana's cancellation reaches the system three times. Which of these outcomes shows the operation is not idempotent?
Ana didn't need a refund button that was harder to tap. She needed a system that recognized the same refund arriving three times.
Repetitions will happen. The design question isn't how to avoid them, but what happens when they arrive. The answer takes two forms: write the operation as a final state, which can be applied again, or give it an identity and remember it.
Pick an operation in your system that returns money, touches stock or sends an email and, outside production, run this article's test: execute it three times and compare the effects. Then make it harder: two attempts at the same time, a repetition days later and another operation changing the state in between. Passing the simple case doesn't prove the others.
To go beyond the article: how to retry with judgment, what “exactly-once” really promises and the pattern behind identity-based idempotency.
Software Architect and Engineer | Java & AI