Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Google Cloud (GCP)›Cloud SQL and Cloud Spanner

Data and Analytics

Cloud SQL and Cloud Spanner

Cloud SQL and Cloud Spanner are managed relational database services designed to handle structured data with varying degrees of scale and global consistency. Cloud SQL provides a fully managed implementation of industry-standard relational engines for vertical scaling needs, while Cloud Spanner offers a unique architecture for horizontal scale with external consistency. Understanding these services is critical for architecting applications that balance performance, consistency, and operational overhead in global deployments.

Understanding Cloud SQL Architecture

Cloud SQL is a fully managed service that automates database administration tasks such as backups, patching, and replication for standard relational database engines. It functions by providing a persistent instance that manages the storage layer while offloading the complexity of database maintenance to the provider. The core value proposition is simplicity; if your application requires a traditional relational model and fits within a single primary instance's capacity, Cloud SQL is the ideal choice. It works by utilizing locally attached high-performance storage while ensuring data durability through automated background replication. When scaling, Cloud SQL typically relies on vertical scaling, meaning you increase the CPU and RAM of the instance. Because it maintains standard protocol compatibility, it serves as a drop-in replacement for existing deployments. The underlying infrastructure ensures that your data remains available and secure without requiring deep administrative knowledge of the underlying operating system or database internals, effectively abstracting the hardware layer entirely.

# Example of creating a Cloud SQL instance via command line
gcloud sql instances create my-db-instance \
  --database-version=POSTGRES_14 \
  --tier=db-f1-micro \
  --region=us-central1 # Sets the geographical deployment region

High Availability and Read Replication

To increase the durability and throughput of Cloud SQL, we employ high availability and read replicas. High availability (HA) functions by maintaining a standby instance in a different zone; if the primary instance fails, the service performs an automated failover. This works by using synchronous replication to ensure the standby is always in sync with the primary, preventing data loss during unexpected events. For read-heavy workloads, read replicas allow you to scale horizontally for query operations. These replicas ingest data asynchronously from the primary, ensuring that your primary write instance remains performant while offloading read traffic. It is important to remember that read replicas introduce eventual consistency, as there is a slight lag between the primary write and the replica update. By distributing read-only traffic across multiple replicas, you can handle significantly higher concurrency without hitting the limitations of a single instance's memory or CPU bandwidth.

# Creating a read replica to offload query traffic
gcloud sql instances create read-replica-1 \
  --master-instance-name=my-db-instance \
  --tier=db-f1-micro # Replicas can be scaled independently of the primary

The Challenge of Distributed Systems and Spanner

As applications expand globally, the limitations of traditional relational databases, such as Cloud SQL, become apparent. The primary challenge is scaling write operations beyond the limits of a single node while maintaining strict transactional integrity. Cloud Spanner solves this by using a distributed, sharded architecture that allows for horizontal scaling of both storage and compute across different regions. It works by using specialized hardware clocks combined with software consensus algorithms to achieve external consistency—the gold standard of transactional accuracy. Unlike traditional databases that suffer from split-brain scenarios or require manual sharding, Spanner abstracts the distribution of data. It automatically shards data based on the load and size, migrating chunks of data across nodes without downtime. This makes it an ideal solution for mission-critical applications that demand global scale, strong consistency, and high availability, which would otherwise be impossible to achieve with a standard single-node database design.

# Defining a Spanner instance for global distribution
gcloud spanner instances create global-db \
  --config=regional-us-central1 \
  --nodes=1 # Scale nodes to increase throughput horizontally

Schema Design and Transactions in Spanner

Effective use of Spanner requires careful attention to schema design, particularly regarding interleaving and primary keys. Because Spanner is a distributed system, how you organize your tables significantly impacts performance. Interleaving allows you to physically store child rows in the same data split as the parent rows, which drastically reduces network latency during join operations. Furthermore, Spanner supports multi-statement transactions that remain consistent across rows, tables, and even shards. This works by utilizing the Paxos consensus protocol, ensuring that a majority of replicas agree on a write before it is committed. You should avoid monotonically increasing primary keys, as these create 'hotspots' where all write traffic is directed to a single node, nullifying the benefits of horizontal sharding. By using UUIDs or bit-reversed keys, you spread the load evenly across all available shards, allowing Spanner to effectively utilize its distributed cluster for maximum write throughput.

-- Defining a table in Spanner with interleaving for performance
CREATE TABLE Users (
  UserId INT64 NOT NULL,
  Name STRING(MAX)
) PRIMARY KEY (UserId);

CREATE TABLE Orders (
  UserId INT64 NOT NULL,
  OrderId INT64 NOT NULL,
  Item STRING(MAX)
) PRIMARY KEY (UserId, OrderId),
  INTERLEAVE IN PARENT Users ON DELETE CASCADE; -- Improves data locality

