Google Spanner
Spanner is Google's globally distributed relational database, available to everyone as Cloud Spanner. It gives you SQL, schemas and ACID transactions like Postgres, scales horizontally across many machines like a NoSQL store, and replicates synchronously across zones or even continents. Its most famous trick is TrueTime, a clock API backed by GPS receivers and atomic clocks that lets Spanner order transactions globally by real time.
A few properties are worth separating:
- Relational and transactional: Tables, primary keys, secondary indexes, SQL (in GoogleSQL or a PostgreSQL dialect), and read-write transactions that can touch any rows anywhere in the database.
- Horizontally scalable: Tables are split into key ranges called splits, and Spanner moves and divides them across servers automatically as data and load grow. You add capacity by adding nodes, not by sharding in your application.
- Synchronously replicated: Every split is replicated with Paxos. A write commits only once a majority of replicas, usually in different zones or regions, have it. Losing a zone, or in multi-region configurations a whole region, doesn't lose committed data.
- Externally consistent: If transaction A commits before transaction B starts, B's commit timestamp is guaranteed to be later than A's, everywhere in the world. This is the strongest consistency guarantee a database offers, and TrueTime is how Spanner provides it without a single global sequencer.
Spanner comes up in interviews in two ways. Sometimes it's a direct answer: "we need a strongly consistent, globally available ledger that scales past one Postgres primary." More often, the interviewer wants to see whether you understand the ideas (Paxos-replicated shards, two-phase commit across them, timestamps from bounded-uncertainty clocks) because they're the backbone of every distributed SQL database, including CockroachDB and YugabyteDB. We'll cover both.
The Data Model
From the outside, Spanner looks like a relational database. You define tables with typed columns and a primary key, and you query them with SQL:
CREATE TABLE Accounts (
AccountId STRING(36) NOT NULL,
OwnerName STRING(MAX),
BalanceCents INT64 NOT NULL,
CreatedAt TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp = true),
) PRIMARY KEY (AccountId);
CREATE TABLE Transfers (
AccountId STRING(36) NOT NULL,
TransferId STRING(36) NOT NULL,
AmountCents INT64 NOT NULL,
Status STRING(16) NOT NULL,
) PRIMARY KEY (AccountId, TransferId),
INTERLEAVE IN PARENT Accounts ON DELETE CASCADE;
CREATE INDEX TransfersByStatus ON Transfers (Status);
Two things in that schema matter more than they look.
The primary key is the physical layout. Spanner stores each table sorted by primary key, and splits the key space into contiguous ranges. Rows with nearby keys live together. That makes range scans by key fast, and it also means the choice of key decides how load spreads across servers.
Interleaving co-locates related rows. INTERLEAVE IN PARENT stores each account's transfers physically right after the account row, in the same split. A transaction that updates an account and inserts a transfer touches one split, so it commits without any cross-server coordination. Interleaving is how you tell Spanner "these rows belong together," and it's the main schema lever for keeping transactions cheap.
Avoiding Hotspots
Because rows are sorted by key, a monotonically increasing key (an auto-increment ID, or a timestamp as the first key column) sends every new insert to the end of the key space, which is one split on one server. That split becomes a hotspot and caps your write throughput no matter how many nodes you add.
The fixes are the same ones you'd use with any range-partitioned store:
- Use a random UUID (version 4) as the key.
- Use a bit-reversed sequence, which Spanner supports natively, so sequential numbers scatter across the key space.
- If you need time ordering, prefix the key with a hash or shard number (
ShardId = hash(UserId) % 64, thenTimestamp), and fan reads out across the prefixes.
If you propose Spanner, say what the primary keys are and why they don't hotspot. "Accounts keyed by UUID, transfers interleaved under their account" shows you know that the key is both the partitioning and the co-location decision.
Secondary Indexes
A secondary index in Spanner is itself a table, keyed by the indexed columns plus the base table's primary key, and it's split and distributed independently. So TransfersByStatus probably lives on different servers from the transfers it points to. Updating a transfer's status updates two places, which makes the write a distributed transaction. Spanner handles that for you, atomically, but it costs latency. You can also interleave an index in a parent table (for example, an index on (AccountId, CreatedAt) interleaved in Accounts), which keeps it next to the data when the index starts with the parent key.