Payment System: Scope
Eight million payments a day sounds like a load problem, and within the first ten minutes you show that it is not. These minutes find out whether you hold the money, what the processor promises, and how many calls a day end with nobody knowing the result. That last count is what the design follows.
Six Questions for the Interviewer
Each answer moves something on the board. The agreed answer follows each one, and the rest of the chapter uses it.
- How many payments a day, and how spiky? This sets whether throughput matters at all. Agreed: 8 million a day, with a sale day's peak at five times the average.
- Does the platform hold the money between the buyer and the seller? A yes means the platform owes sellers money, which takes a ledger and not a status. Agreed: yes, and sellers are paid nightly.
- Does the processor take an idempotency key, and how long does it honour one? A key is a name you send with a request, so that a repeat counts as the same request. The answer sets how long a retry is safe. Agreed: it does; assume 24 hours.
- How does the processor tell you what really settled? Settled means the money actually reached you. The answer sets what the ledger is checked against. Agreed: a daily settlement file.
- One currency and one processor? More of either adds accounts and a routing rule. Agreed: one of each for now, and both come back as curveballs.
- How long must the money records be kept? This sizes the ledger. Agreed: 7 years.
The second and third answers move the most, and neither is yours to assume. Holding the money turns a status into a debt to a million sellers. The key's 24 hours is a horizon the whole design has to work inside. So put the question before you draw any retry: "I need to know what the processor promises about a repeated call, and for how long, because my retries are only safe inside that."
What the Design Must Do
The jobs are four: charge a buyer for an order, refund, pay out each seller's balance nightly, and explain any balance, line by line.
The promises are stricter than the jobs. A buyer is charged at most once for one order, whatever fails. No charge is ever lost track of. No money record is ever edited. And every difference from the processor's files is found within a day.
Out of scope, and you say it: fraud scoring, tax, seller onboarding, the licences for holding sellers' money, and card numbers. Card numbers go from the buyer's browser straight to the processor and come back as a token, a stand-in that only the processor can use.
The Rate, and the Numbers That Beat It
Take the rate first, to get it out of the way. Eight million a day over about 100,000 seconds is 80 a second. A sale's peak of five times is 400 a second. Visa averages about 8,000 transactions a second, so your peak is a twentieth of a card network's ordinary load. The payment rate decides nothing, and you say so.
Now the number that shapes the design. Assume 1 call in 1,000 ends in a timeout or a dropped connection. That is 8,000 payments a day whose result nobody here knows. At launch, at 100 payments a day, it is one every ten days. The rate of failure is the same, and the design it asks for is completely different.
The ledger sizes the store. A ledger line is one row recording an amount for one account. Assume 5 lines a payment, three when charged and two when settled, at about 200 bytes each. That is 40 million lines a day, and 8 gigabytes. A year is just under 3 terabytes, and 7 years is about 20. Three copies make about 61 terabytes. Assume 2 terabytes a node, and that is about 31 nodes.
The writes do not compete with that. A key-value partition takes about 1,000 writes a second, and you run it at half. At peak, 2,000 lines a second need only 4 partitions, so the bytes decide. Both of these sums count lines alone, and Scaling It adds the rest.
The payout run is the other number with a deadline. Take a million sellers. Assume the processor allows 100 payout calls a second, and the run keeps to half. At 50 a second, a million takes 20,000 seconds, about five and a half hours. Assume a six-hour window. It fits, and it is tight.
One sentence covers bandwidth: 400 calls a second is nothing on any link.
API and Entities
A POST to payments carries the order, the amount, a card token and a client key, a value the app makes once for each checkout attempt. It returns a payment with its state: succeeded, failed or processing. A GET on a payment's ID reads it, for the buyer who owns it, and that ID starts with the order's ID for a reason Scaling It gives. A POST to refunds names a payment and an amount, with a client key of its own. A webhook endpoint takes the processor's reports, signed so that nobody else can send one. Amounts are whole cents.
POST /payments
{"order": "o8841", "amount_cents": 10000,
"card_token": "tok_51Hx",
"client_key": "0195e2c4-7a10-7c3e-9d21-5f8a6b2e4c01"}
201 {"id": "o8841-a1", "state": "processing"}
state: succeeded, failed or processing
GET /payments/o8841-a1
200 {"id": "o8841-a1", "state": "succeeded"}
POST /refunds
{"payment": "o8841-a1", "amount_cents": 10000,
"client_key": "0195e2c9-1b44-7d02-8a6e-3c7f1d90ab12"}
202 {"id": "o8841-a1-r1", "state": "processing"}
POST /webhooks/processor
signed by the processor
200
There are four entities: a payment, a payout, an account, and a journal entry made of lines. How those lines work waits for the probe that needs them.
- Sizing for a card network's load when the marketplace is a twentieth of its average. The interviewer waits for the failure question while you draw partitions.
- Leaving "do you hold the money?" unasked, then drawing a status column for a debt to a million sellers.
- Not asking how long the processor honours a key, then promising retries that are no longer safe after that time.
- Throughput ruled out loud as not the problem, with the comparison that shows it.
- A small failure rate turned into 8,000 a day, and the design following from that count.
- What the outside party promises, asked before anything is designed around it.
Knowledge Check
Which clarifying question changes the design the most?
- Does the platform hold the money between buyer and seller?
- How many payments a day, and how high does a sale day go?
- Which card networks and kinds of card do buyers mostly use?
- How long must the money records be kept for an audit?
Eight million payments a day, and 1 call in 1,000 ends in a timeout or a dropped connection. What does that give you?
- 800 a day whose result nobody here knows, a small daily queue
- One every ten days, and a person chases each of them by hand
- 8,000 a day whose result nobody here knows, too many for a person
- 8 a second on top of the traffic, for the servers to absorb
What can you safely rule out of scope, out loud?
- Refunds, since they are rare and support can do them by hand
- Timeouts, since the processor answers nearly every single call
- Payouts, since the money has already reached the platform
- Fraud scoring, tax, onboarding, licences and the card numbers
Which estimate can you state in a breath and leave, because no decision hangs on it?
- The unknown outcomes, 8,000 a day at 1 call in 1,000
- The payment rate, 400 a second at a sale's peak
- The ledger's bytes, about 61 terabytes with three copies
- The payout run, about five and a half hours at 50 a second
You got correct