System design paper · 2012
Spanner
How Spanner runs one database across the world, and uses a clock that reports its own error to make transactions behave as if on a single machine.
You’ll learn to
- Explain how Spanner splits data into groups, copies each group to several datacenters and keeps related rows together.
- Trace a transaction that touches two groups, from the first request to the moment it is safe to tell the client.
- Explain how a clock with a known error margin, plus a few milliseconds of waiting, puts transactions in their true order.
- Describe how reads run without locks on any up-to-date copy, and what systems without Google’s clocks do instead.
Read first: Raft, Lamport clocks
The idea in short
Google needed one database that could hold its advertising data in datacentres on both coasts of the US, grow without engineers splitting it by hand, and still let an application change many rows at once with an all-or-nothing result. Spanner splits the data into pieces, copies each piece to several datacenters and keeps the copies in agreement by a vote.
The hard part is order. If one transaction finishes before another starts, everyone must see them in that order, but clocks on different machines disagree. Google built a clock that returns a range instead of one time, such as 4 ms either side of the true time. Before telling a client that a transaction is done, Spanner waits until the end of that range has passed. The wait costs a few milliseconds and buys a single order for the whole world.
The problem
By 2011 Google’s advertising system, called F1, ran on MySQL that engineers had sharded by hand: split into many separate databases, each customer and all their data assigned to one of them. The data was tens of terabytes, small next to many NoSQL stores, but it had grown too big for this arrangement. The last time the team had to re-split it took more than two years of intense effort, with coordination and testing across dozens of teams. The job was too risky to repeat, so the team started parking some data in a separate store called Bigtable, which gave up the ability to query across everything. Failover between a MySQL primary and its copy was hard too, and risked losing data.
Google’s two main storage systems each fell short in a different way:
- Bigtable had no transactions across rows, and copied data between datacenters only eventually, so readers could see stale data. Users complained often.
- Megastore gave a table-like data model and copies that stayed in step across datacenters, and at least 300 Google applications used it, including Gmail and Calendar. But writes were slow. Any copy could start a write, so writes from different copies collided in the voting step even when they touched different rows. That voting step uses , an algorithm that gets a group of copies to agree on one order of changes (the Raft guide at /papers/raft covers the same job). The authors report that throughput for a group collapsed at several writes per second.
A is a group of reads and writes that either all take effect or none do. Transfers between bank accounts are the classic case. Many NoSQL systems dropped transactions across machines because they are expensive. The Spanner team chose to keep them: “better to have application programmers deal with performance problems due to overuse of transactions” than to code around their absence.
So the question the paper answers is: can one database span the planet, split and move its own data, survive the loss of a datacenter, and still give transactions that behave as if there were one machine with one clock?