Skip to content
Visual explainer 3h to learn 5 steps

Compare Cloud SQL, AlloyDB and Spanner

Choose Cloud SQL, AlloyDB or Spanner from latency, consistency and availability needs, and connect to Cloud SQL or a local Postgres model of it.

By Invicly Tech, , in Google Cloud Engineering and Services. About 8 minutes to read.

Topic 14 of 32

In short

Google Cloud offers three managed SQL families. Cloud SQL runs MySQL, PostgreSQL or SQL Server for ordinary applications. AlloyDB is PostgreSQL-compatible and tuned for heavier workloads. Spanner scales writes across many machines and regions with strong consistency.

Think of it like this: Cloud SQL is a well-run single shop, AlloyDB is the same shop with a faster kitchen, and Spanner is a chain of shops that all share one till and always agree on the total.

Explained simply

A relational database stores data in tables and answers questions in SQL. Google Cloud runs these for you, so you do not patch servers or manage backups by hand. The three main choices are Cloud SQL, AlloyDB and Spanner.

Cloud SQL is the everyday choice. It runs MySQL, PostgreSQL or SQL Server with one main instance, optional copies for reading, and automatic backups. If your app fits on one big machine, Cloud SQL is usually the right and cheapest start.

AlloyDB is for PostgreSQL apps that need more speed, especially mixed workloads with heavy reads or reports. Spanner is different: it spreads data across many machines and even regions while keeping answers consistent. It is powerful, but you design your schema for it.

Connections need care. Every Cloud Run instance opens its own connections. A hundred instances with ten connections each is a thousand, which can exceed the database limit. Keep each pool small.

Press Play on a diagram to walk through it one step at a time.

CompareChoosing a relational service
Cloud SQL
  • MySQL, PostgreSQL, SQL Server
  • One primary, optional replicas
  • Simplest and cheapest start
  • Vertical scaling for writes
AlloyDB
  • PostgreSQL-compatible
  • Faster reads and analytics on the same data
  • Read pools scale out
  • Higher cost than Cloud SQL
Spanner
  • Horizontal write scaling
  • Strong consistency across regions
  • Not a drop-in for existing Postgres
  • Plan for the schema design
SequenceCloud Run reaching Cloud SQL safely
Cloud Run service
Auth Proxy or connector
Cloud SQL instance
  1. 1Open connection (small pool)
    Cloud Run service to Auth Proxy or connector: Open connection (small pool)
  2. 2Encrypted tunnel, IAM checked
    Auth Proxy or connector to Cloud SQL instance: Encrypted tunnel, IAM checked
  3. 3Database session
    Cloud SQL instance to Auth Proxy or connector: Database session
  4. 4Query results returned
    Auth Proxy or connector to Cloud Run service: Query results returned
LayersWhere a write is protected
  1. 1
    Application

    Retries on a dropped connection

  2. 2
    Primary instance

    Accepts the write

  3. 3
    HA standby

    Other zone; takes over on failure

  4. 4
    Backups and logs

    Allow point-in-time recovery

In depth

Choosing from latency, consistency and availability

Ask what limits you today. If one machine can handle your writes, choose Cloud SQL. If you are on PostgreSQL and need faster queries or analytics on live data, look at AlloyDB. If writes must scale beyond one machine or a service must survive the loss of a whole region with strong consistency, look at Spanner.

Be honest about migration cost: Cloud SQL and AlloyDB speak familiar SQL dialects, while Spanner needs schema and key design work. Check current feature and price pages, because editions and limits change.

  • Cloud SQL: default for most apps
  • AlloyDB: PostgreSQL with more performance headroom
  • Spanner: horizontal writes, multi-region
  • Measure first, then upgrade

Connecting and pooling from Cloud Run

The Cloud SQL Auth Proxy opens an encrypted connection and checks IAM, so you do not allow-list IP addresses. Run it on your laptop for testing. On Cloud Run you attach the instance with the add-cloudsql-instances setting and connect through a Unix socket under /cloudsql, or use a language connector.

Set a small pool per instance. A good check: maximum Cloud Run instances times pool size must stay below the database max_connections, with room left for admin tools.

FlowConnection budget check
  1. Step 1
    Find max_connections

    Database setting

  2. Step 2
    Subtract admin room

    Keep a few free

  3. Step 3
    Divide by max instances

    Gives pool size

  4. Step 4
    Set pool size and Cloud Run max instances

    Match the budget

