Episode 3 of 9 · Stripe
The money-movement state machine: a payment is not a row you update
Vertical version.
Chapters
Narration
You pay for something online. Your card is charged, and the bank says yes. Then, a moment before the shop can write down that it worked, its server crashes. Now picture the shop's database. It still says, not paid. The bank says, paid. Which one is true? Did your money move, or did it not? If the shop retries, it might charge you twice. If it does nothing, it might ship nothing. So how does a payment system always know where a payment is, even when its own server can die at any moment? Why should you care? Every online business that takes cards meets this exact moment sooner or later. Servers crash, networks drop, and a bank sometimes stops to ask the cardholder for a code. A system that loses track of one payment either charges a person twice or gives away goods for free. So here is the one question for the next few minutes. How do we always know where a payment is? And here is the plan. First, the obvious way, a status column. Second, the real way, a short list of states. Third, the hops you are not allowed to make. And fourth, what a crash looks like after all that. Stop one, the obvious way. Give every order one status column. It says unpaid. You charge the card, then you update the column to paid. Two steps, one after the other. Now look at the gap between them. If the server dies in that gap, the card was charged but the column still says unpaid. Swap the order and write paid first, and a crash leaves the column saying paid when the bank was never asked. Either way, the column is wrong, and nothing in it says which kind of wrong. A column holds only the last thing someone managed to write. It cannot hold what really happened. Stop two, the real way. Start with a plain idea. A payment is one thing that changes over time. So instead of one word that anyone can overwrite, we list every situation it can be in, and say that it is in exactly one of them. Picture a board with five holes and a single peg. The peg shows where the payment is now. First, it needs a payment method. Then it needs to be confirmed. Then, maybe, the bank asks the cardholder to prove it is them. Then it is processing. Then it has succeeded. Five holes, one peg. In Stripe each situation has a name, like requires confirmation. And each move from one hole to the next is called a transition. One of those situations deserves a closer look, because a card charge is really two operations. The first is called authorize. In plain words, it asks the cardholder's bank to hold the money. Nothing moves, but the money is promised and set aside. The second is called capture. That is when the money is actually pulled. Think of a hotel. It holds a deposit when you book, and takes the real amount when you leave, maybe less, never more. The hold expires after about a week. In between, the payment is waiting, and that waiting is a state of its own. It is a place on the board, not a gap in the code. Stop three, the hops you are not allowed to make. Draw the board with arrows only where a move makes sense. Processing may go to succeeded. A canceled payment may not go anywhere. Why does this matter? Suppose a payment has already succeeded, and a stray retry tries to capture it again. If that were allowed, you would charge a stranger twice. Suppose a payment was canceled, and someone tries to refund it. You would pay out money you never took. Both are real bugs, and both are moves that are not on the board. So the system must refuse them, every time. How does a database refuse a move? It checks the state while it changes it, in one step. The update says, set the state to succeeded, but only where the state is processing. Run it once. The payment is processing, so one row matches, and one row changes. Run it again. The state is now succeeded, so zero rows match, and zero rows change. One row, then zero rows. The second attempt does nothing at all, and it does no harm. The check and the change are the same operation, so nobody can slip in between them. The database is the referee, and the where clause is its rule. Stop four, the crash, again. Go back to the server that died. When it restarts, it does not ask, did my last step work? It cannot know that. It asks a different question. What state is this payment in? It reads the peg. If the peg says processing, it carries on from processing. If the peg says succeeded, there is nothing left to do. Run the same step twice and the guard makes the second one change zero rows. A bank challenge fits the same shape. The payment waits in its requires action hole while a person types a code, exactly like a server that is down. Each hop is also written down as a new line in an append only ledger, and that is its own episode. Three things to keep. One. A payment is not a row you overwrite. It is one thing, in exactly one of a few named states. Two. Only the hops on the board are allowed, and the database refuses the rest. One row changes, then zero. Three. After a crash, you never ask whether the last step worked. You read the state, and carry on from there. A status column remembers the last thing someone wrote. A state machine remembers where the payment is, and what it may do next. That difference is what turns a crash into a pause.
The money-movement state machine: a payment is not a row you update. You pay for something online. Your card is charged, and the bank says yes. Then, a moment before the shop can write down that it worked, its server crashes. Now picture the shop's
Based on the board Money Movement State Machine.
Related papers: Time, Clocks, and the Ordering of Events in a Distributed System.
Covered in The Hold That Is Not Money Yet: authorisation, capture, clearing, settlement and chargebacks, Stripe, the case study.
Saved in this browser, and to your account when you sign in.