Lesson 4 · Minutes 30-45

Payment System: Deep Dives and Curveballs

Problem

You said a key makes a retry safe, and from minute thirty the interviewer tests how far that holds. First come the crash points, one at a time, up to the edge of the processor's promise. Then the ledger's rules. Then the file you agreed on in Scope, which finally does its work. The minutes that remain go to four curveballs.

An Effect Through a Party You Do Not Control

Walk the crash points in order. Before the row is written, nothing has happened, and the client's resend starts it. After the row and before the call, the outbox still holds the message, and the orchestrator sends it. After the call and before the answer, the state is unknown. After the answer and before the entry, the outbox and the key replay it, and the processor gives the same answer.

So exactly once is at-least-once plus the key at each hop, and it ends where the processor's promise ends. That promise is Scope's 24 hours, the horizon Scaling It left for this lesson.

For an unknown there are three options, and time chooses. While the buyer waits and the key is honoured, retry with it. Once the buyer has gone, which the resolver sees when the page stops polling, ask first, since a retry might charge someone who walked away. Past 24 hours, nothing is resent, because a resend could be a new charge, and the file closes it. A buyer on that path sees processing for a day or two.

What to do with an unknown, by situation
The buyer waits and the key is honoured→Retry with it
The buyer has gone, inside the 24 hours→Ask the processor
Past 24 hours→No resend; the file closes it
Retry With the Key vs Ask the Processor

A retry with the same key makes the charge happen if it never arrived, and replays it if it did. Use it while the buyer waits and the key is honoured.

Asking the processor for the charge that carries your payment ID, which every call sends, finds out and changes nothing. Use it once the buyer has gone, and when it says "not found", wait before believing it, because the first call may still be on its way.

Every path ends somewhere. A yes or a no closes the unknown at once. A replayed server error is neither, so the resolver asks. A "not found" that survives the wait closes it as failed, and the buyer may try again under a new key. If that first call lands after all, the file shows it, and the nightly match below books it and refunds it. You can give the whole rule in a breath: "While the buyer's there and the key's good, I retry. Once they've gone I ask, because a retry could charge someone who walked away."

The Ledger's Rules

Entries are appended and never changed. A correction or a refund is a new entry that names the old one. Refund the 100-dollar sale, and the new entry debits seller payable 90, debits commission 10, and credits processor receivable 100. When the file shows the refund, another debits receivable 100 and credits bank 100. If the seller was already paid, that takes the payable below zero, the case the chargeback curveball answers.

The relay feeds the running totals at least once, and adding twice is wrong. Receivable and commission also take every sale, 400 times a second at peak. So the relay folds a batch into one update per account, and that update stores how far the relay has read, in the same transaction as the sum. These totals are a derived view. A nightly check proves they still match the lines, the settled part included, and saves what it proved, so losing them means adding up a day of lines, not 7 years of them. The check runs before the payout, because a wrong total would pay out real money, and a seller whose total fails it waits while the rest are paid.

This balance gates no spending; it explains where the money went. The seller's payout is the only read that moves money, and it waits for the view to pass the cut-off. Seven years on, old lines can go only after a closing entry carries each account's balance forward, so the sums still come out.

Reconciliation Against the File

The file agreed in Scope now does its work. It says what money actually moved, so on what moved, it wins. Assume it carries the key you sent. Each night a job matches by payment ID three ways: file, ledger and payment state. Payouts and refunds match the same way, by their own IDs.

The differences come in classes. Timing: a charge at 23:59 is in tomorrow's file, so wait a day. In the file and unsettled here: that is a lost unknown, so book it, and if the order has moved on without it, refund it under a key of its own. Here and not in the file after the wait: it never landed, so reverse its entry, or just close it as failed if it was an unknown and has no entry. An amount that differs: an unexpected fee or a rate, so book the difference.

The file against the ledger, by class of difference
Timingcharged at 23:59, in tomorrow's file
Nobody is wrong yet. Wait a day. No entry.
In the file, unsettled herea lost unknown
The file is right. Book the charge and its settlement.
Here, not in the filestill missing after the wait
The file is right. Reverse the charge with a new entry.
An amount differsan unexpected fee, a rate
The file is right on what moved. Book the difference.

That reversal touches no seller's money, because payouts only take what has settled. Anything unexplained goes to a suspense account, a holding place that keeps the books balanced, and to a person, who clears it with a new entry. Assume 1 in 100,000 is still unexplained after the resolver and the match. That is 80 a day, a queue one person can clear. When the interviewer asks who wins, you say: "On what moved, the file wins. If neither side can be shown right, it goes to suspense and to a person, and the books still balance."

