Skip to content

Idempotent

Idempotent describes an operation that produces the same result whether executed once or many times; in HTTP, an idempotent method is one whose repeated invocation has the same effect as a single invocation.

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

  1. Use a natural key

    PUT /users/42 is naturally idempotent. POST /users is not; generate the ID client-side or use an Idempotency-Key header.

  2. Use idempotency keys

    Stripe's approach: client sends Idempotency-Key: <uuid>. Server stores the response; a repeated key returns the cached response.

  3. Prefer upserts

    INSERT ... ON CONFLICT DO UPDATE (Postgres), MERGE (SQL standard), PUT in REST.

  4. 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
1870 · Benjamin Peirce introduce el término algebraico
1940s · Eilenberg y Mac Lane en teoría de categorías
1999 · RFC 2616 formaliza idempotencia HTTP
2014 · RFC 7231 refina la definición
2022 · RFC 9110 unifica HTTP semantics
5 · métodos HTTP idempotentes según RFC 9110

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

Etymology

Origin: compound

  • idem (pronoun): Latin, "the same"
  • potens (adjective): Latin, "powerful, having power"

Semantic network

Idempotentdistributehttp-semanhttp-methorfc-9110Retrydistributestripe

● broader ● narrower ● related

Broader

  • distributed-systems
  • http-semantics

Related

  • http-methods
  • rfc-9110
  • Retry
  • distributed-systems
  • stripe

Used in the garden

Sources