Choosing Between SQL and Spanner

Deciding between Cloud SQL and Cloud Spanner comes down to your scale and consistency requirements. You reach for Cloud SQL when your application workload is predictable, fits within the capacity of a standard relational instance, and requires the familiarity of traditional database management. It is optimized for smaller to medium-scale deployments where operational simplicity is preferred. Conversely, you choose Cloud Spanner when your application requirements include massive global scale, the need for unlimited horizontal write growth, and strict transactional consistency that cannot be relaxed. Spanner is a more complex and typically more expensive option, but it eliminates the need for manual sharding and maintenance of distributed consensus. By evaluating the trade-offs in write latency, consistency models, and the need for global data availability, architects can effectively choose the service that aligns with the long-term growth and technical constraints of their specific application environment.

# Checking the status of a Spanner database operation
gcloud spanner databases describe orders-db --instance=global-db

Key points

  • Cloud SQL provides a fully managed environment for traditional relational databases, handling maintenance tasks like backups automatically.
  • Vertical scaling in Cloud SQL is achieved by upgrading the instance machine type, which is effective for workloads that fit on a single node.
  • Read replicas in Cloud SQL allow for horizontal scaling of query workloads but introduce the concept of eventual consistency.
  • Cloud Spanner is designed for massive horizontal scale, allowing both compute and storage to grow across multiple regions.
  • External consistency in Spanner is maintained through the use of specialized clocks and consensus protocols across distributed nodes.
  • Effective Spanner schema design requires avoiding hot keys, which can be mitigated by using non-sequential primary keys like UUIDs.
  • Interleaving in Spanner improves performance by physically co-locating child records with their parent records to reduce network overhead.
  • The decision between Cloud SQL and Spanner hinges on whether the application requires simple, single-instance management or global, multi-node horizontal scalability.

Common mistakes

  • Mistake: Choosing Cloud SQL for globally distributed write-heavy workloads. Why it's wrong: Cloud SQL is a regional service and is not designed for global write scalability. Fix: Use Cloud Spanner for globally distributed, transactional consistency at scale.
  • Mistake: Assuming Cloud SQL automatically handles database schema shards. Why it's wrong: Cloud SQL is a managed relational database that does not natively shard data; you would need to manage sharding at the application layer. Fix: Use Cloud Spanner, which handles database sharding and distribution automatically.
  • Mistake: Neglecting to set up private IP for Cloud SQL connectivity. Why it's wrong: Relying on public IP increases the attack surface for database instances. Fix: Use Private Service Access to connect to Cloud SQL instances within your VPC network.
  • Mistake: Treating Cloud Spanner as a drop-in replacement for any SQL database without considering data modeling. Why it's wrong: Cloud Spanner requires careful primary key design to avoid hotspots. Fix: Use non-sequential UUIDs or bit-reversed keys in your schema to distribute write loads evenly across nodes.
  • Mistake: Failing to manage Cloud SQL connection limits for high-traffic apps. Why it's wrong: Each application connection consumes resources, and hitting the connection limit leads to application downtime. Fix: Use the Cloud SQL Auth Proxy to manage and pool connections efficiently.

Interview questions

What is Cloud SQL and when should you choose it for a Google Cloud project?

Cloud SQL is a fully managed relational database service in Google Cloud that supports MySQL, PostgreSQL, and SQL Server. You should choose Cloud SQL when you need a traditional relational database management system that provides ease of use, automated backups, high availability, and seamless integration with other GCP services. It is ideal for web frameworks, CRM systems, and e-commerce applications where standard SQL compliance and vertical scaling are sufficient to handle your transactional workload.

How does Cloud Spanner differ from Cloud SQL in terms of architectural scalability?

Cloud SQL is primarily designed for vertical scaling, meaning you increase the CPU and memory of a single instance to handle more load, though it supports read replicas for horizontal read scaling. In contrast, Cloud Spanner is a globally distributed, horizontally scalable database. It achieves this by sharding data automatically across many nodes while maintaining strong external consistency. Choose Spanner when your application requires massive scale, global consistency, and uptime beyond what a single-region relational instance can provide.

What mechanism does Cloud Spanner use to achieve global consistency across distributed nodes?

Cloud Spanner uses TrueTime, a technology that relies on atomic clocks and GPS receivers in Google data centers. TrueTime provides a highly accurate, synchronized time reference with bounded uncertainty across the globe. By using these timestamps for transaction ordering, Spanner ensures external consistency—meaning that if transaction A completes before transaction B starts, A is guaranteed to be visible to B, even if the transactions occur on servers thousands of miles apart.

Explain how to manage High Availability (HA) in Cloud SQL versus Cloud Spanner.

