Digital Wallet: First Design
By minute ten you hold a rule that forbids half a transfer, and a relational database keeps that rule for free. So until minute twenty, the design is one transaction on one database. Nothing later in this problem is as simple, and everything later is a fight to get back what it gives you.
This lesson runs one transfer and its resend through that transaction, and says why it is exactly right. It ends on what a split would destroy. Then the interviewer pushes four times, and those pushes are the quiz.
One Transaction, One Database
Clients call stateless app servers through a load balancer. Behind them, one relational database holds two tables: accounts, each row with its balance, and transfers, each row with the client's ID, unique per sender.
A Transfer and Its Resend
Follow 25 dollars from one person to another. The server opens a transaction and locks the two account rows, always in order of account ID, so two opposite transfers cannot deadlock. If either account is missing or closed, fewer than two rows come back, and it refuses. It inserts the transfer row, debits the sender only if the balance covers the amount, credits the receiver, and commits.
BEGIN; SELECT id FROM accounts WHERE id IN (:a, :b) AND open ORDER BY id FOR UPDATE; -- fewer than two rows: refuse, stop INSERT INTO transfers (sender, id, receiver, amount, state) VALUES (:a, :id, :b, :amt, 'committed'); -- a repeat stops here, on the unique ID UPDATE accounts SET balance = balance - :amt WHERE id = :a AND balance >= :amt; -- no row changed: mark it 'refused', commit, stop UPDATE accounts SET balance = balance + :amt WHERE id = :b; COMMIT;
The locks come before the insert because the transfer row's foreign keys touch those same account rows, and locking after that can deadlock. The transfer row goes in before the debit for a reason too. Insert it first, and a resend stops at the unique ID and reads the first answer. Debit first, and a resend could meet the floor and tell someone their money did not move when it already had. A balance that cannot cover the amount marks the row refused and changes nothing else, and that commits too, so a resend hears the same refusal.
The rows are locked explicitly, so all of this holds at read committed, and no stricter isolation level is needed.
Now the crashes. A server that dies before the commit leaves nothing: the database rolls back, and the client's resend starts clean. A server that dies after the commit leaves a client with no answer, and the same resend finds the row and hears committed. A balance read is one row, and it shows only what has committed. An account's history is a query on the transfers table.
Why It Is Exactly Right
Three properties were agreed, and one commit gives all three. Both sides or neither, because one commit covers them. No balance below zero, because the debit checks as it writes. Nobody sees half, because nothing shows until the commit.
Against the 5,000 a second you assumed and offered to move, the ordinary peak of 2,000 fits with room. A young wallet ships this, and it should.
What a Split Will Destroy
Four limits stay on the board, unfixed. The holiday is four times the assumed limit, and as Chapter 3's When One Machine Is Not Enough says, writes cannot be copied away. One primary is one failure for 50 million accounts. The balance row keeps only today's number, so an as-of question means adding up transfer rows and trusting that nothing ever changed a balance without one. And a busy account makes every transfer to it take turns on one row.
Give the first of them before you are pushed, and name what you would watch: "Up to the 5,000 a second I've assumed, which you can move, an ordinary day fits. At the holiday's 20,000, commits start to queue, and since one commit covering both rows is what makes this correct, splitting it is the real problem."
- Apologising for one database, when it is the only design here with a simple proof of correctness. The interviewer hears someone who cannot tell a limit from a flaw.
- Locking the two rows in the order they arrive, so two opposite transfers deadlock.
- Giving the limit as "it won't scale" with no number, assumed or otherwise.
- The single transaction defended in the three properties the question needs.
- The 5,000 flagged as an assumption, offered to be moved, with the holiday set against it.
- The single commit named as the thing a split will destroy.
Knowledge Check
"Peak is four times what this database takes, so you shard by account. The two sides of a transfer now live apart, and you agreed nobody sees a half-done one. How does it commit?"
- "Debit into a pending hold on one shard, then credit the other, in two local steps."
- "Two-phase commit across both shards, inside the one store that holds all the accounts."
- "Debit one shard and credit the other as two writes, retrying the credit until it lands."
- "Partition by region instead, so that most transfers stay on one shard and stay local."
"The coordinator dies after both shards vote yes and before the decision goes out. What becomes of those two accounts?"
- "They stay locked until the decision is known, so the decision must outlive any one machine."
- "Each shard times out and aborts, which frees both rows so that the sender can retry."
- "The two shards ask each other, see two yes votes, and commit without the coordinator."
- "A standby coordinator starts the round again and collects a fresh vote from each shard."
"An auditor wants this account's balance as of last Tuesday at 14:00. Your row holds today's number. What do you build?"
- "A history table that gets a copy of every balance row at the end of each night."
- "A restore of last Tuesday's backup onto a spare cluster, read once for the auditor."
- "A routine that starts from today's balance and undoes each transfer since Tuesday."
- "An immutable log of each account's entries, folded up to the moment they asked for."
"On the holiday one merchant account takes 3,000 credits a second. What does that do to its row, and what do you change?"
- "Give that merchant a shard of its own, so that no other account competes with it."
- "Put a cache in front of its balance, so that the row takes far less of the load."
- "Split its credits over many sub-accounts, and add those up to read its balance."
- "Switch those credits to a saga, so that each one holds the row for less time."
You got correct