Definition
What it is
An idempotent operation has a fixed point. Calling it once sets the system to state X. Calling it a second, third or hundredth time leaves the system in state X. The critical property is not the number of executions; it is the absence of cumulative effects.
PUT /users/42 {name: "Ana"} is idempotent: whether it runs once or ten times, user 42 ends up named Ana. POST /users is not: each call creates a new user. DELETE /users/42 is idempotent in effect (user 42 ends up deleted) even if the second call returns 404: the state after one deletion equals the state after ten.
Why it matters
The moment a network request leaves the client, its fate is unknown. It may have arrived, may have been processed, may have been lost before arriving. If the operation is idempotent, the client can retry blindly; if not, the client must reason about "did my POST create one user or two?". Idempotency turns a distributed-systems problem into a design property.
Safe vs idempotent vs neither
Method | Safe | Idempotent | Typical effect |
|---|---|---|---|
GET | Yes | Yes | None |
HEAD | Yes | Yes | None |
OPTIONS | Yes | Yes | None |
PUT | No | Yes | Replace resource |
DELETE | No | Yes | Remove resource |
POST | No | No | Create, trigger |
PATCH | No | Not guaranteed | Partial update |
HTTP idempotency map
graph TD HTTP[HTTP methods] --> Safe[Safe: GET, HEAD, OPTIONS] HTTP --> Idem[Idempotent: PUT, DELETE, GET, HEAD, OPTIONS] HTTP --> Unsafe[Not idempotent: POST, PATCH] Safe --> Idem style Safe fill:#E8F5E9 style Idem fill:#E8F5E9 style Unsafe fill:#FFEBEE
How to make an operation idempotent
Making an operation idempotent
Use a natural key
PUT /users/42is naturally idempotent.POST /usersis not; generate the ID client-side or use an Idempotency-Key header.Use idempotency keys
Stripe's approach: client sends
Idempotency-Key: <uuid>. Server stores the response; a repeated key returns the cached response.Prefer upserts
INSERT ... ON CONFLICT DO UPDATE(Postgres),MERGE(SQL standard),PUTin REST.Design the schema for it
Unique constraints, version columns, state machines. Schema enforces idempotency that code alone cannot.
Common failures
Origin
The mathematical sense of idempotent appeared in 19th-century algebra: an element a of an algebraic structure is idempotent if a·a = a. Benjamin Peirce (1870) introduced the term for idempotent elements in linear algebras. It passed into category theory in the 1940s (Eilenberg and Mac Lane) for morphisms whose composition with themselves yields themselves. From there it entered computer science as a property of operations and functions. HTTP formalized it in RFC 2616 (1999) for methods whose repeated application has the same effect as a single application; the rule survives in RFC 9110 (2022).
Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request.
— RFC 7231, Section 4.2.2, 2014
Evidence
FAQ
- Safe vs idempotent?
Safe means read-only. Idempotent means repeatable without additional effects. GET is both; DELETE is idempotent but not safe; POST is neither.
- Why does DELETE return 204 then 404?
The response differs, the state does not. RFC idempotency refers to side-effects, not to status codes.
- How does Stripe's idempotency key work?
The client generates a key, sends it with the request, and the server stores the response. A retry with the same key returns the stored response without re-executing the operation.
- Can PATCH be idempotent?
Sometimes.
PATCH {set name: "Ana"}is effectively idempotent.PATCH {increment counter}is not. RFC 9110 does not guarantee it.
Connections
- http-methods
- rfc-9110
- retry
- distributed-systems
- stripe
Fielding, R. & Reschke, J. (2014). RFC 7231. https://www.rfc-editor.org/rfc/rfc7231
Fielding, R., Nottingham, M. & Reschke, J. (2022). RFC 9110. https://www.rfc-editor.org/rfc/rfc9110
Stripe (2024). Idempotent requests. https://stripe.com/docs/api/idempotent_requests
Wikipedia (2024). Idempotence. https://en.wikipedia.org/wiki/Idempotence
Etymology
Origin: compound
idem(pronoun): Latin, "the same"potens(adjective): Latin, "powerful, having power"
Semantic network
● broader ● narrower ● related
Used in the garden
- Architecting API Dominance: Design a REST API That Scales From Day One (4×)
- Master Linux Automation: Bash Scripting Secrets for Solo IT Founders (2×)
- Mastering Database Schema Version Control: The DevOps Secret Weapon (1×)
- From Script to Microservice: Scaling Reporting Automation to 10K Daily Requests (1×)
Sources
Fielding, R. & Reschke, J. (2014). RFC 7231. https://www.rfc-editor.org/rfc/rfc7231
Fielding, R., Nottingham, M. & Reschke, J. (2022). RFC 9110. https://www.rfc-editor.org/rfc/rfc9110
Stripe (2024). Idempotent requests. https://stripe.com/docs/api/idempotent_requests