SkillByAIOpen interactive version →

Lesson 14 / 25

Cloud SQL, AlloyDB and Spanner

Pick a managed relational database for the scale and consistency you need.

Three relational options

Cloud SQL is managed MySQL, PostgreSQL and SQL Server: automated backups, point-in-time recovery, patching and high availability through a regional instance with a standby in another zone and automatic failover. Read replicas offload reads, including in other regions. AlloyDB for PostgreSQL is a PostgreSQL-compatible database built for higher performance, with a columnar engine for analytical queries alongside transactions. Spanner is a horizontally scalable relational database with strong consistency, SQL and transactions across regions, using Google's TrueTime clocks; it suits global systems that outgrow a single primary, such as large payment or inventory systems. Connect to Cloud SQL using private IP or the Cloud SQL Auth Proxy and language connectors, which handle TLS and IAM-based authentication, instead of opening the database to 0.0.0.0/0.

An HA PostgreSQL instance and a secure connection

IAM database authentication avoids storing a database password for the app.

gcloud sql instances create shop-pg --database-version=POSTGRES_16 \
  --region=asia-south1 --availability-type=REGIONAL \
  --tier=db-custom-2-7680 --storage-auto-increase \
  --database-flags=cloudsql.iam_authentication=on

gcloud sql databases create shop --instance=shop-pg
gcloud sql users create orders-api@shop-dev-123456.iam --instance=shop-pg \
  --type=cloud_iam_service_account

# local development through the Auth Proxy (listens on 127.0.0.1:5432)
cloud-sql-proxy --auto-iam-authn shop-dev-123456:asia-south1:shop-pg

Car, sports car and train network

Cloud SQL is a reliable family car, AlloyDB a faster car that also tows analytics, and Spanner a national rail network: built for enormous scale across cities, and overkill for a trip to the market.

Quick check: Which option gives Cloud SQL automatic failover to another zone?

  • A read replica in the same zone
  • Larger storage
  • Public IP
  • Availability type REGIONAL (high availability)
Answer

Availability type REGIONAL (high availability) — A regional (HA) instance keeps a standby in another zone and fails over automatically.