Book Deep Reads · System Design Interview Vol 2 · Chapter 12

Digital Wallet

System Design Interview Vol 2 · Ch 12 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

You send a friend $1 inside a payments app, or hand a teammate some in-game gold. This chapter is about how that transfer gets written into the books. Its central problem is counter-intuitive: the hard part isn't moving the dollar quickly — it's still being able to explain, a year later, exactly why that dollar left.

An analogy first

Think of a wallet as a paper ledger. There are two ways to keep one.

The first is to write down a single number: you have 100, you send 1, so you erase the 100 and write 99. Compact, instantly readable — and whatever you erased is gone forever.

The second is to only ever add lines: "Mar 1, sent 1" / "Mar 2, received 5" — and simply never write a balance at all. To find out what you have now, you add the lines up. Wordier, but the balance on any past day can be recomputed, and everyone who adds it up gets the same answer.

Where this chapter ends up is: replace the first ledger with the second.

Why the old way is hard

Two things are hard at once.

First, a transfer has to happen "at the same time": A loses the dollar and B gains it, and those two strokes must count as one action. What if the power cuts between them? And the two accounts usually don't even live on the same machine.

Second, money gets questioned. When the books don't balance, when a customer disputes a charge, when a regulator asks — you must be able to say what the balance was at that instant and which entries made it so. A ledger holding one number can't answer that. It remembers the present, not the reason.

The mechanism, intuitively

The chapter solves this in three moves.

One: make "at the same time" real. It works like reserving a restaurant table — first put a hold on A's dollar (still A's money, but unspendable), then reserve a slot on B's side, and only once both sides agree does it settle. If anyone backs out, release the hold and drop the reservation. Holding a table isn't dinner, but at least two parties never sit down in it at once.

Two: swap "erase and rewrite" for "only add lines." Every entry is a line that is never modified. The balance stops being something you store and becomes something you compute. So you don't re-add from day one every time, you take a "snapshot" now and then and only add the lines after it.

Three: don't lose the ledger. The same ledger is copied to three machines at once, and every line counts as written only after at least two of the three nod. Lose any one machine and the other two hold an identical book — just keep writing.

How to choose

If what flows through your system is money, or inventory, or anything that will be questioned after the fact, this way of keeping books is worth the trouble. The cost is real too: the ledger only grows, storage grows with it, and a mistake can't be edited — only cancelled out by another line. For a team used to "just fix that row in the database," that's a real adjustment.

One line to remember

Where a digital wallet ends up is this: the balance stops being a number that gets rewritten and becomes the sum of a list of lines that are never rewritten. Precisely because it is computed, it can be recomputed to prove itself — and copied to several machines without drifting.

Want the mechanisms, notation and diagrams? → Switch to the deep read