Systemsfor Aravinda
0 / 78 core
Week 12 · Production storiesAdvanced

Google Spanner

1.5 hours · optional track

Read first: Consensus and Raft, Clocks and ordering

In one sitting

Spanner gives you external consistency: if one transaction commits before another starts, the first has the smaller timestamp. TrueTime returns an interval that contains the true time, and commit wait holds the commit until that timestamp is in the past. One Paxos group is a single write, several groups add two-phase commit, and a new-grad design that says it uses Spanner has not said any of this.

External consistency means commit order agrees with real-time order. If one transaction commits before another starts, the first has the smaller timestamp. A new-grad loop usually does not ask you to build the system that provides that, and it should not accept “I will use Spanner” as the design. This page is the optional track. It is worth an evening because the cost is a clock that returns an interval, a wait on every commit, Paxos inside each group of replicas, and two-phase commit when a transaction touches more than one group. You want to recognize when a diagram has skipped that cost by naming a product.

The problem

A transaction that means the same thing in every region is a hard requirement. “The same thing” here is not “replicas eventually agree.” It is stronger, and it mentions the wall clock. If Alice’s transaction commits, and only then Bob’s transaction starts, Bob’s commit timestamp is after Alice’s, even when Alice and Bob are far apart. Anyone who reads the timestamps can trust that order to match what happened in real time.

That property is what people reach for when they say a design needs a global database. The reach skips the bill. The bill is milliseconds of clock uncertainty on the commit, the operational cost of keeping that uncertainty small, and a consensus round inside the replica group. If the transaction also spans groups, the bill includes two-phase commit, which can block. Most products in a 45-minute loop do not have the requirement. They have a primary in one region and a failover story. This page is how you tell those apart.

The design

TrueTime is an interval

TrueTime is not a logical clock. A Lamport clock is a single integer that preserves “happened before” in one direction and does not promise the converse. A hybrid logical clock mixes a physical clock with a counter so the value stays near wall time. TrueTime does neither of those jobs. It is an interval API. A reading returns earliest and latest, written as the interval from earliest to latest, and the true time is inside that interval. You are not handed a moment. You are handed a window that contains the moment.

The paper treats the uncertainty as milliseconds, kept small with GPS and atomic clocks. There is no single epsilon to memorize. Inventing one, a favorite millisecond you heard somewhere, is how a design pretends the wait has a constant you can ignore. Say milliseconds. Say the clocks are what keep the window there. Stop.

Commit wait

A commit picks a timestamp, then refuses to finish until that timestamp is definitely in the past. The mechanism has three beats.

First, take a TrueTime reading. The true time is somewhere between earliest and latest. Latest may be ahead of the true time. Earliest may be behind it. The width is the uncertainty.

Second, choose the commit timestamp at least as late as the late end of a reading taken for the commit. The timestamp can still be ahead of the true time, because the late end can. If you skipped ahead to “commit now,” you would be stamping the transaction with a time that might not have happened yet.

Third, wait. Take fresh readings until the interval lies entirely after the chosen timestamp. Entirely after means earliest is greater than the timestamp. The true time is at least earliest, so the true time is greater than the timestamp too. The timestamp is in the past. Then the commit is allowed to become visible.

That wait is commit wait. It is what makes the external-consistency claim. Suppose transaction A finishes the wait, and only then transaction B starts. When B starts, the true time is already past A’s timestamp, because A was not allowed to commit until that was true. B’s timestamp is chosen from a later reading, so it is larger than A’s. Commit order matches real time. The consistency lesson defines linearizability for a single register: one copy, and each operation takes effect at a point between its start and its finish. External consistency is the real-time story for transaction commits. Use the commit words on this page. Use the register words on that one.

Skip the wait and the claim falls over. The timestamp can sit in the future relative to the true time, inside the uncertainty of the reading you used. A transaction that starts in that overlap can receive a timestamp that does not respect the order a person watching the wall clock just saw. Commit wait is the part that closes the overlap before anybody is told the commit happened.

reading contains the true time
choose commit timestamp at the late end of a reading, or later
wait until a new reading has earliest greater than that timestamp
only then commit
flowchart LR
  read[TrueTime reading]
  pick[Choose commit timestamp]
  wait[Commit wait]
  past[Interval is past the timestamp]
  commit[Commit becomes visible]
  read --> pick --> wait --> past --> commit

The width of the wait is the uncertainty you still had, on the order of the milliseconds the paper talks about. It is not a number you should write on a board as if you had measured their clocks. GPS and atomic clocks are the operational cost of keeping the width in that band. A five-person team does not run those clocks. Without them the interval gets wider and the wait gets longer, which is the reason the technique does not fall out of an ordinary NTP client. These notes do not quantify “wider.” Do not quantify it either.

One Paxos group, or several

Data is replicated in groups. Each group of replicas runs Paxos. The original paper uses Paxos, not Raft. Raft was written so more people could implement the same class of system safely. When you retell Spanner, the groups run Paxos. Renaming them to Raft groups is a different story.

