Why retrying a payment is dangerous — and the one line that fixes it.
A customer's connection drops mid-checkout, so their app quietly retries the charge —three times. Your naive code runs three times. The customer is charged 3× for one order, and now it's your 3am page. This exact bug has cost real companies real money. Let's make it impossible.
Idempotency: same request, same effect — no matter how many times.
An operation is idempotent when doing it once and doing it ten times land you in the same place. The trick: give each request a unique key, remember the keys you've handled, and make a repeat key a no-op. Retries become safe.
Here's the broken charge(). Run it — it charges three times. Then add the guard so a repeated key does nothing, and run again. Real Python, running right here.
Green means 3 retries → 1 charge. You didn't read about idempotency — you implemented it, and a check confirmed it. That's every Sage lesson: a mental model, a diagram, and a proof a skeptic can run. No self-graded quiz, no “I think I get it.”
That was one lesson. There are hundreds more.
Each one ends in a proof like this and stacks into a portfolio a hiring manager can run.