This chapter covers Google Cloud Spanner, a fully managed, globally distributed, horizontally scalable relational database service that combines the benefits of traditional SQL databases with the scalability of NoSQL systems. For the ACE exam, Cloud Spanner appears in the Planning Solutions domain (Objective 2.3) and is tested on approximately 5-8% of questions. You must understand its architecture, key features like strong consistency and horizontal scaling, and when to choose it over Cloud SQL or Bigtable. This chapter provides a deep dive into Spanner's internals, configuration, and exam-critical details.
Jump to a section
Imagine a global bank with branches in New York, London, Tokyo, and Sydney. Each branch has its own local ledger that records all transactions, but the bank also maintains a single, unified global ledger that is replicated across all branches. When a customer deposits money at the New York branch, the local ledger updates instantly, but the global ledger must be updated at all branches. The bank uses a special system: each transaction is timestamped with a globally unique, monotonically increasing sequence number from a central atomic clock. Before a branch can read the balance, it must see the latest sequence number. If two customers try to withdraw from the same account simultaneously in different branches, the bank's system uses a distributed consensus protocol (like Paxos) to ensure only one succeeds, and the other gets an 'abort' message. The branches communicate over dedicated high-speed links, but even if the Sydney branch loses connectivity, it can still serve read requests using its local copy, though writes are queued until reconnection. This mirrors Cloud Spanner's architecture: each node (branch) has local storage and compute, but all nodes share a global, strongly consistent view via TrueTime timestamps and Paxos-based replication. Spanner automatically handles sharding (splitting data across nodes) and rebalancing, just as the bank might reassign customers to different branches based on load.
What is Cloud Spanner?
Cloud Spanner is a fully managed, globally distributed, horizontally scalable relational database service from Google Cloud. It is the first and only database that offers both strong consistency across regions and horizontal scaling, which traditionally were mutually exclusive. Spanner is designed for mission-critical applications that require ACID transactions, SQL queries, and high availability across multiple regions, such as financial trading platforms, global inventory systems, and gaming leaderboards.
Spanner is not a traditional relational database like MySQL or PostgreSQL; it is a NewSQL database that internally uses a distributed architecture with atomic clocks and Paxos consensus to achieve global strong consistency. The ACE exam expects you to know when to use Spanner versus Cloud SQL (for single-region, smaller workloads) or Bigtable (for NoSQL, high-throughput, low-latency analytical workloads).
How Spanner Works Internally
Spanner's architecture consists of several layers: - Zones: A Spanner instance spans multiple zones (which are roughly equivalent to Google Cloud zones). Each zone contains one or more servers that store data replicas. - Tablets: Data is horizontally partitioned into tablets, each of which is a contiguous range of rows. Tablets are replicated across zones using Paxos for consensus. Each tablet has a Paxos leader that handles writes. - Directory: A directory is a set of contiguous rows that form the unit of replication and load balancing. Directories are stored in tablets and can be split or merged automatically. - TrueTime: Spanner uses Google's TrueTime API, which exposes a globally synchronized clock with bounded uncertainty. TrueTime provides a monotonically increasing timestamp for each transaction, enabling global consistency without locking.
When a write transaction occurs, the client sends the write to the Paxos leader for the affected tablets. The leader assigns a TrueTime timestamp to the write and replicates it to a majority of replicas (Paxos quorum). Once the write is committed, it is visible to all subsequent reads that use timestamps greater than the commit timestamp. Reads can be served by any replica that is sufficiently up-to-date, but for strong reads, Spanner waits until the replica's timestamp is at least as recent as the read timestamp (using TrueTime).
Key Components and Defaults
Instance: A Spanner instance is a set of resources (compute and storage) that hosts databases. You can create instances in a single region or multiple regions (regional or multi-region configurations). The instance configuration determines where data is stored and replicated.
Database: A database is a logical container for tables, indexes, and other schema objects. Each database belongs to exactly one instance.
Table: Tables are similar to relational tables, with primary keys that determine the interleaving and locality of rows.
Interleaved Tables: Spanner allows you to define parent-child relationships between tables where child rows are stored physically close to their parent row (interleaved). This improves join performance for hierarchical data.
Secondary Indexes: Spanner supports global secondary indexes that are stored as separate tables and are updated asynchronously (but still strongly consistent within a few seconds).
Node Count: Each Spanner instance has a number of nodes that determine compute and storage capacity. One node provides up to 2 TB of storage and 1,000 write operations per second (approximate). You can scale nodes up or down with minimal downtime.
Replication: The number of replicas depends on the instance configuration. Regional instances have 3 replicas (one per zone), while multi-region configurations have 5 or more replicas depending on the configuration (e.g., nam3 has 5 replicas across US regions).
TrueTime Uncertainty: The TrueTime uncertainty (epsilon) is typically around 1-10 milliseconds, but Spanner's implementation accounts for this to ensure consistency.
Configuration and Verification Commands
To create a Spanner instance, you can use the gcloud command-line tool:
gcloud spanner instances create my-instance \
--config=regional-us-central1 \
--description="My Regional Instance" \
--nodes=1To create a database:
gcloud spanner databases create my-database \
--instance=my-instanceTo create a table:
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX)
) PRIMARY KEY (SingerId);To query the database using gcloud:
gcloud spanner databases execute-sql my-database \
--instance=my-instance \
--sql="SELECT * FROM Singers"To monitor instance metrics, use Cloud Monitoring:
gcloud monitoring metrics list \
--filter="metric.type = 'spanner.googleapis.com/instance/query_count'"Interaction with Related Technologies
Spanner integrates with:
- Cloud IAM: For access control, you grant roles like spanner.databaseAdmin or spanner.databaseUser.
- Cloud Key Management Service (KMS): For customer-managed encryption keys (CMEK).
- Cloud Audit Logs: For auditing operations.
- Cloud Monitoring and Logging: For performance metrics and error logs.
- Dataflow and Dataproc: For bulk data import/export.
- Cloud Functions and Cloud Run: For serverless applications that need a globally consistent database.
Spanner does not support stored procedures, triggers, or foreign key constraints (though it does support referential integrity via interleaved tables). It also does not support auto-increment; you must use a sequence or generate unique IDs manually (e.g., using UUID).
Create a Spanner Instance
Use the gcloud command or Cloud Console to create a Spanner instance. Choose an instance configuration (regional or multi-region) and specify the number of nodes. Regional configurations are cheaper and sufficient for most applications; multi-region configurations provide global availability and strong consistency. The instance is the top-level resource that contains databases. For the exam, remember that you cannot change the instance configuration after creation; you must create a new instance and migrate data.
Design the Database Schema
Define tables with primary keys that support efficient sharding. Choose primary keys that distribute load evenly; avoid monotonically increasing keys (like timestamps) as they can cause hotspots. Use interleaved tables for hierarchical data (e.g., Customers and Orders) to keep related rows physically close. Create secondary indexes for query patterns that don't use the primary key. Remember that indexes are stored as separate tables and consume additional storage.
Create the Database and Tables
Use gcloud or the Cloud Console to create a database within the instance. Then execute DDL statements to create tables, indexes, and interleaved relationships. Spanner DDL supports most standard SQL DDL, but with some limitations (no foreign keys, no triggers). Use the `CREATE TABLE` statement with `PRIMARY KEY` and `INTERLEAVE IN PARENT` clauses for interleaved tables. For secondary indexes, use `CREATE INDEX`.
Insert and Query Data
Use DML statements (INSERT, UPDATE, DELETE) or the Cloud Spanner API to manipulate data. Spanner supports both standard SQL queries and mutations (batch operations). For optimal performance, use batch writes and avoid single-row transactions. Use read-write transactions for consistent updates across multiple rows. For read-only workloads, use read-only transactions that can be served by any replica without locking.
Monitor and Scale
Monitor instance performance using Cloud Monitoring dashboards for metrics like CPU utilization, storage usage, and latency. Scale the instance by adding or removing nodes. Spanner can scale up to thousands of nodes. Use the gcloud command `gcloud spanner instances update` to change the node count. Scaling is typically seamless but may cause a brief performance impact during data rebalancing.
Enterprise Scenario 1: Global Financial Trading Platform
A large investment bank needs a database to store real-time trade orders and account balances across multiple continents. The bank requires strong consistency to ensure that a trade executed in London is immediately visible to the balance check in New York. They also need high availability with automatic failover. The bank chooses Spanner with a multi-region configuration (e.g., nam3 covering US, Europe, and Asia). They use interleaved tables for accounts and trades (each account has many trades). The primary key for trades includes the account ID and a timestamp to distribute writes. They set up 10 nodes initially to handle peak load. During a regional outage, Spanner automatically fails over to another region without data loss. Common mistakes include not distributing primary keys properly, causing hotspots on the leader node. The bank uses Cloud Monitoring to track CPU and storage and scales nodes during high-volume trading days.
Enterprise Scenario 2: Global Gaming Leaderboard
A mobile gaming company with millions of daily active users needs a real-time leaderboard that is globally consistent. Players from around the world submit scores, and the leaderboard must update instantly without conflicts. The company uses Spanner with a regional configuration (e.g., regional-us-central1) because most players are in North America. They use a single table with a composite primary key (game_id, player_id, score) and a secondary index on score for fast leaderboard queries. They use read-write transactions to update scores atomically. At peak, they handle 100,000 writes per second with 20 nodes. They use Cloud Spanner's built-in client-side buffering to batch writes. A common error is using auto-increment IDs, which cause hotspots; instead, they use random UUIDs. They also use Cloud CDN to cache leaderboard data for read-heavy workloads.
Performance Considerations and Misconfigurations
Misconfiguring Spanner often involves choosing the wrong instance configuration (regional vs. multi-region) or insufficient node count. For example, a single node can handle only about 1,000 writes/second; if the application exceeds that, writes will be throttled. Another common mistake is using a monotonically increasing primary key, which causes all writes to go to a single tablet (hotspot). Spanner automatically splits and rebalances tablets, but a hotspot can still cause performance degradation. Also, forgetting to create secondary indexes for common query patterns leads to full table scans. In production, always test with realistic traffic patterns and use the Spanner emulator for local development.
The ACE exam tests Cloud Spanner primarily under Objective 2.3 (Planning Solutions) and also in the deployment domain. Key areas include:
- When to use Spanner vs. Cloud SQL vs. Bigtable: Spanner is for global strong consistency and horizontal scaling; Cloud SQL is for single-region, smaller workloads; Bigtable is for NoSQL, high-throughput analytical workloads.
- Instance configurations: Regional (3 replicas) vs. multi-region (5+ replicas). Know the names like regional-us-central1 and nam3.
- Node scaling: One node provides ~2 TB storage and ~1,000 writes/sec. Scaling nodes is seamless.
- TrueTime and strong consistency: The key differentiator. Spanner uses TrueTime to provide external consistency (strong consistency across regions).
- Primary key design: Avoid hotspots; use random or hash-based keys. The exam may ask about interleaved tables and their benefits.
- Limitations: No foreign keys, no triggers, no auto-increment. Use sequences or UUIDs.
Common wrong answers: 1. Choosing Cloud SQL for a globally distributed application because it's cheaper. *Wrong*: Cloud SQL does not support multi-region replication with strong consistency. 2. Thinking Spanner is eventually consistent. *Wrong*: Spanner provides strong consistency across all replicas. 3. Believing you can change the instance configuration after creation. *Wrong*: You must create a new instance and migrate. 4. Using auto-increment primary keys. *Wrong*: Causes hotspots; use UUIDs or hash keys.
Edge cases:
Spanner supports up to 10,000 nodes per instance.
The maximum database size is 2 TB per node, but you can scale nodes.
Spanner does not support read replicas in the traditional sense; all replicas can serve reads (with appropriate staleness).
For read-only transactions, you can use stale reads (e.g., 15-second staleness) to reduce latency.
Elimination strategy: If a question asks for a globally consistent relational database with horizontal scaling, eliminate Cloud SQL and Bigtable. If it asks for strong consistency across regions, eliminate Bigtable (eventual consistency) and Cloud SQL (single-region). If it asks about low-latency reads for analytical workloads, Bigtable is better.
Cloud Spanner is the only Google Cloud database that provides both global strong consistency and horizontal scaling.
Use regional instances for single-region deployments; multi-region instances for global availability (e.g., nam3, eur3, asia1).
One node provides approximately 2 TB of storage and 1,000 writes per second (rough estimate).
Primary keys must be designed to avoid hotspots; use random or hash-based keys, not monotonically increasing values.
Spanner does not support foreign keys, triggers, stored procedures, or auto-increment columns.
TrueTime provides global timestamps with bounded uncertainty (epsilon ~1-10 ms) ensuring external consistency.
Interleaved tables store child rows physically close to parent rows for faster joins.
You cannot change the instance configuration after creation; you must create a new instance and migrate.
For read-only workloads, use stale reads (e.g., 15-second staleness) to reduce latency and avoid locking.
Spanner integrates with IAM, KMS, Cloud Monitoring, and Audit Logs.
These come up on the exam all the time. Here's how to tell them apart.
Cloud Spanner
Globally distributed with strong consistency across regions
Horizontally scalable (add nodes for more capacity)
Supports up to 10,000 nodes and petabytes of data
No foreign keys, triggers, or stored procedures
Higher cost due to distributed architecture
Cloud SQL
Single-region deployment (regional or zonal)
Vertically scalable (larger machine types)
Limited to ~64 TB per instance (for MySQL)
Full support for foreign keys, triggers, stored procedures
Lower cost for small to medium workloads
Cloud Spanner
Relational (SQL) with ACID transactions
Strong consistency across all replicas
Best for transactional workloads with complex queries
Supports secondary indexes and interleaved tables
Slower for simple key-value lookups compared to Bigtable
Bigtable
NoSQL (wide-column) with no SQL support
Eventually consistent (unless using single-cluster routing)
Best for analytical workloads (time-series, IoT) with high throughput
No secondary indexes; only row key lookups
Lower latency for simple key-value reads/writes
Mistake
Spanner is just a distributed version of MySQL.
Correct
Spanner is a NewSQL database with a completely different architecture. It uses Paxos for consensus, TrueTime for global timestamps, and a distributed storage layer. It does not support MySQL features like stored procedures, triggers, or foreign keys. The SQL dialect is similar but not identical.
Mistake
Spanner provides eventual consistency across regions.
Correct
Spanner provides strong consistency (external consistency) across all replicas. Writes are committed with TrueTime timestamps, and all subsequent reads see the latest write. This is a key differentiator from eventually consistent databases like Bigtable.
Mistake
You can change the instance configuration from regional to multi-region later.
Correct
Once created, the instance configuration cannot be changed. To switch from regional to multi-region, you must create a new instance with the desired configuration and migrate data (e.g., using Dataflow or export/import).
Mistake
Spanner automatically handles all primary key distribution issues.
Correct
Spanner splits tablets automatically, but if you use a monotonically increasing primary key (e.g., timestamp), all writes go to a single tablet (hotspot) until the tablet splits. You should design primary keys that distribute writes evenly, such as using hash prefixes or random UUIDs.
Mistake
Spanner supports foreign key constraints.
Correct
Spanner does not support foreign key constraints. However, you can enforce referential integrity using interleaved tables (parent-child relationships) or application logic. Interleaved tables ensure child rows are stored physically close to the parent row, improving join performance.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
A regional instance stores data in 3 zones within a single region, providing high availability within that region. A multi-region instance stores data in multiple regions (e.g., nam3 covers US, Europe, and Asia) with 5 or more replicas, providing global availability and strong consistency. Multi-region instances have higher latency due to geographic distance but offer disaster recovery across continents. The exam may ask you to choose between them based on requirements.
Spanner uses TrueTime, a globally synchronized clock with bounded uncertainty, to assign timestamps to transactions. Writes are committed via Paxos consensus across replicas. Reads are served by replicas that have seen the latest timestamp (or wait until the replica's clock advances). This ensures external consistency: if transaction A commits before transaction B starts, B will see A's writes.
No, Spanner does not support auto-increment. You must generate unique primary keys manually. Use UUIDs (e.g., `GENERATE_UUID()` function) or a sequence generator. Avoid monotonically increasing keys like timestamps because they cause hotspots. The exam may test this by asking how to design primary keys for Spanner.
You can use Dataflow templates (like 'Cloud Spanner to Cloud SQL' or vice versa) or export/import. For Cloud SQL to Spanner, export Cloud SQL data to CSV, then import into Spanner using the gcloud command or Dataflow. Alternatively, use the Spanner migration tool (Harbourbridge) for schema conversion. The exam may ask about migration tools.
Spanner does not support foreign keys, triggers, stored procedures, or auto-increment. It also has a limited set of SQL functions (no window functions in some versions). The maximum row size is 10 MB, and the maximum number of columns is 1,024. These limitations are important for exam questions about schema design.
Use Cloud Monitoring to track metrics like CPU utilization, storage usage, query latency, and transaction throughput. Key metrics include `spanner.googleapis.com/instance/cpu_utilization`, `spanner.googleapis.com/instance/storage_used`, and `spanner.googleapis.com/instance/query_count`. Set up alerts for high CPU or storage. The exam may ask about monitoring tools.
Technically yes, but it is overkill and expensive for small applications. Spanner is designed for large-scale, globally distributed workloads. For a small application, use Cloud SQL or Firestore. The exam expects you to recommend Spanner only when horizontal scaling and global strong consistency are required.
You've just covered Cloud Spanner — now see how well it sticks with free ACE practice questions. Full explanations included, no account needed.
Done with this chapter?