Databases / Change data capture

Change data capture

Three changes hit a products table. Follow them from the database’s own log, through a connector and a stream, into a cache, a search index and a warehouse.

Intermediate7 steps~6 min
App — writes to the database as usual
Database — keeps the tables and a log of every change
CDC connector — reads the log and emits change events
Consumer — cache, search index or warehouse
// THE SHORT VERSION

In 30 seconds

A database is rarely the only place its data lives: caches, search indexes and warehouses hold copies. Change data capture (CDC) keeps them up to date.

It works by reading the database’s transaction log, where every insert, update and delete is already recorded in order. A connector turns each entry into an event and publishes it to a stream.

Other systems subscribe: one deletes stale cache entries, one keeps a search index in sync, one feeds analytics. The application writing to the database does not have to know about any of them.

Change data capture, step by step

STEPS01 / 07

Simplified: real positions are LSNs (PostgreSQL) or binlog offsets (MySQL), how much of the old row is logged depends on settings, and connectors usually copy existing rows once before following the log.

STEP 01 / 07 Copies that go stale
THE MATH
INSERT id 7 (pen, 10) UPDATE id 7: 10 → 12 DELETE id 3 (mug)
  1. Step 1: Copies that go stale. The app makes three changes to the products table: it adds a pen, raises its price from 10 to 12, and deletes the mug. A cache, a search index and a data warehouse each hold a copy of this data, and none of them has been told.
  2. Step 2: The log sees every write. Databases already keep a transaction log: every insert, update and delete is appended to it in commit order, at a numbered position. The database uses it to recover from crashes and to feed its replicas.
  3. Step 3: Read the log, not the table. Change data capture means reading that log instead of asking the tables. A connector follows the log the way a replica does and sees all three changes in order. Polling the table would find only the latest state: it would never see the price of 10 and could not tell that the mug was deleted.
  4. Step 4: Each entry becomes an event. The connector turns each log entry into a change event with one fixed shape: the kind of operation, the row before and the row after. An insert has no before and a delete has no after, so a consumer can handle all three with the same code.
  5. Step 5: Publish to a stream. The events are published, in log order, to a stream such as a Kafka topic. The database does not need to know who is listening. Each consumer reads at its own speed and remembers how far it has got, so a slow warehouse does not hold up the cache.
  6. Step 6: Three uses of one stream. Each consumer applies the same events in its own way. The cache deletes the entries that are now wrong. The search index mirrors each change so it stays in sync. The warehouse appends all three events, keeping the history that the table itself forgot.
  7. Step 7: Keep your place in the log. The connector regularly saves the log position it has delivered. If it crashes while sending 103, it restarts after 102 and sends 103 again. Nothing is skipped, but an event may arrive twice, so consumers are written so that a repeat does no harm.
VALUES SO FAR
writes3 changesinput
pos 101····log
pos 102····log
pos 103····log
event····shape
stream····ordered
readers····uses
saved····bookmark
delivery····rule

Spotted a mistake on this page?