Retries are unavoidable in distributed systems: the network blips, a response never arrives, a timeout expires — and the client cannot tell a lost request from one that was executed while its response was lost. Idempotency solves this at the level of the operation: if a retry does not change the result, it can be performed without fear that the account will be charged twice or a duplicate order will appear.
Where the term comes from
In mathematics, an element or an operation is called idempotent when repeating it does not change the result: multiplying by 1 and taking the absolute value always yield the same value no matter how many times they are repeated. Programming borrowed the word from this tradition: operations whose repetition does not change the effect on the server are called idempotent in the same way.
Idempotent HTTP methods
RFC 9110 deems a method idempotent when the intended effect on the server of several identical requests is the same as that of a single request. Only idempotent methods may be retried automatically: if the connection drops before a response arrives, the client knows that a retry will not double the effect.
| Method | Idempotent | Why |
|---|---|---|
| GET, HEAD, OPTIONS | yes | safe methods that only read data |
| PUT | yes | replaces the state of the resource — the same request leads to the same state |
| DELETE | yes | a retry returns the same “resource absent” state, though the response may differ |
| POST | no | each call may create a new object or start a new process |
Idempotency concerns the effect on the server, not the response: a server may log every request and return different status codes, and the method remains idempotent all the same. Under RFC 9110, proxies must not automatically retry non-idempotent requests.
Idempotency key
For non-idempotent operations such as POST /payments, there is the idempotency key: the client generates a unique key itself and sends it with the request. The server stores the result of the first execution and, on a retry with the same key, returns the stored response instead of performing the operation again. This is how Stripe accepts payments: the key is a random string of up to 255 characters, and a retry with the same key returns the stored response of the first request.
Rules for the key: it is generated by the client, because only the client can tell a retry of the same intent from a new intent; it lives briefly — the server stores the responses and removes them after a while; a new intent needs a new key, otherwise the new operation will receive the stored response of the first one.
Where retries are a given
- Message queues: brokers deliver “at least once”, so a duplicate message is the norm, and the consumer makes processing idempotent: it keeps the keys of already processed messages and skips duplicates (the idempotent consumer pattern).
- Kafka producer:
enable.idempotence=true— when it retries after a failure, the broker will not write the batch twice. - Databases:
INSERT … ON CONFLICT DO UPDATEandUPDATE … SET counter = 5instead ofcounter = counter + 1make writes idempotent. - Ansible: modules bring the system to the declared state, so running the playbook again is safe.
Sources
the term is written from these pages- RFC 9110 — HTTP Semantics, раздел 9.2.2 Idempotent Methodsrfc-editor.org/rfc/rfc9110.html(opens in a new tab)
- Microsoft Learn — Idempotent Consumer patternlearn.microsoft.com/en-us/azure/architecture/patterns/idempotent-consumer(opens in a new tab)
- Stripe API — Idempotent requestsdocs.stripe.com/api/idempotent_requests(opens in a new tab)
Verified 8 Nov