Idempotency Explained: How Payment APIs Prevent Duplicate Charges
Idempotency: Why a Retried Payment Can't Charge Twice
Picture a user tapping Pay on a train with bad signal. The request reaches your server, the card is charged, and then the response gets lost on the way back. The app sees a timeout and does the sensible thing: it retries.
Without idempotency, that retry charges the card a second time.
The mental model
Pressing the lift button five times still calls one lift.
An operation is idempotent when doing it twice has the same effect as doing it once. The first press changes the world. Every press after that changes nothing, because the world is already in that state.
How it works
Some operations are idempotent by nature. "Set the email to a@b.com" gives the same result no matter how many times it runs. "Delete order 42" leaves order 42 gone, whether it ran once or three times.
Others are...
Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE