Idempotencia
Claves de idempotencia · Reintentos seguros
Escrito por Darjus Kazakevic · revisado el
A retry must be safe, so key the operation, not the request.
Networks lose responses, not only requests. The charge goes through, the response is lost, the client retries, and the customer pays twice. The fix is not fewer retries. It is making a retry harmless. The caller sends an idempotency key with the operation, and the server stores the result under it. A repeat gets the stored answer instead of a second charge.
Some operations are idempotent by nature: set a value, delete by id. That is why HTTP defines PUT and DELETE as idempotent, and POST as not. The cost for the rest is a key table with an expiry, and one rule: the same key with a different body is an error, not a retry.
En esencia — Make every write call safe to repeat: the caller names the operation with a key, and the server checks that key before it acts.
Cuándo recurrir a él
Recurre a él cuando
- Write calls retried after timeouts, like charges or order creation
- Messages delivered at least once, so duplicates are certain
- Webhooks you receive, which providers send more than once
Reconsidéralo cuando
- Operations already naturally idempotent, like set-by-id or delete
- A step that can't be made safe to repeat — that needs compensation
- Read-only calls: a GET changes nothing, so it is already safe
Conceptos clave
- Idempotent operation
- An operation where doing it twice has the same effect as doing it once. “Set status to paid” is idempotent; “add 10 to the balance” is not.
- Idempotency key
- A unique id that the client creates for one operation and sends with every retry of it. The server stores the key with the result and returns that result on a repeat.
- At-least-once delivery
- Queues and webhooks deliver a message again until it is acknowledged. A crash after the work but before the acknowledgement means the same message arrives twice, so duplicates are normal.
- Concurrent duplicates
- Two copies of the same request can arrive at the same moment. Both see “key not found” and both act. Claim the key first, with a unique insert, and only then do the work.
Reglas de diseño
- Key the operation, not the attempt
- The caller sends a key that names the operation, such as “charge order 42”, and reuses it on every retry. The server checks it before acting and replays the stored answer.
- Claim the key before doing the work
- Two retries can arrive together. Insert the key as 'in progress' under a unique constraint first; the request that loses gets 409 instead of acting twice.
- Refuse a reused key with a new body
- The same key with a different body is not a retry; it is two operations with one name. Store a hash of the body with the key and reject a mismatch with 422.
- Pass the key downstream
- If your service calls a payment provider, send it an idempotency key derived from yours. Otherwise your service is safe to retry but the provider still sees two charges.
Errores habituales
- A new key on every retry. The retry loop calls uuid() each time, so every attempt looks like a new operation.
- Check then act without a claim. Two retries both find no key, and both charge the card.
- Expiring keys before clients stop retrying. A retry after 25 hours, with a 24-hour window, charges again.
- Storing the key in a different transaction from the effect. A crash between them leaves the work done but the key missing.