Idempotency and Safe Retries
Core Concepts (5)
- 1.Networks fail after the server acted, so clients must be able to retry
- 2.An idempotency key turns a repeated request into a replay
- 3.Store the key with the response, scoped to the caller
- 4.Retries need backoff and jitter to avoid stampedes
- 5.Exactly-once effect is built from at-least-once delivery plus deduplication
Map to Your Experience
Imagine tapping Pay and seeing a spinner that never finishes. What do you do, and what do you fear?
Design Brief: Make Payment Creation Retry-Safe
Learn
The hard fact of networked systems is that a client cannot tell the difference between a request that never arrived and a response that never returned. If the client times out on a create-payment call, the money may or may not have moved. The only safe policy is to make retrying harmless.
Reads, replacements, and deletes are naturally idempotent. Creation is not. The standard remedy is an idempotency key: the client generates a unique value for each logical operation and sends it in a header. The server records the key together with the outcome. If the same key arrives again, the server returns the stored response instead of acting a second time.
Details matter. Scope keys to the authenticated caller so one customer cannot collide with another. Store the key before doing the work, and record a state such as in progress or completed, so two simultaneous duplicates do not both proceed. Compare a fingerprint of the request body, and reject a reused key carrying different content. Keep records long enough to cover realistic retry windows, often a day or more.
On the client side, retry only what is safe or keyed, and only for errors that suggest transient failure, such as timeouts and 503 responses. Use exponential backoff with random jitter so thousands of clients do not retry in lockstep. Cap the number of attempts and the total time, and respect any retry hint the server provides.
Teams often say they need exactly-once delivery. In practice that does not exist across a network. What you can build is at-least-once delivery combined with deduplication on the receiver, which gives an exactly-once effect. The idempotency key is that deduplication mechanism, and it is worth designing before the first incident, not after.
Map
List four endpoints in a system you know. For each, state whether it is naturally idempotent, needs a key, or should never be retried. Identify what a duplicate would cost in each case.
Design
Write the idempotency design for create-payment: header name, key scope, the stored record with its states, the duplicate-in-flight behaviour, and the expiry. Add a short client retry policy with attempts, backoff, and jitter. Keep your answer concrete: name real numbers, owners, and examples wherever you can, and note any assumption you had to make so a reviewer can challenge it.
Requirements
- •Define the key format and scope
- •Describe storage and expiry of recorded responses
- •Explain behaviour for a concurrent duplicate
Constraints
- •Charging twice is unacceptable
- •Keys must survive a server restart
Must Cover
Trade-off Axis
safety vs storage cost
Referenced Patterns
Competencies Advanced
Ready to Design?
Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.
Open in Studio →