Payment System: First Design
Minutes ten to twenty put one real charge on the board, in a design plain enough to watch it work. The server calls the processor, waits, and writes down what it heard. Each night a job pays the sellers. Nothing else runs.
A charge and a payout fill these minutes. The charge stops at the moment the call can time out, and you want the interviewer looking right there. The quiz that follows is what they say next, so reply to each one in your own words first.
A Server, a Database and an Outside Party
The buyer's browser gets a card token from the processor, so the card number never reaches you, and posts checkout through a load balancer to the app servers, which keep no state. One relational database holds orders with a status column. The processor is a box outside your system, and a nightly worker sits beside the database.
One Charge, One Payout
Take a 100-dollar order. The server sends the token and the amount to the processor inside the request, and waits. A yes sets the status to paid, a no sets it to failed, and the buyer hears which on the same connection. A refund is the same kind of call, and it sets the status to refunded.
That call has three endings. The processor says yes, the processor says no, or, after an assumed 10 seconds, the server stops waiting and has heard nothing. The first two are answers. The third is where the walk stops, because the status column has no true value to take. Point at it, and leave it there for now.
Then the payout. Each night the job sums each seller's paid orders since the last run and takes off the commission. It calls the processor's payout with what is left, and stamps those orders paid out. By morning the payouts are on their way, and the orders say so.
Why You Could Launch on This
This charges buyers and pays sellers, which is all a launch needs. There is one request a person can watch, one column of state, and nothing else to run. At 100 payments a day, a timed-out call turns up every ten days, and a person looks it up on the processor's dashboard. A marketplace's first year can run on it, and the interviewer should hear that from you before any of what it cannot do.
What the Column Cannot Say
The design has four limits, and you leave all of them standing. A timeout leaves the status with no true value. A retry charges again, because nothing ties it to the first call. The column says where an order is, not what moved, so a balance has no explanation, and a refund after the payout has nowhere to go. And a job that dies halfway has paid some sellers and may pay them again.
Put the limit at a count of unknowns, and put it there yourself: "This works at a hundred payments a day, because someone can chase the odd timeout by hand. At eight million a day that's eight thousand unknown charges a day, so what a timeout means has to change."
- Opening with a ledger and an orchestrator before one card has been charged on the board.
- Marking a timed-out payment failed in the walkthrough, then hearing the interviewer ask whether the money moved.
- Treating the nightly payout as an afterthought, when it is where the money leaves for good.
- One charge and one payout walked through, with a stop at the moment the call can time out.
- What the design gets right at launch, said before what it cannot do.
- The limit put at a count of unknowns a day, not at a request rate.
Knowledge Check
"You call the processor, and ten seconds later the call times out. Did the charge happen, and what does your code do next?"
- "It did not, so I mark the payment failed and let the buyer try the card again."
- "It probably did, so I mark the order paid and let tonight's check catch any misses."
- "I can't know yet, so I record the outcome as unknown and find out before acting."
- "I can't know yet, so I send the charge again at once until a clear answer comes."
"The buyer's phone lost signal, the app sent the checkout again, and the card now shows two charges. What was missing?"
- "A disabled pay button in the app, so that the checkout cannot be submitted twice."
- "A search of recent charges for the same card and amount before every new charge."
- "A look at the order's status column first, to skip the charge if it already says paid."
- "One key per payment attempt, kept with the payment, sent on every call to the processor."
"Finance asks why this seller's balance is 4,312 dollars. You have a number in a column. Where does the explanation come from?"
- "From balanced entries that are never edited, with the balance computed by adding them."
- "From an audit table that a trigger fills each time the balance column is changed."
- "From the orders table, recomputing what the seller earned each time finance asks."
- "From a nightly snapshot of every balance, so that any two days can be compared."
"A million sellers get paid every night. Tell me how that run works, and what happens when it dies halfway."
- "One nightly job loops over the sellers and records the last one it paid, then resumes."
- "One queued payout per seller, each with its own key, sent by workers at a set rate."
- "One payout per sale as each sale settles, so that no nightly run exists to die at all."
- "One database transaction around the whole run, which rolls back if anything fails."
You got correct