SDE & MLE Prep

Digital Wallet (Balance Transfers)

Requirements: users hold balances and transfer money to each other; no money created or lost, every transfer applied exactly once, full audit trail; high throughput (target ~1 M transfers/s at peak in Alex Xu's version).

transfer(A -> B, $5, idempotency key)
  -> sequencer / partition by account -> command log (replicated with Raft)
  -> state machine: validate (A has $5?) -> emit events "A -5", "B +5" -> apply to balances (in memory + snapshots)
  -> read side (CQRS): balances, statements, reconciliation

Deep dives

  • Sharding accounts across many databases makes a transfer a distributed transaction when A and B live on different shards: options are 2PC (blocking), TCC (try: freeze A's $5, confirm/cancel on both sides) or a saga with compensation.
  • Event sourcing (the reproducible design): store commands and resulting events in an append-only log; current balances are a deterministic replay of the log. Gives a perfect audit trail, easy recovery (snapshot + replay) and the ability to rebuild read models.
  • Exactly-once: client idempotency keys, dedupe table keyed by request id inside the same transaction as the balance change.
  • Reliability: replicate the event log with Raft across zones; balance state is derived, so any replica can rebuild it.
  • Reconciliation with the external ledger/bank daily; double-entry bookkeeping (every transfer = debit + credit summing to zero).