Design a Digital Wallet
0%
Editorial Solution
System requirements
Functional
Capacity estimation
Traffic
API design
Database design
PostgreSQL: Core Financial Data
High-level design
Client Layer
Request flows
P2P Transfer Flow
Detailed component design
Double-Entry Ledger
Trade offs/Tech choices
Strong Consistency vs Performance
Failure scenarios/bottlenecks
Partial Transfer Failure
Future improvements
Multi-Currency Support
Table of Contents
System requirements
Functional
Capacity estimation
Traffic
API design
Database design
PostgreSQL: Core Financial Data
High-level design
Client Layer
Request flows
P2P Transfer Flow
Detailed component design
Double-Entry Ledger
Trade offs/Tech choices
Strong Consistency vs Performance
Failure scenarios/bottlenecks
Partial Transfer Failure
Future improvements
Multi-Currency Support
Here is the single most important principle for this entire design: money cannot be created or destroyed. Every dollar that enters the system must be accounted for, every transfer must have a matching debit and credit, and every failure must either complete or fully reverse. This principle (borrowed from 500 years of double-entry bookkeeping) shapes every decision we make.
Most system design problems optimize for throughput or latency. A digital wallet optimizes for correctness. Get the feed algorithm wrong at Instagram and users see slightly less engaging content. Get the balance calculation wrong at a wallet and real money vanishes. Keep this framing in mind throughout.
Functional
Let us anchor these numbers to a real-world reference: PayPal processes roughly 40 million transactions per day. We will use similar scale to estimate our infrastructure needs.
Traffic
- Users: 100M registered, 30M monthly active users
- Transactions: roughly 40M/day = roughly 500 TPS average, roughly 5,000 TPS peak (Black Friday, flash sales, end-of-month bill payments)
- Double-entry writes: each transaction creates 2 ledger entries + 2 balance updates = roughly 1,000 balance writes/sec average, 10,000 peak. This is the number that matters for database sizing. Each transaction is not 1 write but 4+ writes.
- Balance reads: checking balance is far more frequent than transacting. Users open the app, check balance, close the app. Estimate 10x transaction volume = 5,000 QPS average for balance checks. This is the number that justifies a Redis cache layer in front of PostgreSQL.
- Fraud scoring: every transaction hits the fraud service synchronously. At 500 TPS, the fraud service must sustain 500 evaluations/sec with p99 latency under 20ms to keep within our 100ms total budget.
All endpoints require authentication via JWT token in the Authorization header. An API Gateway handles rate limiting (critical for preventing rapid automated drain attacks on compromised accounts) and request routing. Every mutation endpoint accepts an Idempotency-Key header. This is non-negotiable for a payment system.
Wallet
POST /api/wallets/deposit, Add money from linked bank/card. Body:{ source_id, amount, currency }. Header:Idempotency-Key: <uuid>. Initiates a charge on the linked account via the payment gateway. Returns a transaction record with status PENDING until the gateway confirms.POST /api/wallets/withdraw, Withdraw to linked bank account. Body:{ destination_id, amount, currency }. Header:Idempotency-Key: <uuid>. Debits the wallet balance and initiates a bank transfer. Settlement typically takes 1-3 business days.GET /api/wallets/balance, Current balance and currency. Returns from Redis cache when available, falls through to PostgreSQL on cache miss. This is a display-only read. Never used for deduction decisions.GET /api/wallets/transactions?cursor={lastId}&limit=20, Paginated transaction history. Cursor-based, not offset-based, because new transactions constantly push older ones down. Each entry includes transaction_id, type, amount, status, counterparty, and timestamp. Filter support:?type=P2P&status=COMPLETED&from=2026-01-01.
Financial systems have a non-negotiable constraint: ACID transactions for balance updates. This rules out eventually consistent NoSQL databases for core wallet data. PostgreSQL is the backbone. It provides transactions, foreign keys, and the referential integrity that financial systems demand.
PostgreSQL: Core Financial Data
wallets: one row per user, the source of truth for balance
The system uses a service-oriented architecture where each service owns a specific domain. Unlike Instagram where the Feed Service might need 10x more instances during peak hours, a wallet system's core challenge is not raw throughput but transaction integrity. Every service on the critical path must preserve the "money cannot be created or destroyed" invariant.
Client Layer
P2P Transfer Flow
This is the most instructive flow because it exercises every consistency mechanism in the system: idempotency, fraud detection, distributed locking, double-entry bookkeeping, and cache invalidation. If you can explain this flow clearly in an interview, you demonstrate mastery of the entire design.
Double-Entry Ledger
This is a 500-year-old accounting principle invented by Luca Pacioli in 1494, and it remains the foundation of every modern financial system, from local banks to PayPal to blockchain ledgers. The rule is deceptively simple: every transaction creates exactly two ledger entries. A debit and a credit. And total debits must always equal total credits across the entire system.
Strong Consistency vs Performance
For wallet balances, strong consistency is non-negotiable during deductions. You cannot show a user $100 and then reject their $80 purchase because the actual balance is $50. This means every deduction reads from PostgreSQL (not the Redis cache), acquires a distributed lock, and executes within an ACID transaction.
The performance cost is real and measurable: a Redis cached read takes under 1ms, while a PostgreSQL read with distributed lock takes roughly 30ms. That is a 30x latency increase per deduction. But this cost applies only to balance-modifying operations (transfers, payments, withdrawals), roughly 500 TPS. Balance display, transaction history browsing, and analytics queries can all read from Redis caches or PostgreSQL read replicas with eventual consistency, roughly 5,000 QPS served from cache.
Partial Transfer Failure
The most dangerous failure mode in a wallet: the sender is debited but the recipient credit fails. Without proper safeguards, money vanishes. It left the sender's wallet but never arrived at the recipient's.
Mitigation for internal transfers: both operations (debit sender, credit recipient, two ledger entries, transaction record) execute in a single PostgreSQL BEGIN...COMMIT transaction. If the credit INSERT fails for any reason (constraint violation, disk full, process crash) the entire transaction rolls back atomically, including the debit. The sender's balance is unchanged. Nothing happened. This is the fundamental reason ACID transactions are non-negotiable for financial systems.
Multi-Currency Support
Currently the wallet supports a single currency per user. Multi-currency support extends the system to let users hold USD, EUR, GBP, JPY, and other currencies simultaneously in separate currency wallets.
The key additions: real-time FX rate feeds from providers like Reuters, Bloomberg, or the ECB, rate locking at transaction initiation so the rate shown to the user is the rate applied even if the market moves during the 100ms processing window, cross-currency transfer with transparent fee display (users see "Send $100 = 85.50 EUR, fee: $1.50" before confirming), and currency-specific regulatory compliance (some currencies have transfer restrictions).
Try this problem yourself
Reading a solution is not the same as producing one under interview pressure. Design it on the whiteboard and get scored feedback.
0%
Editorial Solution
System requirements
Functional
Capacity estimation
Traffic
API design
Database design
PostgreSQL: Core Financial Data
High-level design
Client Layer
Request flows
P2P Transfer Flow
Detailed component design
Double-Entry Ledger
Trade offs/Tech choices
Strong Consistency vs Performance
Failure scenarios/bottlenecks
Partial Transfer Failure
Future improvements
Multi-Currency Support
Table of Contents
System requirements
Functional
Capacity estimation
Traffic
API design
Database design
PostgreSQL: Core Financial Data
High-level design
Client Layer
Request flows
P2P Transfer Flow
Detailed component design
Double-Entry Ledger
Trade offs/Tech choices
Strong Consistency vs Performance
Failure scenarios/bottlenecks
Partial Transfer Failure
Future improvements
Multi-Currency Support