Tech Interview RoomStudy guides

Cloud SQL

How do you migrate a database to Cloud SQL with minimal downtime?

Advanced · 2 min read · Lesson 5 of 5 in Cloud SQL

Options

  • Database Migration Service (DMS): Google's managed service for MySQL and PostgreSQL (and Oracle/SQL Server to PostgreSQL or AlloyDB via conversion workspaces). It performs an initial full dump and then continuous replication (CDC) from the source, so the target stays in sync until you cut over.
  • Native tools: pg_dump/pg_restore or mysqldump (simple, but downtime equals dump and restore time); logical replication for PostgreSQL; external-server replication for MySQL (Cloud SQL as a replica of an external primary).
  • Import from Cloud Storage: SQL dump or CSV files with gcloud sql import.

A low-downtime plan

  1. Assess: versions, extensions and features supported by Cloud SQL, database size, change rate, network connectivity (VPN or Interconnect for on-premises sources).
  2. Prepare the source: enable binary logging with GTIDs (MySQL) or logical decoding with pglogical (PostgreSQL), and create a replication user.
  3. Create the migration job (continuous); wait for the full load, then for replication lag near zero.
  4. Validate: row counts and checksums, run the application's test suite against the target, compare query performance.
  5. Cut over: stop writes on the source (maintenance mode), wait for lag to reach zero, promote the Cloud SQL instance, switch application connection strings (via config or DNS), then re-enable writes.
  6. Fallback plan: keep the old source untouched for a while, or set up reverse replication.

Gotchas

Users, grants and some objects are not migrated automatically; sequences may need resetting; tables without primary keys can break CDC; large objects and unsupported extensions need special handling.

Example

bash
# Simple path for a small PostgreSQL database (downtime = export + import)
pg_dump -Fc -h old-db -U app orders > orders.dump
gsutil cp orders.dump gs://my-migrations/orders.dump
gcloud sql import sql orders-db gs://my-migrations/orders.sql --database=orders   # for plain SQL dumps

# Low-downtime path: a DMS continuous migration job (source and destination profiles created first)
gcloud database-migration migration-jobs create orders-to-cloudsql \
  --region=asia-south1 \
  --type=CONTINUOUS \
  --source=onprem-pg-profile \
  --destination=orders-db-profile \
  --peer-vpc=projects/my-proj/global/networks/prod-vpc

gcloud database-migration migration-jobs start orders-to-cloudsql --region=asia-south1
# ...validate, stop writes, wait for zero lag...
gcloud database-migration migration-jobs promote orders-to-cloudsql --region=asia-south1

Follow-up questions interviewers ask

Try answering each one before you open it.

How do you keep the cutover window short in a Spring Boot application?

Externalise the datasource URL and credentials (Secret Manager, config server or environment variables) so the switch is a config change and rolling restart; use read-only or maintenance mode to stop writes cleanly; and run Flyway/Liquibase migrations against the new instance before the switch so the schema version matches.