Boss lesson · Unit 02
Retries without double charges
A lost reply, a retry, and the key that makes paying twice impossible
What you practise
- A timeout leaves the client not knowing
- Retry safely with an idempotency key
- What the server stores
- POST against PUT
Try a question from this lesson
Pick one
A client sends a payment and never gets the reply. From where the client sits, which of these could have happened?
client POST /payments {"amount": 50}
... no reply; the client times outCorrect answer
Any of these: it cannot tell them apart
Why
A lost request and a lost reply look identical from the client: silence. The card may or may not have been charged. Every retry design starts from this uncertainty.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
An idempotency key
Before the first attempt, the client invents a unique key for this one purchase. It sends the same key with every retry.
The server remembers each key together with the reply it produced. A request whose key it has seen is not run again: the saved reply is sent back.
POST /payments HTTP/1.1
Idempotency-Key: 7f3c-91ab-22e0
Content-Type: application/json
{"amount": 50}Concept
Or let the client name it
POST to a collection lets the server pick the id, so every POST makes a new thing.
If the client picks the id, it can PUT to the item's own URL. PUT means "this resource is exactly this", so a retry sets the same payment to the same state: idempotent with no extra machinery.
PUT /payments/7f3c-91ab-22e0 HTTP/1.1
Content-Type: application/json
{"amount": 50}10 more exercises in the app
- 2× type the answer
- 1× fill the blanks
- 4× multiple choice
- 2× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.