Distributed transactions: the two ledgers must agree
Vertical version.
Chapters
Narration
Last time a majority decided who holds the pen. One leader, one book, every write landing in a single place. Now the money has to leave that book and land in another one, on a different machine. The chai stall keeps two ledgers, and no single transaction can span them. Debit Alice, credit Bob, and if the second machine dies in the gap, a hundred rupees stops existing. The first answer is a protocol. A coordinator asks both shards one question before anything moves. Can you commit this? Each shard does the work, locks the row, writes it to its own log, and votes yes without committing. Only when both have promised does the coordinator send commit, and both apply in the same breath. All or nothing, across two machines. Now kill the coordinator in that gap. Both shards have voted yes, both are holding their rows, and the one process that knew the decision is gone. Nothing inside the protocol ends this. The rows stay locked while every other customer waits, until somebody walks in the next morning, reads the in doubt transaction, and commits both books by hand. The second answer gives up on locks entirely. A saga is three ordinary local transactions in a row. Shard A debits Alice and commits, and the row is free again in the same breath. Shard B credits Bob and commits. The receipt is written last. Nobody votes, nobody waits, nobody holds anything, and between step one and step two the two books openly disagree. All three steps ran that way, until the receipt service fails. There is no rollback here, because every earlier step already committed on its own shard. So the saga runs backward and writes the opposite of each one: reverse the credit on Bob, refund the hundred to Alice. Square again, and the ledger now carries four lines describing a transfer that never survived. And the honest third answer is to not distribute the transaction at all. Choose the shard key so that accounts which have to move money together live on the same machine, and the whole problem collapses into one ordinary local commit. Two phase commit when you must be strongly consistent and can afford to block. A saga when you need to stay available. Neither, when one shard already holds both books. Two machines cannot share one transaction. You either block until they agree, or let them drift and write the undo yourself.
Based on the board The Two Ledgers Must Agree.
Try it in the lab: The two ledgers must agree.
Related papers: In Search of an Understandable Consensus Algorithm, Spanner: Google's Globally-Distributed Database, Time, Clocks, and the Ordering of Events in a Distributed System, Brewer's Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services, Paxos Made Simple and Large-scale Incremental Processing Using Distributed Transactions and Notifications.
Covered in The Two Ledgers Must Agree (Distributed Transactions).
Saved in this browser, and to your account when you sign in.