awesome-everything RU
↑ Back to the climb

Backend Architecture

Why idempotency: making retries safe

Crux A network can swallow the server''''s response. Without an idempotency key the client has no way to retry safely — every duplicate POST may fire the side effect twice.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at junior altitude — the surface
◷ 10 min

Bea hits “Pay Now” on a checkout page. Her wifi drops before the response arrives. She presses again. The server has no way to tell the second click from the first — so it charges her twice.

The network guarantee problem

The internet guarantees at-least-once delivery if the client retries until it sees an answer, and at-most-once if it never retries. Neither is acceptable on its own:

  • At-most-once — a payment that never finished because the client gave up.
  • At-least-once without dedup — a double charge.

The gap between them is the application’s job to close. The tool is idempotency.

What an idempotency key is

An idempotency key is a client-generated opaque string — typically a UUID v4 (36 characters) — sent in the Idempotency-Key HTTP request header. The client picks one key per logical operation, not per request. Every retry of the same operation reuses the exact same key.

The server uses the key as a primary token to answer: “have I already processed this?” If yes, it replays the original response without re-running the business logic. If no, it processes and records the result.

ScenarioWithout keyWith key
Response delivered1 charge1 charge
Response lost, client retries once2 charges1 charge (replay)
Response lost, client retries 3×4 charges1 charge (replay)
Key expires before retryN/ATreated as new — risk of duplicate

The metaphor: envelope + clipboard

Imagine sending money via courier. You mark the envelope “Order #A7-payment” and tell the courier: “If this envelope was already delivered, do not deliver another.” On every retry you write the same order number on the new envelope. The recipient checks a clipboard (the idempotency cache) and either delivers the envelope or says “already done — here is the receipt.”

The clipboard remembers. Without it, every envelope looks new.

HTTP convention

Stripe popularized the Idempotency-Key header in 2014. Square, PayPal, Adyen, and the IETF draft draft-ietf-httpapi-idempotency-key-header all follow the same shape. RFC 9110 §9.2.2 lists which HTTP methods are idempotent by spec (GET, HEAD, PUT, DELETE, OPTIONS) — POST is not, which is exactly why the header exists.

Quiz

What problem does an idempotency key solve?

Quiz

A flaky network lost the response to a POST /charge. What is the safest thing the client can do?

Order the steps

Put the steps of a safe retry in order:

  1. 1 Client generates a unique Idempotency-Key (UUID v4) for the operation
  2. 2 Client sends POST with the Idempotency-Key header
  3. 3 Network or server fails before the client sees a response
  4. 4 Client retries the same POST with the SAME Idempotency-Key
  5. 5 Server recognizes the key and replays the cached response or finishes the original work
  6. 6 Client gets one confirmation and stops retrying
Complete the analogy

Fill in the blank: an idempotency key is like marking the envelope with an order number so the recipient checks a _______ before delivering it again.

Recall before you leave
  1. 01
    What is the difference between at-least-once and at-most-once delivery, and why is neither acceptable alone for a payment API?
  2. 02
    Why is one key per logical operation the rule, not one key per HTTP request?
  3. 03
    Which HTTP methods are idempotent by RFC 9110 definition, and why does POST need the header?
Recap

The internet cannot deliver a request exactly once — only at-most-once (no retry) or at-least-once (retry until acknowledged). A payment API needs exactly-once behavior, which requires both sides to cooperate: the client retries with the same Idempotency-Key header, and the server deduplicates using that key as a primary lookup token. Stripe popularized the Idempotency-Key HTTP header in 2014; IETF draft draft-ietf-httpapi-idempotency-key-header formalizes it. The next lesson covers the server-side state machine that makes this work.

Connected lessons
appears again in179
Continue the climb ↑Server-side state machine: four states of an idempotency key
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.