Zero-Downtime Database Migrations: On-Prem to AWS with DMS and pglogical
A cutover-first guide to migrating production PostgreSQL and MySQL to AWS with AWS DMS and pglogical: replication, validation, sequences, rollback and runbook.
Across several on-premises-to-AWS moves of PostgreSQL and MySQL workloads, including more than 4 TB of production data, the migrations that went well shared one trait: the team designed the cutover and rollback first and the data copy second. Here is the approach, with the details that usually bite.
1. Pick the replication path
- AWS DMS (full load + CDC) works across engines and into RDS/Aurora, with built-in validation. Great for heterogeneous moves or when you cannot enable logical replication on the source.
- pglogical / native logical replication suits PostgreSQL to PostgreSQL. You get tighter control and fewer type-mapping surprises, but need
wal_level = logicalon the source and a primary key (or replica identity) on every replicated table.
Whichever you choose, migrate the schema separately and first using the engine's native tooling, then let the replication tool move data only.
2. Prepare the source
# postgresql.conf (source) for logical replication
wal_level = logical
max_replication_slots = 10
max_wal_senders = 10
shared_preload_libraries = 'pglogical'
Check disk headroom for WAL. A stalled subscriber makes the source retain WAL indefinitely, so alert on replication slot lag from day one. On RDS targets, logical replication is enabled through the parameter group (for example rds.logical_replication).
3. Initial load, then catch up
Start CDC so changes during the bulk copy are queued, run the full load, then let replication drain until lag is near zero and stays there under normal traffic. Tune LOB handling and CDC batch sizes in DMS for tables with large objects, because defaults can throttle throughput badly. Consolidating redundant replication tasks also reduces cost and noise.
4. Validate continuously, not once
- Row counts per table, then checksums on critical tables or sampled ranges.
- DMS data validation or an equivalent comparison job running during replication.
- Application smoke tests pointed at the target in read-only mode before any switch.
- Compare query plans for your top queries. A different version or missing statistics can change performance.
5. The gotcha: sequences and other non-replicated state
Logical replication moves rows, not sequence positions. Forget this and your first insert after cutover fails with a duplicate key. During the cutover window, advance every sequence with a safety margin:
SELECT setval('orders_id_seq', (SELECT max(id) FROM orders) + 1000);
Also check roles and grants, extensions, scheduled jobs, large objects, and anything stored outside tables that the tool does not carry over.
6. A cutover runbook that fits in an hour
- Lower DNS TTLs a day ahead.
- Announce the window; enable read-only mode or pause writers at the application.
- Wait for replication lag to hit zero; confirm with a final row-count check.
- Sync sequences on the target.
- Switch connection strings or DNS to the target.
- Run smoke tests and watch error rates, latency and replication health.
- Keep the source intact and read-only for an agreed soak period.
7. Decide rollback before you start
A rollback plan that only exists in someone's head is not a plan. The strongest option is reverse replication from the new primary back to the source during the soak window, so you can switch back without losing writes. If that is not feasible, define the exact point after which rollback means data loss, and get that accepted by stakeholders in advance.
Takeaways
- Design cutover and rollback first.
- Schema first, data second, validation throughout.
- Sequences are the classic day-one failure.
- Rehearse in staging with production-sized data and time the cutover.
Planning a migration and want to compare notes? Get in touch.
Enjoyed this?
I write about PostgreSQL, AWS and database reliability. Have a question or a similar problem to solve?