Idempotency
Making an operation safe to repeat, so that retries after timeouts and duplicate messages never charge twice or create two orders: natural idempotency, idempotency keys, deduplicated consumers, and idempotency across services.
Overview
An operation is idempotent when doing it twice leaves the system in the same state as doing it once. Setting a user's email to ana@example.com is idempotent; adding 10 dollars to a balance is not. In math terms, f(f(x)) = f(x).
It matters because distributed systems retry. A client that sends a request and hears nothing back cannot tell whether the request was lost, whether it ran and the reply was lost, or whether it is still running. The only safe move is to try again, and retrying is only safe if the operation is idempotent. Message queues add the same problem from the other side: most deliver each message at least once, so consumers see duplicates.
The goal is sometimes called exactly-once, but no network can deliver a message exactly once. What systems actually build is at-least-once delivery plus idempotent processing, which gives an exactly-once effect. This topic covers the ways to get there and the details that make them hold up under crashes and concurrency.
What you will be able to do
- Say which HTTP methods and which kinds of operations are idempotent, and why.
- Make a write idempotent with an upsert, a unique constraint, a conditional write or an idempotency key.
- Build an idempotency-key store that survives concurrent retries and crashes.
- Deduplicate messages in a consumer so duplicates have no effect.
- Keep a multi-service flow idempotent end to end.
Key concepts
- Idempotent vs safe
- The ambiguous timeout
- At-least-once plus idempotent
- Upserts and unique constraints
- Conditional writes
- Idempotency keys
- Request fingerprint
- Processed-message table
- Derived keys across services
Interview questions
- What does idempotent mean, and which HTTP methods are idempotent?
- Design an idempotent payment API.
- What is the difference between exactly-once delivery and exactly-once processing?
- How do you make a message consumer idempotent?
- Two identical requests with the same idempotency key arrive at the same time. What happens?
- How long should you keep idempotency keys, and what does it cost?
- How do you keep a multi-service order flow idempotent?
- Can you make an increment idempotent?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio