At the community canteen's lunch rush, the till belongs to Sister Chen. The house rule: no need to shout your order, just slip a note through the window with your desk number and the date. Sister Chen pins each note to the corkboard behind her, cooks one meal, crosses it off, and charges once.
Liu from the third floor never quite trusts it. When he slips a note in and hears nothing back, he writes another and slips that in too. Some lunchtimes, three notes all saying "3-17, Thursday" arrive.
Sister Chen never gets annoyed. She looks only at the number: if "3-17, Thursday" is already pinned on the board, every later copy gets folded and tucked under a plate. Still only one meal is cooked, and still only one charge is made. When Liu comes to collect, she hands over his meal along with the folded notes: "Got it three times, made it once."
On his first day, the new helper Fang lands in the busiest part of the lunch rush. Every note he sees goes straight into the wok and straight into the ledger. When they close up and tally, "3-17" has been cooked three times and charged three times. Liu comes over with the bill to check it, red in the face: he ate one meal but owes for three.
Sister Chen doesn't call Fang lazy. He works harder than anyone. She taps the corkboard: "In this job, notes will come again and people will worry. You can't stop that. The one thing you can hold on to: don't count how many notes came in. First check whether that number is already on the board. If it is, the meal is already made. Even if he slips in ten more, treat each one as him asking 'Is mine ready?' and just hand him the same answer again."
Fang crosses out the two extra charges and eats the two extra meals himself. The next day, the first thing he does is not light the stove. He turns around and checks the board.
What does this story illustrate?
Concept: Idempotency, one of the core design principles of payment APIs like Stripe.
Idempotency means that performing the same operation once or many times has the same effect. In payments it solves the problem of "safe retries": networks time out, clients resend, users click twice, but a charge must never be duplicated. Stripe's approach is for the caller to generate a unique idempotency key (Idempotency-Key) for each request. The first time the server sees a key, it executes the request normally and stores the result. Whenever it sees the same key again, it does not charge again; it simply returns the stored result unchanged.
- The note = a request sent by the client; Liu slipping in notes again and again = retries on an unstable network
- "Desk number + date" = the idempotency key (Idempotency-Key)
- The corkboard = the "processed keys + results" stored on the server
- Cooking one meal and charging once = the operation runs only once
- Handing over the same meal again = returning the cached original response to a duplicate request, rather than raising an error or ignoring it
- Fang cooking "every note he sees" = an endpoint with no idempotency protection, where retries turn straight into duplicate charges
Comprehension checks
- Understanding check: When Sister Chen gets a duplicate note, why doesn't she just ignore it, and instead "hands him the same answer again"? Mapped to a payment API: when a second request arrives with the same idempotency key, is the correct behavior to return an error, silently ignore it, or something else? Why?
- Transfer check: Message-queue consumers often receive the same message more than once. Give an example from your own work: what would you use as the "desk number + date" to turn an operation that is not idempotent into one that can be retried safely?