Idempotency
| Concept | |
|---|---|
![]() Image placeholderPlace the file shown below in the assets/articles folder idempotency.webp | |
| Defined in | RFC 9110 (HTTP semantics) |
| Related | Retries, at-least-once delivery |
| Field | APIs, distributed systems |
Idempotency is the property of an operation whose effect is the same whether it is applied once or many times. In mathematics, a function f is idempotent if f(f(x)) = f(x) for every x; taking the absolute value of a number twice gives the same result as taking it once. In software, the term describes operations that can be safely repeated, which makes it one of the most practical ideas in the design of reliable systems.
The property matters because networks and machines fail in ways that leave the caller uncertain. If a request times out, the client cannot know whether the server processed it. When the operation is idempotent, the client can simply send it again.
Why it matters
Distributed systems cannot guarantee that a message is delivered exactly once without help from the application. They can usually guarantee delivery at least once, by retrying until an acknowledgement arrives. Retrying means that duplicates will sometimes reach the receiver. Making the receiver idempotent turns at-least-once delivery into effectively-once processing. Payments, order placement and message handling all depend on this reasoning.
Idempotency also simplifies recovery. A job that can be re-run from the beginning after a crash is far easier to operate than one that must be repaired by hand.
Idempotency in HTTP
The HTTP specification classifies request methods by their intended semantics.[1] A method is idempotent if the intended effect on the server of several identical requests is the same as for a single request.
| Method | Idempotent | Notes |
|---|---|---|
| GET, HEAD, OPTIONS, TRACE | Yes | Also safe: they do not change state |
| PUT | Yes | Replaces a resource with the supplied representation |
| DELETE | Yes | Removing something twice leaves it removed |
| POST | No | Typically creates a new resource each time |
| PATCH | Not guaranteed | Depends on the patch format |
PUT /users/42 {"name":"Ada"} -> state: name=Ada
PUT /users/42 {"name":"Ada"} -> state: name=Ada (unchanged)
POST /orders {...} -> creates order #1
POST /orders {...} -> creates order #2 (not idempotent)The definition refers to the intended effect on server state, not to the response. A second DELETE of the same resource may return a 404 status where the first returned 204, and the operation is still idempotent because the resulting state is identical.
Making non-idempotent operations safe
Many important operations, such as charging a card or creating an order, are naturally non-idempotent. Several techniques make them safe to retry.
Idempotency keys
The client generates a unique key for each logical operation and sends it with the request, commonly in an Idempotency-Key header. The server stores the key together with the result of the first attempt. If the same key arrives again, the server returns the stored result instead of repeating the work.[2] Payment providers popularised this pattern, and an IETF draft proposes a standard header for it.[3]
Natural keys and unique constraints
If every order carries a client-supplied identifier, a database unique constraint rejects the duplicate. In SQL this is often written as an insert that does nothing on conflict, or as an upsert that overwrites with identical data.
Conditional requests and versioning
Headers such as If-Match carry an entity tag so that an update applies only if the resource is still in the expected state. This prevents a stale retry from overwriting newer data and is a defence against lost updates, which relate closely to the problems described in race conditions.
Deduplication of messages
Message consumers record the identifiers of messages they have already handled and skip repeats. The record and the business change should be committed in the same transaction, otherwise a crash between the two reintroduces duplicates.
Idempotency beyond APIs
The idea appears throughout operations and tooling. Configuration management tools such as Ansible aim for tasks that describe a desired state, so running a playbook twice leaves the system unchanged the second time. Infrastructure-as-code tools compare desired and actual state and change only the difference. Database migrations are often written to be re-runnable, and Kubernetes controllers repeatedly reconcile the observed state of a cluster toward the declared one, which only works because each reconciliation step is idempotent.
Pitfalls
- Side effects outside the operation. Sending an email or publishing an event is not undone or deduplicated by an idempotent database write.
- Key scope and expiry. Keys must be unique per operation and per client, and stored long enough to cover realistic retry windows.
- Different payload, same key. A server should reject a repeated key whose request body differs from the original, since that indicates a client bug.
- Concurrent duplicates. Two copies of a request may arrive at the same moment, so the key check and the work must be serialised, for example with a unique constraint or a lock.
- Hidden state. Operations such as "increment a counter" or "append to a list" are not idempotent unless reformulated, for instance as "set the counter to 7".
Common misconceptions
Idempotent does not mean "safe": a DELETE is idempotent but certainly changes state. It also does not require identical responses on every call. And idempotency does not remove the need for retry discipline; clients should still use backoff with random jitter so that a recovering server is not overwhelmed by synchronised retries.
Designing operations as "make the state equal to X" rather than "change the state by Y" is a simple rule of thumb that produces idempotent behaviour in most cases and pays off every time the network misbehaves.
Editorial connections and engineering implications
Idempotency keys and concurrent retries
An API can make an operation retry-safe by accepting a client-generated idempotency key. The server records the key with the outcome of the first completed request and returns the same outcome for later requests with that key. The key should be scoped, retained for a documented period and associated with a request fingerprint so that reuse with different data is rejected. A unique database constraint or transaction is needed to handle two matching requests that arrive at nearly the same time.
Idempotency does not mean every repeated request has no effect. A read is naturally repeatable, while a payment-creation request needs a durable operation identity and a defined result. A timeout also does not prove that the server failed: it only means the caller did not receive a timely response. This is why retry rules must reflect the operation's semantics. Idempotency is essential in service architectures, HTTP APIs and distributed deployments.
See also
- Microservices – An architectural style that structures an application as independently deployable services communicating through explicit interfaces.
- Race Condition – A defect in which the outcome of a program depends on the unpredictable timing of concurrent operations.
- CAP Theorem – A result stating that a distributed data store cannot simultaneously guarantee consistency, availability and partition tolerance.
- HTTP/3 – The third major version of the Hypertext Transfer Protocol, which runs over QUIC instead of TCP.
- Kubernetes – An open-source system for automating deployment, scaling and management of containerised applications.
- PostgreSQL – An open-source object-relational database system with a strong emphasis on standards compliance and extensibility.
- TLS 1.3 – The 2018 version of the Transport Layer Security protocol for encrypted network communication.
- Tim Berners-Lee – British computer scientist who invented the World Wide Web.
References
- ↑ RFC 9110, HTTP Semantics, section 9.2.2 Idempotent Methods (rfc-editor.org)
- ↑ Stripe API documentation: Idempotent requests (docs.stripe.com)
- ↑ IETF draft: The Idempotency-Key HTTP Header Field (datatracker.ietf.org)
