Idempotency Explained: How Safe Retries Work in Distributed Systems

Idempotency explained in plain terms means understanding a simple promise: running the same operation twice has the same effect as running it once. The central question is what happens when a request is sent, the network fails, and nobody knows whether it arrived, and the answer is idempotency, which makes it safe to simply try again.

An idempotent operation is one that can be applied more than once without changing the result beyond the first application. Setting a value to ten is idempotent, since setting it again changes nothing, while adding ten is not, since doing it twice adds twenty, and that distinction is what makes retries safe in a system where messages can be lost or duplicated.

The idea is simple: in a distributed system, a caller can never be sure whether a request that timed out was processed or not. If the operation is idempotent, the caller can retry it without fear, and the system will end up in the right state either way. This article explains what idempotency is, how it works, and where it is used.

Key takeaways

  • An idempotent operation has the same effect run once or many times.
  • Idempotency makes retries safe when the outcome of a request is unknown.
  • Idempotency keys let a server recognise a repeated request.
  • Reads are naturally idempotent; writes usually need design work.
  • Payments, APIs, and message processing all depend on it.

What idempotency is

Idempotency comes from mathematics, where an operation is idempotent if applying it twice gives the same result as applying it once. In software, the term is used for operations on a system: a request that sets a user’s name, deletes a file, or marks an order as shipped is idempotent, while a request that adds money to a balance, increments a counter, or sends an email is not, unless it is specially designed to be.

The problem idempotency solves is the ambiguity of failure. When a client sends a request and receives no answer, there are two possibilities: the request never arrived, or it arrived, was processed, and the answer was lost on its way back. The client cannot tell which, and the only safe action is to retry, but a retry is only safe if processing the request a second time is harmless.

Idempotency is therefore a property of the operation’s design, not of the network. A system that promises idempotency must detect repeated requests and handle them correctly, usually by remembering which requests it has already processed, and returning the original result instead of doing the work again. Our guide to REST APIs explains the interfaces where idempotent methods such as PUT and DELETE are part of the design, and our guide to distributed systems explains the setting in which requests can be lost, delayed, or duplicated.

How idempotency works

The most common mechanism is the idempotency key. The client generates a unique key for each logical operation, usually a random identifier, and sends it with the request. The server stores the key together with the result of the operation, and if a request arrives with a key it has already seen, it returns the stored result without doing the work again.

The key must be stored durably, in the same transaction as the work itself, or the guarantee breaks. If the server processes a payment and then crashes before recording the key, a retry will process the payment a second time, so the key and the effect must be committed together, atomically, which is why idempotency is usually implemented inside the database rather than in front of it.

Some operations are idempotent by nature and need no key. Setting a field to a value, deleting a record, and marking a state are all safe to repeat, since the second run finds the system already in the desired state and changes nothing. Designing operations this way, as setting state rather than changing it by an amount, is the simplest route to idempotency.

Other operations need a check before they act. A payment service, for example, can look up whether a charge with the same reference already exists before creating a new one, and return the existing charge instead. This check-then-act pattern works when the check and the act are atomic, but it can fail under concurrency, so a unique constraint in the database is usually the safer enforcement.

Message processing uses idempotency to handle duplicate delivery. A consumer that receives the same message twice, because the broker redelivered it after a crash, can check a record of processed message identifiers and skip the second copy, so that an order is never created twice from a single event.

A dark desk with a glowing green stamp pressing the same mark twice onto a document, the second impression landing exactly on the first
A dark desk with a glowing green stamp pressing the same mark twice onto a document, the second impression landing exactly on the first, shows how idempotency works: the second run changes nothing.

Comparing idempotency with other safety measures

Idempotency is one of several ways to make distributed operations safe, and each solves a different part of the problem. Exactly-once delivery tries to prevent duplicates at the transport level, transactions make a group of changes atomic, and deduplication removes repeated records after the fact, but none of them replaces idempotency entirely.

The table below sets out the main contrasts between the leading approaches.

Aspect Idempotency Exactly-once delivery Transactions
What it guarantees Same effect, repeated runs Each message delivered once All changes, or none
Where it lives In the operation’s design In the messaging layer In the database
Handles retries Yes, by design Only if delivery is perfect Only within one system
Cost Key storage and checks Complex protocols Locks and coordination
Best for APIs and payments Stream processing Single-database work

