In everyday life
Like a lift call button: press it once or five times, and one lift comes. A coin slot is the opposite: every coin counts again.
One case
A customer double-clicks “Pay” for order 42, and the shop’s server receives two requests 300 milliseconds apart. Each request asks the payment service for a new $20 charge, so the customer pays $40. Had both requests carried the same idempotency key, the payment service would have made one charge and answered the second request with the first result.
Every case
Networks drop answers, users double-click, and jobs restart after a crash. So any action may arrive twice, and the sender often cannot tell whether the first try worked. An action that states the end result, such as “set order 42 to paid” in the shop’s own records, is safe to repeat. An action that states a change (“charge $20”, “create the folder”) stacks up or fails on the second run. A charge happens at the payment company, so the shop cannot make it safe on its own side: it sends an idempotency keyA unique id the sender attaches to one intended action, such as one payment. The receiving service does the action once and answers any repeat with the first result. with each charge, and the payment service makes only one charge per key.
The word
Engineers call this
Write each action so that a second run finds nothing left to do. Ask: what happens if this runs twice?