The order worked. The app retried. Two charges.
transcript
You tap Place order once. You get charged twice. And the first order had worked.
Flaky 4G on the way home. The app sends your order. 5 seconds pass. The app gives up. At 5.2 seconds, the server commits it. Too late. The app retries. A second order. A second charge.
Why retry blindly? Because the app can't know what happened to the first one. Lost, half done, or done with the reply lost. All look the same.
The fix: an idempotency key. One per order. The app makes it once, and every retry reuses it. The server checks: seen this key? Yes? Skip the work. Send back the first order.
But here's the catch. Same key, same cart? Replay the first answer. Same key, different cart? Rejected. That's a client bug. A changed cart is a new operation. It gets a new key.
Timeouts will happen. Retries will happen. Send the same key, and a retry gets the first order back.
Full breakdown in the lecture.