
Migrating a Production Postgres Database With CDC and Dual Writes
Cutting over a production Postgres cluster before reconciliation finishes is the mistake that pulls a replatforming project under, and unplanned outages during that window run roughly $14,056 per minute. The pattern that avoids it pairs change data capture with dual writes, in a specific order.
Logical replication goes up first. A snapshot backfills history, the application begins writing to both clusters, reconciliation runs until divergence is zero, then DNS flips. A rollback path stays open until the new primary has carried a full business cycle.
That order is the whole trick. CDC without dual writes leaves a widening replication lag during cutover; dual writes without CDC leave no safety net if one side drops a transaction. You need both.
The Pattern in One Paragraph
Call the current primary source and the target destination: a new major version, a cross-cloud replica, or a cluster with the schema you actually wanted. A replication slot on source streams the write-ahead log to a CDC consumer, which applies rows to destination. Once initial lag closes, the application starts writing to both clusters in the same transaction scope, with source as the authority. A reconciliation job compares row counts and checksums until they match. Read traffic shifts to destination. Writes shift. Dual writes stay on for one more business cycle as a reversible safety net, then come down.
The failure modes to design against are the usual ones: a dropped WAL segment, a schema change that replication silently ignored, and a dual-write that committed on one side and crashed before committing on the other.
Logical Replication Is the Prerequisite, Not the Plan
Postgres can stream row-level changes over logical replication, but the server has to be configured for it. wal_level must be set to logical, which is not the default and requires a restart. Plan the restart into the migration timeline before anything else, because on a busy OLTP system it is the one unavoidable blip the pattern cannot eliminate.
The documentation is explicit about what logical replication does not carry: DDL changes and incremental sequence updates are not replicated. That means schema migrations run on both clusters, in the same order, with the application tolerating either shape during the overlap. Sequences need a separate sync step at cutover or writes on destination collide with ids the source already used.
Debezium Does the Capture, Kafka Does the Durable Queue
For anything beyond a single-destination flip, the Debezium Postgres connector is the default tool. It takes a consistent initial snapshot of every selected schema, then continuously streams INSERT, UPDATE, and DELETE events into Kafka topics keyed by primary key. The snapshot and the streaming phase are a single logical operation from the consumer's point of view, which removes an entire class of ordering bugs.
Kafka earns its place here because it is the durable buffer that lets destination fall behind without losing events. If destination needs a replay, the consumer rewinds its offset. If destination is rebuilt from scratch mid-migration, the snapshot runs once more and the stream picks up where it was. AWS DMS covers the same pattern with a managed runtime and replication slots as the source, at the cost of less control over transformations.

Dual Writes Fix What CDC Alone Cannot
A pure CDC cutover has one weak moment: the instant the application starts writing to destination, source stops being authoritative, and any event still in flight from source has to drain before the switch is safe. Most teams underestimate how long that drain is under real load. Dual writes close that drain window by making the application the second source of truth during the overlap.
The hazard in dual writes is well documented. The dual-write problem is what happens when an application commits to two systems without a shared transaction boundary and one of them fails: the systems diverge and no one notices until a reconciliation job runs. Martin Kleppmann's argument that the log is the single source of truth is the right mental model here. Treat source's WAL as the authority, treat the application's write to destination as a hint that reconciliation will verify, and never let a user-facing path block on the destination write succeeding.
Backfill, Then Reconcile Row by Row
Debezium's initial snapshot handles the backfill for most tables. For very large tables, a parallel export-and-import outside the snapshot path is faster: pg_dump a consistent snapshot by LSN, restore to destination, then start the CDC stream from that same LSN. The Stripe engineering team's writeup of their zero-downtime data migrations describes the same backfill-plus-tail pattern against document stores, and the reasoning carries over to relational systems.
Reconciliation then runs on a schedule until the migration window closes:
- Row counts per table, compared on a rolling window of the last N minutes and on the full table.
- Checksums over ordered primary-key ranges, so a diff narrows to a specific slice rather than a whole table.
- Business-key probes on the ten or so tables that drive revenue, run every minute, with a page on any delta.
Divergence on the hot tables is the signal that cutover is not safe yet. Teams that skip this step find out at 3 a.m. that a trigger on source was not recreated on destination, or that a sequence drifted by 400 and every new order is colliding on its primary key. Our walkthrough of monolith-to-microservices migration without downtime covers the same reconciliation discipline for service-level splits.
Cutover and the Rollback That Pays for Itself
The cutover itself is short. Pause writes at the application layer for a few seconds, drain in-flight CDC events, sync sequences, verify a final checksum on the hot tables, flip the write path to destination, resume. Reads can shift earlier, incrementally, behind a feature flag per endpoint.
The rollback path is where the pattern earns its money. Keep source receiving writes from destination via CDC in reverse for one full business cycle after cutover. PostgreSQL 16's bidirectional logical replication, released in September 2023, made this cleaner by preventing the loops that blocked earlier active-active attempts. If destination develops a problem, flipping writes back to source is a configuration change, not a restore from backup. The option itself is usually never exercised, which is exactly what you want from an insurance policy.
The pattern is not free. Dual writes add latency to every mutation during the overlap, CDC adds operational surface area, and reconciliation is a new job to monitor. Teams weighing the tradeoff against a simpler maintenance-window cut should run the numbers in our breakdown of legacy modernization cost. For most production systems carrying real transaction volume, the math lands on the CDC-plus-dual-writes side, and the API layer is where the dual-write logic lives, not the database.
What a Good Answer Sounds Like
Ask any vendor pitching a Postgres migration one question: show the reconciliation report from the last cutover they ran, and the rollback rehearsal that went with it. A good answer is a dashboard or a runbook with specific row counts, checksum diffs, and a documented LSN at which the rollback path was tested under load. A bad answer is a slide about CDC and a promise that nothing will go wrong. The difference between those two answers is the difference between a 90-second write pause and a weekend-long incident, and it is visible before any contract is signed.