The night has an order, and it fits. An ordinary day's 8 million settlements each write a state change, two lines and an outbox row, with the relay's mark on it. That is five writes each, or 40 million. Ten partitions at half their limit take 5,000 a second, which makes 8,000 seconds, a little over two hours. Only then do the totals pass the cut-off and the payout window open. If the file is late, so is the run, because it pays only settled money. Sellers get paid late, and never wrongly. Each write names the state it expects, so running the job twice changes nothing.

The one-service job is Backend Deep Dive's Reconciliation. What is new here is that it checks a ledger, and that every correction is a new entry.

Curveballs

"The processor is down, and a second one exists." Open a breaker on the first, as in Chapter 6's Failure Across Many Services, and route new payments to the second. Never send an unknown there. That is the double charge the design exists to prevent, so unknowns wait for their own processor. A payment's processor goes on its row when the row is created, and never changes, so a call the first may have seen cannot drift across. The price is a token that works at both or a buyer who re-enters a card, the processor recorded on each payment, and a second settlement file.

"Add currency conversion." Each entry now balances per currency, in whole minor units such as cents, never in floating point. A conversion is two balanced entries through a conversion account, one in each currency, with the rate stored on them. The file may settle at another rate. That difference has its own account, which reconciliation expects, so it never lands in suspense.

"A chargeback arrives months after the seller was paid." A chargeback is the buyer's bank taking the money back. The processor takes a fee with it. The payout cannot be recalled, so a new entry shaped like a refund's lowers the seller's payable, perhaps below zero, and one more line books the fee. Contest it and win, and another new entry puts the money back. It counts against the charge, so no refund can follow it. A payable below zero needs its answer in advance. Assume 5 percent of sales sits in a reserve account and is released once the chargeback window has passed. On the 100-dollar sale that is 5 dollars and one more line, and the release, batched per seller each day, adds little, which takes Scope's 5 lines to about 6, and its 31 nodes to about 37. Beyond the reserve, future sales net it off, and a seller below zero gets no payout row until they have. Who bears the loss when the seller is gone is a policy, not a design, so ask.

"An auditor asks you to prove nothing was edited." Append-only by rule is a promise, so make it evidence. The application's role can only insert. Each entry's hash includes the previous one on its partition, so changing one line breaks every hash after it. Each day's last hash goes somewhere the database's administrators cannot reach. The settlement files are a witness you did not write. The cost is one sequence per partition, so its entries commit one at a time, and a checking job to run.

How Each Level Answers

A mid-level answer gets to a charge and a payout with an order table, and adds an idempotency key once the interviewer describes the double charge. It marks a timeout as failed at first, and corrects that when asked whether the money moved. It comes to the ledger when finance's question is put.

A senior answer says throughput is not the problem and turns the failure rate into a daily count. It introduces the unknown state unasked, gives the rule for a new key, and works a sale through balanced entries. It places the payout last, runs it as a rate against a window, and treats the file as the truth for what moved.

A staff answer asks what the outside party promises and for how long, and designs to the edge of that promise. It hands reserves and chargebacks to finance as a policy finance owns. It plans the second processor as a contract and a routing rule, including what unknowns do during the switch. And it decides what an auditor would accept as evidence, not only what the code intends.

Interview Pitfalls
  • Failing an unknown over to the second processor to keep checkout up, which is how a buyer gets charged twice.
  • Treating reconciliation as a report nobody acts on, rather than a job that writes entries.
  • Editing a ledger line to fix a difference, then being asked to prove that nothing was edited.
What Interviewers Listen For
  • The choice between retrying and asking made by whether the buyer is still there.
  • Which side wins a disagreement, and what happens when neither can be shown right.
  • The chargeback's effect on payouts raised before the interviewer has to.

Knowledge Check

The processor goes down, and a second one exists. What is the best move?

  • Fail every open payment over to the second, unknowns included
  • Keep retrying on the first with the same keys until it recovers
  • Open a breaker, and route only new payments to the second
  • Refuse all checkouts until the first processor has recovered

A chargeback arrives months after the seller was paid. What does the design write?

  • An edit to the original sale's lines, so that they come to zero
  • A new entry lowering the seller's payable, perhaps below zero
  • A recall of the seller's payout, through the processor's own call
  • A note on the payment, with no lines until the seller pays it back

An auditor asks you to prove nothing was edited. What turns append-only into evidence?

  • An audit table that a trigger fills whenever a line is changed
  • A code rule and a review process that together forbid any updates
  • A nightly snapshot of the whole ledger, kept in the same database
  • Chained hashes, with each day's last hash out of the admins' reach

The file settles a conversion at a different rate from the one on your entries. Where does the difference go?

  • Into an account of its own, which reconciliation expects
  • Into seller payable, so that sellers absorb every rate move
  • Into suspense, where one person reviews each one by hand
  • Onto the original entry, by updating its rate to the file's

You got correct