AlgoViz

Design a Digital Wallet

hard 45 min

Let 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

0:00 / 4:18

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

  1. 1494

    Luca Pacioli's Summa de arithmetica is printed in Venice. It includes a description of double-entry bookkeeping as merchants used it.

  2. 2007

    M-Pesa starts in Kenya: money moves between phones without bank accounts.

  3. 2009

    Venmo starts, says its own page: payments between friends.

  4. September 2016

    Same Day ACH begins in the United States, for credits first.

  5. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 4

Ask. The app sends amount, receiver and one key that every retry repeats

Understand it

Jars and a book
Asha5Lee2

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

ProductKind
PayPal, AlipayWallet and shop payments
Venmo, Cash AppPayments between people
M-PesaPhone wallet with shops as cash points
Apple Pay, Google PayPay with a stored card; the money stays at the bank
ZelleFrom bank account to bank account
StripePayments for businesses
Only some of them keep a balance for you. Ask which kind you are asked to design.

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

The wallet app and shops call the gateway, which calls the wallet service. The wallet service asks the fraud check and writes to the ledger in PostgreSQL, which has a synchronous standby in another zone and read replicas. A bank worker and reconciler exchanges files and statements with the partner bank, books the results in the ledger and compares the two. An outbox sends events to Kafka for notifications and search.
Blue is asking, pink is moving money, orange is events and bank traffic. Dashed arrows do not delay a transfer.

Where to read what

QuestionPlace
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

IdeaPage
All or nothingACID
Safe repeatsIdempotency
Locks and versionsConcurrency control
Steps that undoSaga
Events from the databaseChange data capture
Calls that tell you laterWebhooks
Correct or availableIntegrity vs uptime
Scoring paymentsFraud 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