๐ Idempotency: cheat sheet
๐จ The analogyPressing a lift button five times still calls one lift: the system remembers each press.
๐ The regular explanationMaking it safe to repeat a request. Networks lose replies, so clients retry; idempotent operations and idempotency keys make sure a retry has the same effect as the first attempt, never a double charge.
- You press the button for a delivery lift that charges per trip. The lift is sent, but the light on the button doesn't come on.
- Not knowing it worked, you press again. A careless lift sends a second trip, and you pay twice.
- A good lift remembers each press by a ticket number. You press, with ticket 7. The lift goes once and notes ticket 7.
- You press again with the same ticket. The lift sees ticket 7 already done and just turns your light on. One trip, one charge.
- A new trip gets a new ticket number, so real repeat orders still work.
- The post room delivers order m1. The clerk does it, and ticks m1 off the done list, together.
- The clerk's "got it" goes missing, so the post room delivers m1 again, just to be safe.
- The clerk checks the done list, sees m1, and simply says "got it" again. Nothing is shipped twice.
- Your finger slips and the same press, with the same ticket number, reaches the lift twice at the very same moment.
- Both presses ask the list "has ticket k1 gone?" and both hear "no". The first one to write the ticket on the list wins it.
- The second press tries to write the same ticket and the list refuses, since the number is already there. No second trip is sent.
- The first trip is still being charged, so the lift tells the second press to come back later. Nothing is sent twice, but the second press has to wait.
- The first trip finishes, and the second press tries again after a short pause. The lift hands back the same answer, so both lights come on. The second press paid a delay and an extra trip to the lift, but the trip happened once.
๐งฉ Key questions
- The lift went but the light didn't come on, so you press again. What does a careless lift do?
- Sends a second trip, and you pay twice. You can't tell whether it worked, so pressing again is natural.
- How does the smart lift know a press is a repeat?
- Each press carries a ticket number, and a repeat ticket means it already went. The same ticket again just turns your light on; a new trip gets a new ticket.
- The post room clerk's "got it" goes missing, and the letter arrives again. What does the clerk do?
- Finds it on the done list and just acknowledges it. Anything already on the done list is just acknowledged.
- Two presses with the same ticket reach the lift at the same moment, and both hear "not gone yet". What stops two trips?
- The list refuses to take a ticket that is already on it, so only one press wins. Checking and then writing leaves a gap; writing first closes it.
- The lift writes the ticket on the list, then loses power before the trip. What goes wrong when you press again?
- No trip ever comes: the lift sees the ticket and thinks the trip already went. The ticket and the trip must go in the book together.