CHAPTER DEEP READ · System Design Interview Vol 2 · Ch 11

Design a Payment System

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

中文 →

What is this chapter about?

You tap "Pay now" in a shopping app, and a second later the page says "payment successful." This chapter builds what runs inside that second: it takes money from the buyer, and one day hands money to the seller — not a penny more, not a penny less.

One strange thing first

This system is remarkably slow. A million transactions a day works out to about a dozen per second — less traffic than a mid-sized forum. And yet it is the hardest chapter in the book to get right.

Because the difficulty was never speed. It is that "did the money move?" must have a definite answer.

Why the old way is hard

You press pay, the phone spins for thirty seconds, and a line appears: network error. Were you charged? You don't know. Neither does the server. The request may never have arrived — or the bank may have taken the money and only the receipt got lost on the way back.

The system now has two options. Treat it as failed, and the customer may be charged for nothing. Treat it as succeeded, and the merchant may ship for nothing. Worse, the customer will press the button again — and one purchase becomes two charges.

The mechanism, intuitively

① Give every payment a ticket number. However many times the same ticket is handed over the counter, the clerk does the work once and simply copies out the first receipt again. "I'm not sure it went through, so let me try again" turns from a dangerous act into a safe one.

② Write every amount down twice. Every movement of money lands on two accounts at once, one side down and the other up, and the whole book always sums to zero. Miss an entry and the sum gives it away — money is conserved by the shape of the bookkeeping, not by the care of the bookkeeper.

③ Compare books every night. Your record of the day, line by line, against the record the payment company sends you. Matches pass. Mismatches get pulled out: the ones that are just a fee can be fixed by rule, and whatever is left goes to a human. This step is not insurance — it is a necessity, because the first two tricks never cover everything.

④ Card numbers never enter your house. The card form belongs to the payment company; the number goes straight into their machines and you get back a stand-in code. What you cannot store, you cannot leak.

What it costs

All of this turns "paying" from a single act into a process with states: it can sit in "processing," it can land in a retry queue, and every day it leaves behind a small tail that a human must settle by hand — that inconvenience is the price of being correct.

One thing to remember

A payment system is absurdly slow and absurdly hard, because its only job is to make "did the money move?" have a definite answer. A timeout tells you neither success nor failure, so every payment carries a ticket number that is honoured once, which makes retrying safe again; every amount is written on two sides, so a missing entry cannot hide; and every night the books are compared against the payment company's, so whatever the first two missed gets pulled out and fixed by hand. The card number, meanwhile, is never let into your machines at all.

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