Design a Digital Wallet
hard 45 minLet 50 million people keep money in an app and send it to each other, at 174 transfers a second.
Video lesson · Chapter 1 of 6
Send fifty dollars
A digital wallet in 90 seconds
1:27Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A digital wallet holds a balance for each person and moves money between balances. A transfer inside the wallet moves no money at any bank: it changes who owns a part of the money that the company keeps at its partner bank.
The heart is a ledger: a list of entries that is only added to. Every movement is written at least twice, once where money leaves and once where it arrives, and the amounts of one movement add up to zero.
Why it exists
People want to pay a friend now, while the banking system may take days. A shared ledger makes it instant, as long as both have a wallet.
The price is care. A lost chat message is a nuisance; a lost or doubled payment is somebody's rent. So correctness comes before speed here, and every number must be explainable years later.
A short history
1494
Luca Pacioli's Summa de arithmetica is printed in Venice. It includes a description of double-entry bookkeeping as merchants used it.
2007
M-Pesa starts in Kenya: money moves between phones without bank accounts.
2009
Venmo starts, says its own page: payments between friends.
September 2016
Same Day ACH begins in the United States, for credits first.
20 July 2023
The FedNow Service goes live: instant payments between banks in the United States.
Words to know
- Ledger entry
- One line: account, signed amount, transaction. Never changed.
- Double entry
- The entries of one transaction add up to zero.
- Idempotency key
- A name for one intention, sent again with every retry.
- Hold
- Money set aside: moved into a pending account.
- Settlement
- The moment money really moves between banks.
- Reconciliation
- Comparing our ledger with the bank's statement, line by line.
- ACH
- The network for bank transfers in the United States, which works in batches.
- Reversal
- New entries that undo old ones. The old ones stay.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Ask. The app sends amount, receiver and one key that every retry repeats
Understand it
Marbles only move between jars. The book can tell the story of each one.
Scope
What are we building?
A wallet for one country: 50 million people, transfers between them, payments to shops, and money coming from and going to banks. Amounts are whole cents in one currency.
Never lose
A confirmed transfer survives a failing machine.
Never double
Five taps on Send are one payment.
Always add up
The sum of all entries is zero.
Wait well
Banks answer days later, or twice, or not at all.
Similar products
| Product | Kind |
|---|---|
| PayPal, Alipay | Wallet and shop payments |
| Venmo, Cash App | Payments between people |
| M-Pesa | Phone wallet with shops as cash points |
| Apple Pay, Google Pay | Pay with a stored card; the money stays at the bank |
| Zelle | From bank account to bank account |
| Stripe | Payments for businesses |
The finished system
Where the design ends up
The last version in one region. The Design tab starts with one service and one database.
The system
Where to read what
| Question | Place |
|---|---|
| How do I open this interview? | Approach |
| How big is it? | Numbers |
| How does it grow? | Design |
| Ledger, retries, banks, fraud? | Deep dives |
| Can I practise it? | Mock |
The ideas and their own pages
| Idea | Page |
|---|---|
| All or nothing | ACID |
| Safe repeats | Idempotency |
| Locks and versions | Concurrency control |
| Steps that undo | Saga |
| Events from the database | Change data capture |
| Calls that tell you later | Webhooks |
| Correct or available | Integrity vs uptime |
| Scoring payments | Fraud detection |
Summary
The whole topic in one minute.
One database transaction checks the balance, writes both entries, updates both balances and stores the idempotency key. Either all of it happened or none of it. Retries find the key and get the first answer.
Money that is on its way is in an account too. So a withdrawal is three small transfers over days, and at any moment the sum of all accounts is zero. The bank's statement is compared every day, because the bank is the only witness outside.
- Every movement is two lines that add up to zero.
- The ledger is the truth; the balance is a sum we keep ready.
- The key is stored in the same transaction as the money.
- Money on its way has an account of its own.
- Repair with new entries, never with edits.
Sources
- Stripe: Ledger, tracking and validating money movement
- Square: Books, an immutable double-entry accounting database
- TigerBeetle: two-phase transfers
- Stripe API: idempotent requests
- PostgreSQL: explicit locking and deadlocks
- Nacha: Same Day ACH
- Federal Reserve: about the FedNow Service
- 31 CFR 1010.430: records are kept for five years