How do you migrate a database to Cloud SQL with minimal downtime?
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_restoreormysqldump(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
- Assess: versions, extensions and features supported by Cloud SQL, database size, change rate, network connectivity (VPN or Interconnect for on-premises sources).
- Prepare the source: enable binary logging with GTIDs (MySQL) or logical decoding with
pglogical(PostgreSQL), and create a replication user. - Create the migration job (continuous); wait for the full load, then for replication lag near zero.
- Validate: row counts and checksums, run the application's test suite against the target, compare query performance.
- 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.
- 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-south1Follow-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.