In this class of protocol an entry is committed when a majority of the group has stored it. A minority of the replicas can be down and the group can still accept the write. These notes do not publish the replica count. Do not say three or five. Say a majority of the group.

A transaction that touches one group is a Paxos write. A majority of the group stores the entry, and commit wait still applies. External consistency is a statement about the timestamp and the wall clock, and it still applies when the replicas sit close together. Replicas in the same region do not get to skip the wait.

A transaction that touches several groups uses two-phase commit on top of the Paxos leaders. The prepare records themselves are replicated. A prepare is not one machine’s private memory. The leader of a group replicates the prepare through that group’s Paxos, so losing one replica does not lose the promise.

Two-phase commit still blocks if the coordinator dies after the participants have prepared. Each participant has promised it can commit. The promise is on the replicated log. Neither participant should finish alone. If one group committed and the other aborted, the transaction would be half applied. They wait until the decision is known. A replicated prepare survives a replica failure inside the group. It does not authorize the group to invent a decision the coordinator had not published. That block is the full failure story of two-phase commit, and it is in force here even though the prepares are replicated. The transactions lesson names the same block for the general protocol. Spanner’s addition is that the prepare record is a Paxos entry, not a single process’s disk.

flowchart TD
  client[Client]
  leaderA[Paxos leader of group A]
  replicasA[Replicas of group A]
  leaderB[Paxos leader of group B]
  replicasB[Replicas of group B]
  wait[Commit wait]
  client --> leaderA
  leaderA --> replicasA
  leaderA --> leaderB
  leaderB --> replicasB
  leaderA --> wait
  leaderB --> wait

Read the diagram as the multi-group path. The client talks to a leader. That leader runs two-phase commit with the other group’s leader. Each leader replicates through its own group, which is how the prepare becomes durable inside the group. Commit wait sits on the commit. If the transaction touches only group A, the arrows to group B are absent and the write is a Paxos write plus the same commit wait. Most of the design work in placing data is an attempt to stay on that smaller path.

Directories

Directories are the unit of data placement. Rows that are read together can be moved together. Placement is how you decide whether tomorrow’s transaction is one Paxos group or a two-phase commit.

Use a concrete layout and label it as an example, not as a Google schema. A user row and that user’s balance row are read on the same request. Put them in one directory. Place the directory on one Paxos group. A transfer that only debits that balance is a single-group Paxos write, plus commit wait. A transfer between two users whose directories sit on different groups contacts two leaders and runs two-phase commit. The directory boundary is the transaction boundary you chose when you placed the rows.

This is the same instinct as ordinary partitioning. Rows that a transaction needs together belong on the same partition, or you pay a cross-partition commit. Spanner’s name for the movable bundle is a directory. Your name, in a new-grad design, can be “the shard key keeps this account’s rows together.”

What the distance costs, without a fake sum

Light in fiber is about 200,000 km/s, about 200 km per millisecond. A 4,000 km path is 20 ms one way and about 40 ms for the round trip before routers. A planning band for coast-to-coast round trip is tens of milliseconds. That arithmetic is physics from the latency lesson. It is not a Spanner benchmark and it is not a service-level agreement.

200,000 km/s = 200 km per ms
4,000 km / 200 km per ms = 20 ms one way
round-trip floor = 40 ms, before routers

A Paxos round whose replicas span that path pays that floor, and commit wait adds the clock uncertainty on top. The uncertainty is milliseconds in the paper’s treatment. Do not add 40 and some epsilon you made up and call the sum a Spanner latency. A group that stays inside one region pays a much smaller network term. Cross-zone in one region is often around a millisecond, and that too is a planning band, not a measurement of their fleet. Commit wait is still on the commit, because the timestamp has to be in the past even when the replicas are close. Distance and clock uncertainty are two different lines on the budget. Write both. Add them only after you have measured both, which in an interview you have not.

Path What actually runs What the commit waits on
One group, replicas close together A Paxos write Majority of the group, then commit wait for the timestamp to be in the past
One group, replicas far apart The same Paxos write The same, plus the round trip the distance imposes
Several groups Two-phase commit on the Paxos leaders, prepares replicated Both groups’ prepares, the decision, and commit wait
Coordinator dies after prepare Participants have a replicated promise and no decision They block until the decision is known
Rows of one transaction in one directory You stay on the one-group path You avoid the two-phase commit row

What they traded

Choice What they gained What they spent
TrueTime interval and commit wait External consistency, commit order matches real time Every commit waits out clock uncertainty, and you operate GPS and atomic clocks
Paxos in each replica group A write survives a minority of replicas being down Consensus latency inside the group, and the operational cost of Paxos
Two-phase commit across groups One transaction can touch several groups, with prepares replicated Participants block if the coordinator dies after prepare
Directories as the unit of placement Rows read together move together, so a common transaction stays in one group A bad split turns that transaction into two-phase commit
A global deployment The timestamp order means the same thing in every region Fiber on the Paxos round when a group is spread out, on top of commit wait

