Bookmarks

Idempotency

A property of an operation to produce the same result whether it runs once or any number of times, so that it can safely be retried after a failure.

Also: operation idempotency, idempotent request, idempotent, idempotence

checked 8 Nov

All terms

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.

MethodIdempotentWhy
GET, HEAD, OPTIONSyessafe methods that only read data
PUTyesreplaces the state of the resource — the same request leads to the same state
DELETEyesa retry returns the same “resource absent” state, though the response may differ
POSTnoeach 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 UPDATE and UPDATE … SET counter = 5 instead of counter = counter + 1 make 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

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • retry with exponential backoff

    Retry with exponential backoff is a strategy for when to retry a request; idempotency answers whether it is safe to retry at all.

  • eventual consistency

    Eventual consistency is about how quickly replicas converge; idempotency is about the effect of retrying a single operation.

  • ACIDACID transaction properties

    ACID is a set of guarantees for a single transaction inside a database; idempotency is a property of an operation retried over the network, which ACID does not provide by itself.

In depth in the references

where the topic is covered in detail