How it works
Spanner puts table and index data into ordered key ranges called splits. It automatically divides and redistributes ranges as data and load grow. The primary key still determines locality and possible hotspots. Automatic splits cannot prevent contention when concurrent transactions update one row.
A Paxos group replicates each split and elects a leader. The leader coordinates writes. A voting quorum must participate for progress. Read-write replicas store data and vote. Witness replicas vote but do not serve reads. Read-only replicas serve reads but do not vote.
A read-write transaction that accesses multiple splits uses two-phase commit across the participating groups. Paxos keeps each group's state durable through replica failures. Two-phase commit coordinates the atomic result across groups. These mechanisms solve different problems. Both require coordination.
TrueTime provides a bounded interval for the current time, not perfectly synchronized clocks. Spanner assigns timestamps and uses commit wait to support external consistency. Transactions with its default serializable isolation respect the real-time order of completed transactions. Not every read must contact every region.
Spanner keeps multiple data versions with MVCC. Read-only transactions can read a consistent snapshot without read-write transaction locks. Default serializable read-write transactions use locks and can abort under contention. SQL queries may execute across multiple splits. Access paths, indexes, and transaction scope still matter.
What it gives you in a design
- Distributed relational transactions
- Update related rows atomically, even across different splits. This helps with inventory reservations and ledger entries. A wide transaction needs more coordination than a local transaction.
- External consistency
- Default serializable transactions respect real-time transaction order. State the isolation and read mode explicitly. Do not assume that all supported modes provide the same guarantees.
- Strong and stale snapshot reads
- Strong reads return a consistent view of current committed data. Stale reads sacrifice freshness for possible reads closer to clients. Choose staleness separately for data exploration, reports, and decisions that require correctness.
- Automatic data distribution
- Spanner manages splits and their locations as the database grows. You still need keys that distribute writes. You also need enough provisioned compute capacity for the workload.
- Secondary indexes and interleaved tables
- Secondary indexes provide other access paths but require more write work. They can also create hotspots. Interleaved tables can improve parent-child locality when the schema and access patterns match.
- Change streams
- Change streams expose committed data changes for downstream work, such as updates to search indexes or analytics systems. Consumers still need durable progress records and retry-safe operations. A stream does not guarantee exactly-once external side effects.
Scaling, replication and failure
Design the primary key for both distribution and access patterns. A leading monotonically increasing timestamp or sequence can concentrate inserts at one key range's end. Hashing, UUIDs, or bit-reversed sequences can distribute writes. These choices have costs for ordered scans and locality. Check secondary-index keys for the same problem.
More compute increases available capacity. Useful throughput depends on split distribution, query efficiency, and contention. One inventory row, account balance, or global counter remains a serialization point. Keep transactions short. Index selective predicates. Separate high-volume derived counters from authoritative transaction state when requirements permit.
Regional and multi-region configurations have different trade-offs for latency and failure domains. Writes must reach the leader and the required voting quorum. Client locations, leader locations, and replica topology affect latency. Read-only replicas provide more read locations. They do not automatically reduce write coordination costs.
Spanner handles failures through replicated state and leader election. Writes can continue only where the relevant groups retain a quorum. An isolated minority cannot independently accept conflicting writes. Survival of a regional failure depends on the selected configuration. Some stale reads may remain possible when fresh reads or writes cannot proceed.
Limits and defaults to know
Many of these values are configurable. Check the current documentation before you use an exact value.
| Commit size | A commit has a 100 MiB limit, including index and change-stream data. Divide large logical jobs into bounded transactions. |
|---|---|
| Mutations per commit | A commit supports up to 80,000 mutations, including mutations for indexes. This does not directly limit the number of rows an application can update. |
| Version retention | The version retention period defaults to 1 hour. You can configure it up to 7 days. Historical reads must stay within retained history. |
| Default transaction isolation | Read-write transactions default to serializable isolation. Reassess anomalies and guarantees if you select another supported isolation mode. Do not assume that the default guarantees still apply. |
Use it when
- The design needs atomic updates across records that may reside on many servers.
- A growing relational workload needs horizontal scaling without application-managed shard routing.
- Multi-region durability and strong transaction correctness matter more than minimum write latency.
- The team wants managed replication and failover. It can justify the operational and financial trade-offs.
Avoid it when
- A single-region PostgreSQL or MySQL deployment easily meets scale and availability requirements with less cost and complexity.
- The application must accept writes independently on both sides of a network partition. It must reconcile the writes later.
- The main workload involves large analytical scans, media storage, or high-volume telemetry. It has little need for relational transactions.
- The throughput target requires repeated updates to one globally shared row at very low latency. Horizontal database scaling does not remove that contention.
What interviewers ask about it
When Spanner is on your whiteboard, expect these follow-up questions. Each answer gives the core of a strong answer.
- How does Spanner provide external consistency without perfectly synchronized clocks?
- TrueTime reports a time interval that contains the actual time. Spanner assigns timestamps and uses commit wait to account for this uncertainty. Transaction concurrency control and replication also help default serializable transactions respect real-time order.
- Why does Spanner need both Paxos and two-phase commit?
- Paxos replicates the state of each split and supports safe progress despite replica failures. Two-phase commit coordinates one atomic transaction across multiple splits. Replicas store participant and coordinator state for recovery.
- What happens if a region becomes isolated?
- Groups that retain a voting quorum can continue to make progress, possibly after a leader election. A side without a quorum cannot accept independent writes. The replica layout determines whether the system tolerates the loss of a region.
- How would you prevent write hotspots?
- Do not concentrate new writes under a leading key whose value only increases. Check index keys and table keys. Distribute independent writes across key ranges. Redesign the application for a single record with frequent updates. More splits do not solve this problem.
- How do you handle transaction retries and an uncertain commit result?
- An aborted transaction can require the application to repeat the entire transaction callback. The callback must not send emails, charge cards, or do other irreversible external actions. For uncertain outcomes, use a durable operation ID. Check the transaction state before you issue another logical operation.
- Does Spanner remove the need for sagas or an outbox?
- Spanner can remove some coordination that the application manages when one Spanner database transaction contains all affected state. Payments, queues, and other external services remain outside that boundary. An outbox, idempotency, and compensating workflows may still be necessary.
Common mistakes
- The claim that TrueTime replaces consensus or removes clock uncertainty.
- The assumption that automatic splits immediately fix a hot row or an append-only hotspot.
- The claim that multi-region deployment provides local, low-latency writes everywhere without quorum coordination.
- The assumption that a database transaction includes external APIs in its atomic boundary. External side effects inside a transaction callback that can repeat.
Alternatives
- PostgreSQL or MySQL
- Choose a conventional relational database when one writable primary and its replicas meet the requirements. It usually provides a simpler initial design. It avoids distributed transaction coordination for ordinary local transactions.
- CockroachDB
- Consider it when you need distributed SQL and serializable transactions but also need deployment flexibility or independence from a Google Cloud-specific service. Compare locality controls, failure behavior, SQL compatibility, and operational responsibilities.
- DynamoDB
- Consider it for predictable key-based access patterns in AWS when broad relational queries and cross-partition relational transactions are not the main requirement. Explicitly evaluate transaction scope and the consistency mode for the selected global-table configuration.
- BigQuery
- Choose it for warehouse-style analytical queries over large datasets, not transaction updates that need low latency. A common design keeps authoritative transaction state in Spanner. It exports changes for analytics.
Where it is used
Spanner is a good fit for these interview questions. Each line tells you why.
- Design a payment systemSpanner can store idempotency records, balances, and ledger entries in one transaction. External payment operations remain a separate reliability boundary.
- Design a ticket booking system like TicketmasterSerializable transactions can enforce exclusive seat allocation. Contention on popular inventory still requires admission control and short transactions.
- Design a hotel booking systemA transaction can atomically reserve inventory across multiple nights. It can also coordinate hold expiration and payment state.
- Design a wire transfer systemDistributed transactions can atomically record balanced ledger entries and transfer state. The external bank call remains outside that transaction.
- Design Netflix's concurrent screen limitStrong transactional admission checks can stop concurrent regional requests from allocating more account-wide playback slots than the limit allows.
Common questions
Is Google Spanner SQL or NoSQL?
Google Spanner is a relational SQL database with schemas, indexes, and ACID transactions. It also distributes storage and execution across servers. A relational model does not remove the need to consider keys, locality, and distributed query costs.
Is Google Spanner strongly consistent?
Yes. Its default serializable transactions provide external consistency, which includes respect for real-time transaction order. It also supports stale reads and other isolation choices. Describe the guarantees of the mode your application uses.
What is the difference between Google Spanner and Cloud SQL?
Spanner supports horizontally distributed relational storage and transactions, including multi-region configurations. Cloud SQL manages conventional database engines such as PostgreSQL and MySQL. It generally uses a writable primary with replicas and high-availability options. Choose based on scale, SQL compatibility, latency, and failure requirements. Do not assume that Spanner is always the next upgrade.