Operating Log-Based CDC

Concepts covered: Change Data Capture

A log-based pipeline on Postgres needs ongoing care in 3 places: the first copy of rows that existed before it started, the replication slot that holds its place in the log, and table definitions that change while it runs.

Change records move left to right. The slot keeps WAL on the source until the connector confirms each position.
  • orders (source, Postgres)
  • replication slot (queue, WAL, logical slot); alerting: retained WAL > 20 GB
  • CDC connector (transform, log-based reader); backoff: exponential, 1 s to 5 min
  • change stream (queue, change topic)
  • schema check (quality gate, schema registry); on error: incompatible change to DLQ
  • orders replica (storage, warehouse); idempotency: upsert by order_id, highest LSN wins
  • revenue dashboard (consumer, finance)
  • orders -> replication slot
  • replication slot -> CDC connector
  • CDC connector -> change stream
  • change stream -> schema check
  • schema check -> orders replica
  • orders replica -> revenue dashboard

The initial snapshot

The WAL keeps recent history, usually hours to days, and never the whole life of a table. A connector started today on orders would see today's changes and none of the 40 million rows already there. So every log-based pipeline begins with a snapshot: a full copy of the table, stitched to a position in the log.

Postgres makes that stitch exact. Creating a logical replication slot fixes its starting position, an LSN (log sequence number), and can export a transaction snapshot that sees the database as of that LSN. The connector copies the table through that snapshot, then streams from the slot. Everything committed before the LSN arrives in the copy, and everything after it arrives through the log.

The catch is duration. Copying 40 million rows in a single transaction can take hours, and a transaction open that long stops Postgres from cleaning up dead rows on the source. Chunked snapshots, the approach in the Netflix framework from the intro, copy primary-key ranges and reconcile each chunk with the live stream, so no transaction stays open for long. Either way, a connector that restarts mid-snapshot copies some rows twice, so the sink upserts by key.

Replication slot lag

A replication slot remembers the last LSN the connector confirmed, and Postgres keeps every WAL file after that point, because the connector may still need it. While the connector keeps up, retained WAL stays small. When it stops (a crashed worker, an expired credential, a deploy paused halfway), WAL accumulates on the source's own disk.

The slot retains WAL for the whole database cluster, every table, even when the pipeline reads only orders. Suppose the cluster writes 6 GB of WAL an hour and its disk has 150 GB free. A connector that dies on Friday evening fills that disk by Saturday night, and a Postgres primary that cannot write WAL shuts down, taking the application with it.

SELECT
  slot_name,
  active,
  pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) AS retained_bytes
FROM pg_replication_slots
WHERE slot_type = 'logical'

The cap trades a large incident for a smaller one. An invalidated slot cannot stream again, and the changes between its last LSN and now are gone from the log, so the pipeline takes a new snapshot. That costs hours of copying; a full disk costs the application its database.

When the table's definition changes

Postgres logical decoding carries row changes and no DDL. After someone runs ALTER TABLE orders ADD COLUMN currency text, the connector finds out from the next change to orders, which arrives with a relation message describing the new column list. Records from then on carry the extra field, and records already in the stream keep the old shape.

Change on the sourceWhat the stream showsWho breaks
Add a nullable columnA new field from the next change onNobody, if readers ignore unknown fields
Rename a columnOld field gone, new field presentEvery reader of the old name
Drop a columnField missing from new recordsReaders that require it
Widen `integer` to `bigint`Same field, larger values possibleSinks typed to the narrow type

Treat the change stream like any other event stream. Register its schema and let backward-compatible changes through. Renames and type changes go through expand-contract on the source. A change the registry rejects goes to the dead letter queue instead of corrupting the replica.

The orders connector crashed 9 hours ago. Retained WAL is 54 GB, the slot has no max_slot_wal_keep_size cap, and the disk has 96 GB free. The connector fix is ready. What should happen first?

About this lesson

This section is part of the Ingestion Patterns: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.