In Cloud SQL, High Availability is achieved by configuring a failover replica in a different zone. If the primary instance fails, Google Cloud automatically promotes the standby replica. In Cloud Spanner, HA is built into the architecture. You select a multi-region configuration, and Spanner automatically replicates data across multiple regions. Because Spanner is inherently distributed, it provides a 99.999% availability SLA, as there is no single master instance that needs to be 'failed over' in the traditional sense; the service remains active even if an entire region goes offline.

Compare the use cases for Cloud SQL and Cloud Spanner when migrating a legacy monolithic application.

When migrating a monolith, Cloud SQL is often the first choice because it is drop-in compatible with existing MySQL or PostgreSQL codebases, requiring minimal schema changes. However, if that legacy application is struggling with performance limits due to write contention or database size, you should consider Cloud Spanner. While Spanner requires schema design adjustments—specifically focusing on primary key distribution to avoid hotspots—it removes the architectural ceiling of a single-node database, allowing the monolith to scale globally without rewriting the application core.

How do you handle schema design in Cloud Spanner to prevent performance 'hotspots'?

Performance hotspots in Cloud Spanner occur when you use monotonically increasing values, like timestamps or sequential integers, as the primary key. This causes all inserts to hit the same partition, creating a bottleneck. To fix this, you should use techniques like bit-reversing the value, using a UUID, or pre-splitting the database. For example, instead of a simple sequential ID, you might prepend a hash of the ID. Here is a conceptual approach: 'CREATE TABLE Users (UserID STRING(MAX), Data STRING(MAX)) PRIMARY KEY (UserID);' where the UserID is generated as a random UUID rather than a sequential counter to ensure write distribution.

All Google Cloud (GCP) interview questions →

Check yourself

1. An application requires high availability and strong consistency across multiple regions with a global footprint. Which service is the most appropriate choice?

  • A.Cloud SQL with Read Replicas
  • B.Cloud Spanner
  • C.Cloud SQL with Failover Replica
  • D.Cloud Storage with database drivers
Show answer

B. Cloud Spanner
Cloud Spanner provides global consistency and high availability across regions. Cloud SQL is regional; while read replicas provide global read-only access, they do not support synchronous multi-region writes or global strong consistency.

2. Why is primary key design critical in Cloud Spanner compared to traditional relational databases like Cloud SQL?

  • A.Cloud Spanner requires keys to be in integer format only
  • B.Primary keys in Spanner cannot be modified after table creation
  • C.Poorly designed keys can lead to write hotspots in the distributed architecture
  • D.Cloud SQL does not support primary keys, while Spanner does
Show answer

C. Poorly designed keys can lead to write hotspots in the distributed architecture
Spanner shards data across nodes based on primary keys. Sequential keys create hotspots on a single node. Cloud SQL runs on a single node, so key order does not impact distribution performance. Spanner supports various key types, and keys can be managed.

3. Your team needs to migrate an existing MySQL application to Google Cloud with minimal code changes. Which service should you choose?

  • A.Cloud Spanner
  • B.Cloud SQL for MySQL
  • C.BigQuery
  • D.Firestore
Show answer

B. Cloud SQL for MySQL
Cloud SQL for MySQL is a fully managed service designed to support existing MySQL applications with minimal configuration changes. Spanner uses a different SQL dialect and requires significant schema refactoring, while BigQuery and Firestore are not relational databases.

4. What is the primary benefit of using the Cloud SQL Auth Proxy when connecting to a Cloud SQL instance?

  • A.It automatically upgrades the database version
  • B.It provides secure, encrypted connections without requiring SSL certificates to be managed manually
  • C.It increases the storage capacity of the database automatically
  • D.It allows the database to be accessed without a GCP service account
Show answer

B. It provides secure, encrypted connections without requiring SSL certificates to be managed manually
The Cloud SQL Auth Proxy secures connections by wrapping them in an encrypted tunnel and handling IAM-based authentication, removing the complexity of managing SSL certificates. It does not handle version upgrades, storage scaling, or bypass IAM requirements.

5. A global application expects varying traffic patterns and needs to scale compute resources horizontally without downtime. Which architecture is best?

  • A.Cloud SQL with vertical scaling (upgrading machine type)
  • B.Cloud SQL with multiple read replicas in different regions
  • C.Cloud Spanner with node count adjustment
  • D.Cloud SQL on a single large instance to avoid replica latency
Show answer

C. Cloud Spanner with node count adjustment
Cloud Spanner allows for seamless horizontal scaling by adding nodes without downtime. Cloud SQL generally requires vertical scaling (changing instance size), which often involves a brief restart. Read replicas add read capacity but do not address write scaling or horizontal compute expansion.

Take the full Google Cloud (GCP) quiz →

← PreviousPub/Sub — Event MessagingNext →Vertex AI — Training Pipelines

Google Cloud (GCP)

20 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app