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.
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.
- MySQL, PostgreSQL, SQL Server
- One primary, optional replicas
- Simplest and cheapest start
- Vertical scaling for writes
- PostgreSQL-compatible
- Faster reads and analytics on the same data
- Read pools scale out
- Higher cost than Cloud SQL
- Horizontal write scaling
- Strong consistency across regions
- Not a drop-in for existing Postgres
- Plan for the schema design
- Cloud Run service to Auth Proxy or connector: Open connection (small pool)1Open connection (small pool)
- Auth Proxy or connector to Cloud SQL instance: Encrypted tunnel, IAM checked2Encrypted tunnel, IAM checked
- Cloud SQL instance to Auth Proxy or connector: Database session3Database session
- Auth Proxy or connector to Cloud Run service: Query results returned4Query results returned
- 1Application
Retries on a dropped connection
- 2Primary instance
Accepts the write
- 3HA standby
Other zone; takes over on failure
- 4Backups 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.
- Step 1Find max_connections
Database setting
- Step 2Subtract admin room
Keep a few free
- Step 3Divide by max instances
Gives pool size
- Step 4Set 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.
- 1Compare scale and consistency~35m
- 2Connect with the Cloud SQL Auth Proxy~35m
- 3Pool connections from Cloud Run~35m
- 4Add read replicas and high availability~35m
- 5Restore 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
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:17Local 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.
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-modelPoint-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.
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-restoreQuick 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.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Run the local Postgres model and create a table with two rows.
- Exercise 2medium
Pick a pool size. Database max_connections is 100, you keep 10 for admin, and Cloud Run max instances is 15.
- 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.