Book a 30-min callBook a call

Change data capture (CDC): streaming database changes with the write-ahead log

How change data capture works: polling vs triggers vs the log, PostgreSQL slots and publications, MySQL binlog, snapshots, ordering, deletes and idempotency.

A database cylinder writing into a long log strip, a reader box with a bookmark on the strip, and arrows fanning out to three boxes for a search index, a cache and a warehouse.
Written by
BYD Tech
Published
Topic
Deep dives
Reading time
14 min read

Questions people ask

Is CDC the same as event sourcing?

No. In event sourcing, the stored events are the source of truth and the tables are derived from them. In CDC, the tables are the source of truth and the events are derived from their changes, at the level of rows and columns. CDC is often used to publish an outbox table, which is how an application gets business-level events out of a regular database.

Why not just poll by updated_at?

Polling misses hard deletes, collapses several updates between polls into one, and can skip rows whose transaction commits after the poller has moved its cursor past their timestamp. It is fine for a simple sync that tolerates those gaps. When every change matters, read the log.

Does log-based CDC slow down the database?

Usually only a little on the write path, since the log is written anyway; wal_level = logical and REPLICA IDENTITY FULL add some WAL volume. The larger risk is a stopped consumer: its replication slot keeps WAL on disk until it catches up, so cap it with max_slot_wal_keep_size and alert on retained WAL.

What happens if the CDC consumer is down for a long time?

In PostgreSQL, the slot keeps WAL until the consumer returns or the slot hits max_slot_wal_keep_size and is marked lost, after which the consumer needs a new snapshot. In MySQL, binlog files are removed after binlog_expire_logs_seconds, and a connector that falls behind that window also has to re-snapshot.

Can CDC capture schema changes?

Not through PostgreSQL logical decoding, which does not carry DDL. Plan schema changes as compatible steps: add optional columns, update consumers first, and remove old columns only after nothing reads them.

Building something like this?