The first row is the one to be able to say without notes. The guarantee is the timestamp order against real time. The price is the wait, and the clocks that keep the wait in the milliseconds. Everything else is how they made a replicated, multi-group database able to offer that guarantee. You can want the guarantee and still refuse the price. Refusing it is a design. Naming the product is not.

What to steal

Steal the sentence, then decide if the product is paying that cost. The sentence is: if A commits before B starts, A’s timestamp is smaller, in every region. The reason it is on the advanced track is that you pay commit wait and a specialized clock to get it. A product that truly needs a transaction in one region to be after a transaction that has already committed in another, in real time, is allowed to pay. Say that requirement first. Then say the wait.

Steal directories under a boring name. Put rows that one transaction reads and writes on the same partition so the commit stays local. That advice does not require Spanner, Paxos, or TrueTime. It requires you to know the transaction boundary before you draw shards.

For a new-grad loop, the design that answers the question is one region, one primary, and the failover. A synchronous replica in another zone if the commit must survive a machine. Fencing on the failover, a lease or a generation number, so the old primary cannot keep accepting writes after the new one has started. You say what the user waits for, which is one commit on that primary. You do not say “Spanner” in order to stop talking.

A design that “uses Spanner” has not answered the question. It has not said which rows share a directory. It has not said whether the transaction is one group or several, which is the difference between a Paxos write and two-phase commit. It has not said what commit wait does to the latency budget, and it has not kept clock uncertainty separate from the fiber round trip. It has not said what happens when a replica fails and the group needs a majority, or what happens when the coordinator dies after prepare. It has not said why this product needs commit order to match real time in every region. Those are the questions the name was substituting for.

A five-person system copies the placement habit and the one-primary failover. It does not run atomic clocks, it does not operate Paxos groups, and it does not take a cross-group two-phase commit as the default for a new table. If a reviewer asks for global external consistency, you repeat the requirement back and ask whether one region is actually insufficient. Most of the time it is sufficient, and the honest diagram is the primary.

In five minutes, if someone names Spanner: the goal is a commit order that matches real time, TrueTime is an interval, commit wait holds the commit until the timestamp is in the past, one group is Paxos, several groups are two-phase commit with replicated prepares, directories keep a transaction on one group. Then say you are not proposing that. You are proposing one region, one primary, and a fenced failover. There is no epsilon on the board.

Check yourself

TrueTime returns an interval. Why is a single timestamp the wrong description of the API, and what must you refuse to invent?

A reading returns earliest and latest, and the true time is inside that window. It is not a Lamport clock and it is not a single moment. The paper treats the uncertainty as milliseconds, kept small with GPS and atomic clocks. There is no single epsilon to memorize. A made-up millisecond hides the wait. Commit wait exists because the window is a window.

Transaction A commits, and only then transaction B starts. Why is A's timestamp smaller, and which step fails if you skip commit wait?

A is not allowed to commit until a TrueTime interval lies entirely after A’s timestamp, which means the true time is already past that timestamp. B starts after that, so B’s timestamp comes from a later reading and is larger. That is external consistency. If you skip the wait, A’s timestamp can still be ahead of the true time, inside the uncertainty of the reading. A transaction that starts in that overlap can receive a timestamp that does not match real-time order. The wait is the step that makes the claim true.

What runs for a transaction that touches one Paxos group, and what extra protocol runs when it touches two?

One group is a Paxos write. A majority of that group stores the entry, and commit wait still holds the commit until the timestamp is in the past. Two groups use two-phase commit on top of the Paxos leaders. The prepare records are themselves replicated, so a prepare survives the loss of one replica inside a group. The paper uses Paxos, not Raft. These notes do not give a replica count, so the answer is a majority, not a specific N.

The coordinator dies after both groups have prepared. What are the participants stuck waiting for, and what does a replicated prepare not let them do?

Each group has promised it can commit, and that promise is a replicated prepare. They cannot finish the transaction until the decision is known. If one group committed and the other aborted, the transaction would be half applied, so neither side invents the outcome. A replicated prepare means the promise survives a replica failure. It does not mean the group may guess the coordinator’s decision. They block.

The interviewer asks how you will keep accounts consistent, and you answer that you will use Spanner. What have you still not said?

You have not said which rows share a directory, or whether a transaction is one Paxos group or a two-phase commit across groups. You have not put commit wait on the latency budget, and you have not kept clock uncertainty separate from a cross-country round trip of tens of milliseconds. You have not said what a majority failure or a dead coordinator does. You have not said why the product needs commit order to match real time in every region. The answer that usually closes a new-grad question is one region, one primary, a replica if the commit must survive a machine, and a fenced failover.

Based on: Spanner, OSDI 2012, Corbett and others.