Bookmarks

CDC — change data capture

An approach to data integration in which every change — an insert, an update or a delete — is carried from one system to another almost immediately after the transaction commits.

Also: change data capture, log-based CDC, query-based CDC, transaction log reading

checked 9 Nov

All terms

A classic scheduled export compares table snapshots or updates by a “last modified” timestamp: such queries load the database and still miss deletes. CDC takes a different route — it picks changes from the transaction log or from triggers and delivers them to subscribers as a stream of events, so the sink data lags behind the source by seconds, not until the next overnight run.

How it works

The primary method is reading the transaction log: the WAL in PostgreSQL, the redo log in Oracle, the binlog in MySQL. The database writes every change there anyway — a CDC process reads this log and turns low-level entries into events. In PostgreSQL the mechanism is called logical decoding: the contents of the WAL are transformed into a stream of changes that applications can understand.

MethodHow it worksCost
Transaction loga separate process reads the WAL, redo log or binlogbarely loads the database, but requires privileges and server configuration
Triggersa trigger on each table writes the change into a log tableslows down the very transactions that modify the data
Snapshot comparisonqueries by timestamp or by checksumsmisses deletes and same-frame overwrites, loads the database

An event assembled from the log usually carries the minimum about what happened:

jsonUpdate event for a row: operation type, table and before/after values
            {              "op": "u",              "source": {"table": "orders", "lsn": 3412054112},              "before": {"id": 42, "status": "NEW", "total": 1990},              "after": {"id": 42, "status": "PAID", "total": 1990}            }            

Debezium and the typical pipeline

The open-source CDC tool is Debezium, a change data capture platform. Its connector attaches to the database, takes a consistent snapshot of the tables on the first run, then resumes the change stream from the snapshot point and produces an event for every insert, update and delete at the row level. The changes of each table are published to a separate Kafka topic, from which consumers read them.

The connector remembers the log position for each event and after a failure resumes from the same spot — events are not lost. The price of this reliability: on PostgreSQL the changes are consumed through a replication slot, which holds WAL segments until the consumer has read them.

Where it is used

  • Synchronising caches and search indexes: Elasticsearch receives row changes almost immediately after the commit.
  • Cache invalidation: an event about a row change tells all instances that it is time to re-read the record from the database.
  • Audit: the change log answers who changed the data, when and how.
  • Gradual migration between systems: changes keep flowing while the old database is still running.

Other meanings of CDC

The same abbreviation also appears outside IT — for example, the Centers for Disease Control, the US public health agency — but in IT, CDC usually means change data capture.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • ETLETL: extract, transform, load

    Scheduled ETL exports data in batches and often recomputes it in full; CDC delivers the changes themselves, almost in real time.

  • replication

    Replication reproduces changes on copies of the same database in its native format; CDC turns changes into events for other systems — brokers, caches, data stores.

  • transactional outbox pattern

    In the outbox pattern the service itself writes the event inside its own transaction; with CDC the events are collected by an external process reading the transaction log.

In depth in the references

where the topic is covered in detail