Replicas, high availability and recovery

High availability places a standby in a different zone. If the primary fails, the service fails over, and your app reconnects. Read replicas copy data to extra instances so reads can be spread out, but they lag slightly and are not the failover mechanism.

Point-in-time recovery uses backups plus transaction logs. You restore to a chosen timestamp by cloning into a new instance, check the data, then switch the app or copy rows back. Practise this before you need it.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Compare scale and consistency~35m
  2. 2
    Connect with the Cloud SQL Auth Proxy~35m
  3. 3
    Pool connections from Cloud Run~35m
  4. 4
    Add read replicas and high availability~35m
  5. 5
    Restore with point-in-time recovery~35m

Key points

  • Start from the need: latency, consistency, size and availability. Most new apps fit Cloud SQL; move up only when a measured limit appears.
  • The Cloud SQL Auth Proxy (or a language connector) encrypts traffic and checks IAM, so you avoid allow-listing IP addresses.
  • Every Cloud Run instance has its own pool. Total connections are instances times pool size, and it must stay under the database limit.
  • High availability keeps a standby in another zone for failover. Read replicas serve reads; they are not a failover plan on their own.
  • Point-in-time recovery needs backups and logs turned on. A restore creates a new instance, so you switch the app over afterwards.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
cloud-sql-proxy <project-id>:europe-west2:orders-db --port 5432 &

psql "host=127.0.0.1 port=5432 user=app dbname=orders"

# No Google Cloud account? Model it locally:
docker run --rm -e POSTGRES_PASSWORD=example -p 5432:5432 postgres:17

Local connection through the proxy. Placeholder connection name.

Local model of Cloud SQL for PostgreSQL

No account needed. Prerequisite: Docker. This runs a throwaway Postgres, creates a table and keeps a small pool in the app. Expected evidence: the select returns one row. Cleanup: the container is removed when stopped.

bash
docker run --rm -d --name pg-model -e POSTGRES_PASSWORD=example \
  -p 5432:5432 postgres:17
sleep 3
docker exec pg-model psql -U postgres \
  -c "create table orders(id int primary key, total numeric);" \
  -c "insert into orders values (1, 19.99);" \
  -c "select * from orders;"
docker stop pg-model

Point-in-time restore plan

At 10:15 someone deleted rows by mistake. Prerequisite: backups and point-in-time recovery were on beforehand. Clone to a new instance at 10:14, verify, then repair production. Cleanup: delete the clone when finished, because it costs money.

bash
gcloud sql instances clone orders-db orders-db-restore \
  --point-in-time=2026-12-02T10:14:00Z
gcloud sql connect orders-db-restore --user=postgres
# copy back the missing rows, then:
gcloud sql instances delete orders-db-restore

Quick reference

  • Start with Cloud SQL unless a measured limit says otherwise.
  • Auth Proxy: encrypted, IAM-checked, no IP allow-list needed.
  • Pool budget: max instances x pool size < max_connections.
  • HA standby handles failover; read replicas handle reads.
  • Point-in-time restore creates a new instance.
  • Spanner gives strong consistency with horizontal write scaling.

Common mistakes

  • Setting a large pool per instance, then scaling Cloud Run to 100 instances and running out of connections.
  • Treating a read replica as high availability. Failover uses the HA standby.
  • Choosing Spanner for a small app because it sounds safer. It adds cost and design work you may not need.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which service targets PostgreSQL apps that need more performance?

Question 2

2Cloud Run scales to 50 instances with pool size 20. How many connections can open?

Question 3

3What does a read replica NOT do by itself?

Question 4

4A point-in-time restore gives you what?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Run the local Postgres model and create a table with two rows.

  2. Exercise 2medium

    Pick a pool size. Database max_connections is 100, you keep 10 for admin, and Cloud Run max instances is 15.

  3. Exercise 3hard

    Write a design note choosing a database for an order service that must grow to 5000 writes per second in two regions with strong consistency.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need the Auth Proxy on Cloud Run?
You need a secure path to the instance. Cloud Run can attach the instance for you, or you can use a connector library. The proxy is the common route from a laptop.
Can I use Cloud SQL as a free learning database?
No, instances are billed while they run. Use a local Postgres container or a trial credit, and delete instances when done.
Is a read replica a backup?
No. A replica copies mistakes too. Keep backups and point-in-time recovery as well.

Go deeper

ShareXLinkedInWhatsApp