TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Idempotency

๐Ÿ”˜ 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.
The lost replyStep 2 of 2
๐Ÿง’You๐Ÿ›—Lift control๐Ÿ“’ Lift logยฃ40 charged!
  1. 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.
  2. Not knowing it worked, you press again. A careless lift sends a second trip, and you pay twice.
Pressing the button twiceStep 3 of 3
Pressed buttonsk7key k7 โ†’ 201, payment 981: k70k8k8โœฆโœฆโœฆโœฆโœฆ123๐Ÿง’You๐Ÿ›—Lift control๐Ÿ“’ Lift logยฃ20 once
  1. A good lift remembers each press by a ticket number. You press, with ticket 7. The lift goes once and notes ticket 7.
  2. You press again with the same ticket. The lift sees ticket 7 already done and just turns your light on. One trip, one charge.
  3. A new trip gets a new ticket number, so real repeat orders still work.
The same message twiceStep 3 of 3
Done listm1m10123๐Ÿ“ฎPost room๐Ÿง‘โ€๐Ÿ’ผClerk๐Ÿ“’ Order book
  1. The post room delivers order m1. The clerk does it, and ticks m1 off the done list, together.
  2. The clerk's "got it" goes missing, so the post room delivers m1 again, just to be safe.
  3. The clerk checks the done list, sees m1, and simply says "got it" again. Nothing is shipped twice.
Two copies race for one keyStep 5 of 5
Pressed buttonsk1key k1 (in progress): k10123๐Ÿง‘First press๐Ÿง‘Second press๐Ÿ›—Smart lift๐Ÿ“’ Lift log
  1. Your finger slips and the same press, with the same ticket number, reaches the lift twice at the very same moment.
  2. 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.
  3. The second press tries to write the same ticket and the list refuses, since the number is already there. No second trip is sent.
  4. 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.
  5. 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.