System DesignSep 15, 2026⏱️ 3 min read
Design a Digital Wallet: 2PC vs TCC vs Saga, Event Sourcing & Raft Consensus

Design a Digital Wallet: 2PC vs TCC vs Saga, Event Sourcing & Raft Consensus

Scaling Peer-to-Peer Wallet Transfers to 1 Million TPS

When Alice transfers $100 from her digital wallet to Bob's wallet across sharded account databases (Alice lives on Shard A, Bob lives on Shard B), a single-node SQL BEGIN; UPDATE ... COMMIT; cannot span two distinct servers atomically without distributed coordination!
Digital Wallet Event Sourcing & Raft Architecture

Digital Wallet Event Sourcing & Raft Architecture


1. Distributed Transactions Compared: 2PC vs. TCC vs. Saga

PatternHow It WorksLock ContentionBest Use Case
Two-Phase Commit (2PC)Coordinator runs Prepare (acquires X-locks on all shards) → Commit❌ Holds locks across network RPCs; stalls on coordinator crashLow-TPS legacy cross-DB monoliths
TCC (Try-Confirm-Cancel)Phase 1: Try reserves balance (frozen += $100); Phase 2: Confirm transfers or Cancel releasesShort business-level reservation; no long DB row locksCross-shard wallet transfers requiring strict invariants
Saga PatternSequential local transactions + Compensating Undo actions on failureZero cross-shard locks (eventual consistency)Long-lived multi-service workflows (Checkout → Shipping)

Why TCC Shines for Wallets

In Try, Shard A atomically deducts available funds into a pending hold (available -= 100, pending += 100) while validating Bob's account exists on Shard B. Only after both Try phases persist an outbox/transaction log does Confirm finalize balances—ensuring Alice can never double-spend!

2. Reproducibility & Auditability: Event Sourcing + CQRS

In banking systems, auditors don't just ask "What is Alice's balance today?"—they ask "Prove Alice's balance was $420.00 at 3:14 PM last Tuesday and trace every cent."
Event Sourcing flips database design inside out:
  • Source of Truth = Immutable Command/Event Log: Never overwrite balances blindly. Record every immutable transaction event (WalletCredited, WalletDebited) sequentially in an append-only log (e.g., Kafka / RocksDB).
  • Deterministic State Machine: Replaying events from offset 0 through the state machine deterministically reproduces the exact balance at any timestamp.
  • CQRS Read Views & Snapshots: Take periodic balance Snapshots (offset_100000 -> $420) so recovery replays only recent delta events in milliseconds rather than scanning years of history!

3. Pushing to 1 Million TPS: In-Memory State Machines + Sharded Raft

Even Redis + SQL disks face network RTT bottlenecks at 1M TPS. High-frequency wallet cores borrow from consensus systems:
  1. Partition wallets across $N$ Sharded Raft Groups (Node Leader + 2 Followers per shard).
  2. The Shard Leader runs Event Sourcing entirely in memory / embedded RocksDB (1M+ ops/sec), replicating every ordered event via Raft Consensus before acknowledging!
  3. If the Leader dies, Followers hold a strictly identical replicated command log and elect a new Leader in milliseconds with zero lost cents.

Share this article

Comments