CONCEPT
What is an idempotency key?
An idempotency key is a unique id that the client chooses and sends with a request, so a retry of the same operation can be recognized and not applied twice. The server stores the key together with the result, and answers a repeated key with the stored result instead of acting again.
Also called: idempotency token, request id. By Zeeshan Adil··Distributed Systems·Databases
The problem it solves
A request times out. The client does not know whether the server never got it, did the work and lost the reply, or is still working. Giving up risks losing the action. Sending it again risks doing it twice, which for a payment or a text message is a real harm. The client cannot tell the cases apart, so it needs a way to retry that is safe in all of them.
How it works
The client generates a unique key when it first decides to do the operation, and reuses the same key on every retry of that operation.
POST /charges Idempotency-Key: 7f3a... -> server applies it, stores key + result
POST /charges Idempotency-Key: 7f3a... -> server finds the key, returns the stored result
Rules that make it work:
- Generate once per operation, not once per attempt. A new key on each retry defeats the point.
- Store the key atomically with the effect. Use a unique constraint in the same transaction as the write. Checking first and writing second leaves a gap that two concurrent retries can both slip through.
- Keep keys long enough. A key only protects retries made while it is remembered. Providers keep them for a limited time.
What it does not guarantee
It makes retries safe, not the first attempt succeed. It does not help if the server ignores the key. It also says nothing about two different operations: reusing a key for a different request should be rejected, not silently answered with the old result.
Measured
These numbers come from my own runs. In the sharding challenge, 100 writes whose reply was lost after the commit were retried. With the same request id on the retry, 100 rows were stored and 0 were duplicates. With a retry that started over without one, 200 rows were stored and 100 were duplicates.
In the one message per action challenge, a local run against a fake provider that injected timeouts and duplicate webhooks sent 2,000 actions through 200 random SIGKILLs and a database restart. All 2,000 had exactly one effect in the provider's own log. 60 needed a second attempt, and each one reused the original key after a lookup.
RUN IT YOURSELF
Challenge · Distributed Systems ChallengesOne message per action through a flaky providerGO DEEPER
When RabbitMQ Isn't Enough: Building Durable, Retryable JobsDatabase-backed jobs with atomic claiming, backoff, circuit breakers and an outbox, and what each piece does and does not guarantee.Why Exactly-Once Processing Is Hard: RabbitMQ, Retries, and IdempotencyWhere RabbitMQ duplicates come from and how an outbox, deduplication keys and idempotent consumers protect database effects and external calls.RELATED CONCEPTS
What is the transactional outbox pattern?What is at-least-once delivery?SOURCES
Get the next deep dive by email
New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.
You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.
Written by Zeeshan Adil
I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.