Idempotency
Placeholder. This lesson is drafted but not written yet.
A timeout left you without an answer, so you retry. If the first attempt did land, the customer is now charged twice.
An operation is idempotent when performing it twice has the same effect as performing it once. That property is what makes a retry safe.
The key is supplied by the caller
The server cannot tell a retry apart from a genuine second request. Only the caller knows the two are the same intent, so the caller names it:
await fetch(CHARGES_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Idempotency-Key': order.id
},
body: JSON.stringify(order)
});
The server stores the result against that key. A second request carrying the same key returns the stored result instead of charging again.
What is still to write
- Choosing a key that is stable across retries but unique across intents
- Storing the response, not just a “seen” marker
- How long a key has to be retained
- Operations that are naturally idempotent, and ones that can never be