The practical result is that idempotency is the right tool for operations a client may retry, exactly-once delivery suits stream processing where the framework can help, and transactions suit work that stays inside one database.

Where idempotency is used

Payment processing depends on idempotency more than any other area. A customer who clicks pay twice, or a client that retries a charge after a timeout, must never be charged twice, so payment providers require an idempotency key with every charge, and return the original charge when a key is repeated.

Public APIs use idempotency keys for any operation that changes state. A client creating a resource sends a key with the request, and if the network fails, it retries with the same key, receiving the original resource instead of creating a duplicate, which is why the pattern is now standard in API design.

Message consumers use idempotency to survive redelivery. A queue that guarantees at-least-once delivery will sometimes deliver the same message twice, and a consumer that records processed identifiers can skip the duplicate, so that an event is never applied twice to the system.

Cloud infrastructure uses idempotency in its control planes. A request to create a virtual machine, a bucket, or a DNS record can be retried safely, because the control plane checks whether the resource already exists and returns it, rather than creating a second one.

File and data synchronisation uses idempotency to make uploads safe. An upload that is interrupted can be resumed or retried, and the server, recognising the same content or the same upload identifier, stores it once, so that a flaky connection never produces duplicate files.

Strengths and limits

The greatest strength of idempotency is that it turns an impossible question into a safe action. A client that does not know whether its request succeeded no longer needs to find out; it can simply retry, and the system will do the right thing, which is what makes distributed systems usable in the presence of unreliable networks.

It is also a strength that idempotency is cheap once it is designed in. A key, a unique constraint, and a stored result are all that is needed, and the pattern scales well, since the check is a single lookup that does not require coordination across machines.

The limits are real, though. Idempotency keys must be stored, and storing them forever is expensive, so systems usually expire them after a day or a week, after which a retry may be treated as new. Some operations also cannot be made idempotent at all, such as sending an email or making a phone call, where the effect happens outside the system and cannot be undone or detected.

Idempotency checklist

  • Design every state-changing operation to be idempotent from the start.
  • Accept an idempotency key with every request that changes state.
  • Store the key and the result together, in one atomic step.
  • Return the stored result when a key is repeated, not an error.
  • Use unique constraints in the database to enforce the guarantee.
  • Prefer setting state over changing it by an amount.
  • Expire old keys, and document how long they are kept.
  • Record processed message identifiers in every consumer.
  • Test retries after a crash at every point in the operation.

Frequently asked questions

What is idempotency in simple terms?

It means doing something twice has the same effect as doing it once. An idempotent operation can be retried safely, because the second run changes nothing, which matters when a request may have been processed but its answer was lost.

What is an idempotency key?

It is a unique identifier the client sends with a request, so the server can recognise a repeat. The server stores the key with the result of the operation, and if the same key arrives again, it returns the stored result instead of doing the work twice.

Which HTTP methods are idempotent?

GET, HEAD, PUT, and DELETE are idempotent by definition, since repeating them has the same effect. POST is not, since repeating it usually creates a new resource, which is why APIs that accept POST often add idempotency keys.

How long should an idempotency key be kept?

Long enough to cover any realistic retry, usually a day to a week. Keeping keys forever is expensive, and after expiry a repeated key may be treated as a new request, so the retention period should be documented.

Can sending an email be idempotent?

Not truly, since the email leaves the system and cannot be taken back. The best a system can do is record that it sent the message, and check that record before sending again, which prevents most duplicates but cannot guarantee none.

When should I not rely on idempotency?

When the operation has an effect outside the system, when the key cannot be stored atomically with the work, or when the operation is a read that never changes state. In those cases another mechanism, such as a transaction, may be needed.

The bottom line

Idempotency explained in one line is this: make every operation safe to repeat, and a lost answer stops being a problem.

It is the property that lets a client retry without fear, and the reason a payment, an API call, or a message can survive the unreliable networks that distributed systems are built on.

This article is general information about how idempotency works, not advice about designing operations for a particular system.

Latest articles

Scroll to Top