Chapter Five · Building Blocks

Coordination, Transactions and Time

Some facts in a design may be held by only one machine at a time: who leads, who holds a lock, which write came last, whether a change committed, which ID is taken. Six lessons cover why that is hard and the tool for each: consensus, leases with fencing, clocks, transactions across partitions, and IDs made without asking anyone.

6 lessons

Partitioning and replication, the two chapters before this one, multiply the machines. This chapter is about what those machines must agree on, and what the agreement costs. A network that loses messages and nodes that pause without warning make certainty impossible in general, and every working system gets around that with a majority, a timeout that is eventually right, or a design that removes the question.

The chapter starts with why agreement is hard, and what a slow node looks like from outside. It then rents consensus from a coordination service, fences a lock holder that paused, orders events without trusting wall clocks, commits a change across partitions, and makes IDs on many machines at once. The last lesson closes the loop on the URL shortener's counter blocks.

The facts only one machine may hold, and the tool for each
Which node leads a partition→An election through consensus
Who may write to a shared resource→A lease and a fencing token
Which of two writes came last→One owner that numbers them
Whether a change on two partitions committed→Two-phase commit, or a saga
Whether an ID is already taken→Counter blocks, or time-ordered IDs

Lessons in This Chapter