Core Concepts

Change Data Capture

Copy the state of a live database, then follow its committed changes without losing a write between the two steps.

Free~17 min

Change data capture (CDC) turns committed database changes into a feed that other systems can read. During a live migration, the new database needs a copy of the existing rows and the changes made while that copy was running. The part to get right is where the copy ends and the change feed begins.

Pair the snapshot and the stream start

Store 42's inventory is moving to another database shard while the old shard continues to take orders. We start with a snapshot, a consistent view of the rows at one point in the database's history. The source gives us that snapshot and a matching stream position, a place in its ordered feed of changes. In this example P100 marks the point they share.

At P100, item A12 has quantity 5 and item B20 has quantity 2. While we copy those rows, the old shard commits two more changes. P101 sets A12 to 4. P102 deletes B20. The stream supplies both commits even if a batch of snapshot rows is still on its way.

Taking a copy and finding the log position in a separate step can leave a gap. For example, if we take the snapshot, then record P101 as the place to begin reading after, the copy contains quantity 5 and the feed skips the change to 4. Ask the source or connector for its consistent pair.

PostgreSQL logical decoding turns information in the write-ahead log into a change feed. When creating a logical slot through the replication protocol, PostgreSQL can return a consistent stream start and the name of an exported snapshot. Import that snapshot for the copy before the exporting connection runs another command or closes. The replication protocol defines the exact start at the returned consistent point. PostgreSQL exported snapshots · PostgreSQL replication protocol

Apply late rows and deletes safely

The simplest target has two stages. Load the entire snapshot, then apply the later commits in order. The source or a downstream buffer needs to retain the unapplied changes while the copy runs.

We can also copy and apply changes in parallel. In this migration the target labels snapshot rows with P100 and changes with their later source positions. The target compares each key's current version and writes the value and version in one atomic operation. A conditional update or a transaction that locks that key ensures the older copy cannot slip in after a newer change. If A12 has already reached 4 at P101, its old copy at P100 leaves it unchanged.

If several changes to one key share a commit position, apply them in source order and record the key's final value and commit position together. To version each change separately, include its position within the transaction in the version.

A delete needs the same comparison. When P102 removes B20, save a tombstone, a versioned deletion marker for that key. A late snapshot row for B20 arrives with P100, sees the marker at P102, and is skipped. Keep the marker until earlier data can no longer arrive or be replayed into this target.

A change needs enough information to identify its affected row. In PostgreSQL logical replication, a published table generally uses its primary key as the replica identity for updates and deletes. Check the table and connector configuration before relying on an old row's full contents. Some connectors also use the word tombstone for a broker record. Debezium's PostgreSQL connector can emit a key with a null value after a delete for Kafka log compaction. This migration keeps the deletion version at its target so it can compare later arrivals. PostgreSQL replica identity · Debezium delete events and tombstones

Resume at a durable checkpoint

A checkpoint here is the target's saved position through which changes have been applied. Suppose we apply P102 and then crash before telling the source we are through P102. The source can send the same delete again. Our target can tell that the change is already applied if it saved the effects of the source commit and its applied position in one transaction.

If the target transaction had rolled back, its position would still be P101 and replay would apply P102. Confirm progress to the source once the changes are safely stored for recovery. If a connector confirms after writing to a durable intermediate log, the target keeps its own applied checkpoint for that log and the intermediate log must support replay.

Advance only through the prefix that is safely stored. A worker that sees P104 while P103 is still failing cannot mark the stream complete through P104. Track progress separately per source shard. Where the target must preserve a multi-row invariant, apply the source transaction's changes together before advancing through it.

Run the feed without exhausting the source

A PostgreSQL logical replication slot tracks progress for a logical change reader and protects log data that the reader may still need. PostgreSQL marks log locations with log sequence numbers (LSNs). In pg_replication_slots, restart_lsn is the oldest WAL location the slot may still need. confirmed_flush_lsn is how far the reader has reported that it received the data. The target's applied checkpoint usually lives separately.

After a database crash, a logical slot can restart from an earlier persisted position and return recent changes again. A stopped reader can make the source hold old WAL, and a configured max_slot_wal_keep_size can let required WAL become unavailable to an excessively lagging slot. Watch WAL retained per slot, disk space, and how much of the source stream the target has actually applied. PostgreSQL logical slots · Slot progress fields · PostgreSQL WAL retention settings

A standby can have the table data and still lack a usable logical slot for the connector. PostgreSQL 17 introduced synchronization for configured failover slots. It is asynchronous, so check before promotion whether the candidate has the required slot and can still supply the needed interval. If no verified history covers a missing interval, rebuild from a fresh paired snapshot and stream start. PostgreSQL 17 release notes · PostgreSQL logical replication failover

Hand ownership to the target

The old shard owns writes while the target catches up. Verify the copy while it runs. When ready to switch, block new writes to store 42 on the old shard and let any admitted writes finish or abort. Record the final committed stream position. Wait for the snapshot and for the target to apply through that position. Give the target write authority and update routing while keeping the old shard closed to writes. The change feed is how the two copies catch up. The cutover decides who can accept the next write. Vitess resharding and write switch

In an interview, draw the snapshot and stream boundary, then walk one write during the copy, one late delete, and one restart. State how you tell that the copy is current enough to hand over.

See also

Used in

  • Database Sharding moves store 42 with a backfill, a commit stream, versioned deletes, and a final catch-up before switching ownership.

Done reading? Mark it so it sticks in your dashboard.

Discussion