CS PAPERS DEEP-READ · PAPER 24

Spanner — Google's Globally-Distributed Database

Corbett et al. · Google · OSDI 2012

中文 →

What did this paper do?

In 2012 Google built Spanner — a database that spreads its data across data centers on several continents, yet still behaves like one ledger whose ordering everyone agrees on. It runs Google's most critical services (like the advertising backend) and is the intellectual source of later "global databases" such as Google Cloud Spanner, CockroachDB, and TiDB. Its most famous trick is an "honest clock" called TrueTime.

First, the pain of the old world

To be both fast and durable, a database has to copy its data several times and scatter the copies across cities. But once the data is spread out, a nasty question appears: which happened first? An order placed in Beijing, a price edited in New York — which takes effect first? Judge by each data center's own clock? Every machine's clock runs a little fast or slow, and none can swear its time is exactly right. So global databases of the day had to choose one of two: huddle in one region and keep a clean ordering, or spread worldwide and live with occasional stale reads and scrambled ordering. Spanner insisted on having both.

The idea: make clocks "honest"

An ordinary clock's flaw is pretending to be precise — it tells you "it's exactly 3:00," while secretly it isn't sure. Spanner does the opposite: it makes every machine's clock own up to its error, reporting not a point but a small interval — "it's somewhere within a few milliseconds around 3:00, and the true time is definitely inside this range." That "time with an honest margin of error" is TrueTime.

How does it sort out the ordering?

An honest interval alone isn't enough; the key is to "wait a beat" when finalizing. Every time a change is about to become final, Spanner first stamps it with a timestamp, then deliberately waits a few milliseconds — until even the fuzziest clock admits "that timestamp is now in the past" — before revealing the change to anyone. That wait guarantees one thing: any change that starts after it will get a strictly larger timestamp. So every data center worldwide, even without ever consulting one another, gives the exact same answer to "which came first" — that ledger's ordering is one everyone accepts. For fault tolerance, Spanner keeps a copy of each piece of data in several data centers and uses a round of "voting" to keep the copies in agreement, so losing a machine or two doesn't stop it.

What it brought

Spanner was the first to prove that "span the globe" and "strongly consistent transactions" can coexist — you don't have to pick one. On data straddling continents, you can transfer money, place orders, and update inventory as confidently as on a single-machine database, and the system guarantees the ordering never scrambles and what you read is always the latest, sensible state. To be honest about the cost: this "wait a beat" adds a few milliseconds of latency to every write, and it relies on real GPS antennas and atomic clocks installed in every data center — hardware not everyone can afford to build.

Remember one thing

Stop pretending the world's clocks all agree to the microsecond — have each clock honestly report "the time is somewhere in this small interval," then wait a short beat at every commit until that error has been "waited out," and the whole world will give one and the same answer to "which came first." So a database blanketing the planet feels, in use, like one single-machine ledger with an ordering everyone accepts.

Want the TrueTime interval and commit-wait timeline, the Paxos-group + two-phase-commit diagram, and the numbers? → switch to the deep read