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

45.10 How do you keep it consistent?

3 min read+10 XP

Where strong consistency is required, and where it isn't:

  • Strong: the intent's state change and its ledger transaction are written in one database transaction (Chapter 17). Either both happen or neither.
  • Exactly once in effect: the outbox row for the merchant webhook is written in that same transaction, then published at least once (Chapter 23); merchants de-duplicate by event ID.
  • Eventual, and that's fine: dashboards, analytics, search over payments and merchant reports can lag by seconds or minutes.
  • Sharding: shard by merchant (or account) so a payment's intent and ledger lines live on one shard; cross-shard movements (rare: platform-wide accounts) use a saga with compensating ledger transactions, never a silent partial write.
  • Clocks: use database time or an ordered ID for ledger ordering, not server clocks.

Memory code: "State and money change together, in one transaction; everything else can catch up."