System design paper · 2012

Spanner

Corbett, Dean, Epstein, Fikes, Frost, Furman, Ghemawat, Gubarev, Heiser, Hochschild, Hsieh, Kanthak, Kogan, Li, Lloyd, Melnik, Mwaura, Nagle, Quinlan, Rao, Rolig, Saito, Szymaniak, Taylor, Wang and Woodford · OSDI 2012

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.

A clock that is sure of the time only to within 4 ms picks the late end of its range as the commit time the moment the request arrives, then waits until that moment has surely passed. That takes 8 ms, and afterwards no later transaction can be given an earlier time.

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?

The design choices

Spanner’s authors describe a system meant to scale to millions of machines across hundreds of datacentres and trillions of database rows. Its first customer was F1, which rebuilt Google’s advertising backend on it. Applications choose how widely to copy their data: within one region to survive machine failures, across continents to survive a regional disaster, or near their users to make reads fast. The authors claim it is the first system to distribute data at global scale and still support externally consistent distributed transactions. It offers a table-like data model with a SQL-based query language, automatic splitting and failover, and transactions that behave as if one machine ran them. The sections below take them in order.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

About the paper

Spanner: Google’s Globally-Distributed Database by Corbett, Dean, Epstein, Fikes, Frost, Furman, Ghemawat, Gubarev, Heiser, Hochschild, Hsieh, Kanthak, Kogan, Li, Lloyd, Melnik, Mwaura, Nagle, Quinlan, Rao, Rolig, Saito, Szymaniak, Taylor, Wang and Woodford, OSDI 2012.

Read the original paper

More System design papers

All papers