Lesson 7 of 16
45. Design a Payment System (Stripe / UPI)

45.6 How does idempotency work end to end?

3 min read+10 XP

Step 5: Deep dives (10 minutes)

Chapter 24's double-charge problem, applied at every hop:

  • Merchant → us: an Idempotency-Key per intended payment; the intents table has a unique constraint. In the chapter's code, a retried "pay ₹1,248 for order 881" returns the same intent, and there's still one.
  • Us → bank: every attempt carries our attempt reference; banks and networks de-duplicate on it, so our retry after a timeout doesn't debit twice.
  • Bank → us: callbacks carry the bank's reference; we apply each once.
  • Inside us: ledger transactions have deterministic IDs (capture:pi_1, refund:pi_1), unique in the ledger, so recording the same event twice is a no-op. In the chapter's code, a second refund post returns "duplicate".

Memory code: "A key at every hop, and every effect recorded under a name that can't repeat."