CDC: The Three Families
Concepts covered: Change Data Capture
Change data capture (CDC) turns each write to a source table, deletes included, into a change record the pipeline can apply downstream. The bounded pull from the previous section is already a crude form of it. The 3 families of CDC differ in where they find the changes: in a side table the database fills, in the table itself, or in the database's own log.
Trigger-based: the database copies each change
A trigger is a function the database runs inside the same transaction as a write. A CDC trigger on orders inserts a row into orders_changes for every write to it, deletes included. Each of those rows records which operation ran and when, along with the key and the new values. Ingestion reads orders_changes with a bounded window and prunes what it has loaded. Deletes are captured, and so is every intermediate value.
The source pays for it. Every write to orders now performs a second write, which adds write latency and storage. Every column change to orders also needs a matching change to the trigger and the side table. Trigger-based CDC fits databases that expose no change log, or tables with modest write rates where nobody can get log access.
Query-based: poll the table for changes
Query-based CDC is the bounded incremental pull run on a timer of 10 minutes, or as little as 1 minute. It needs only a read-only login and an updated_at column. It sees each row as it stands at poll time, so 2 updates between polls arrive as a single change, and a hard-deleted row never arrives at all. Every poll is also a query on the source, so a shorter interval means more load.
Log-based: read the replication log
Every relational database records each change in a log before applying it to the table, so it can recover after a crash and feed replicas. Postgres calls this the write-ahead log (WAL); other engines call theirs a binlog or redo log. A log-based connector reads that log the way a replica would and emits a record per change, in commit order, deletes included. The source does no extra writes, and changes arrive seconds after commit. The cost moves to operations, the subject of the next section.
The animation runs 4 changes to orders between 10:00 and 10:11: order 1001's total rises to 13000 at 10:03, order 1003 is inserted at 10:05, order 1002 is deleted at 10:06, and 1001 rises again to 13500 at 10:08. Play both variants and compare the replica at the end.
- orders (source, Postgres)
- wal (queue, write-ahead log)
- poller (transform, query-based)
- replica (storage, warehouse)
- orders -> wal
- orders -> poller
- poller -> replica
- Query-based: Result: replica still has 1002, which orders deleted. Nothing in the poll could see the delete
- Log-based: Result: replica matches orders row for row, the delete included
The query-based poller fires at 10:10 with its bookmark at 10:00. It copies 1001 at 13500 and the new 1003, and never learns that 1001 passed through 13000 or that 1002 was deleted, so the replica still shows order 1002 at 8000. The log-based connector applies each change around 2 seconds after it is written, and the replica ends identical to the source.
| Family | Hard deletes | Intermediate states | Extra source writes | Access needed | Latency |
|---|---|---|---|---|---|
| Trigger-based | Captured | Captured | A second write per change | Create triggers and tables | Poll interval of the side table |
| Query-based | Missed | Collapsed to the latest | None | Read-only login | Poll interval |
| Log-based | Captured | Captured | None | Replication privilege, log config | Seconds |
Choosing a family
When deletes or every intermediate state matter and you can get replication access, log-based wins. When the source is a vendor-hosted database that grants a read-only login, query-based is the only family on offer, and you ask the owners for soft deletes, a deleted_at column, so a delete becomes an update the poll can see. Trigger-based is the fallback for when deletes matter but log access is impossible, provided the write rate can absorb a second write.
A fraud model needs every status a payment passes through (authorized, captured, refunded), and some payments move through all 3 within a minute. Hard deletes never happen. The source is a self-hosted Postgres your team administers. Which family fits?
- Query-based every minute, since rows are never deleted and a read-only login is enough
- Trigger-based, writing each status to a side table inside the payment's own transaction
- Log-based, reading each status change from the WAL through a replication slot
- A full load every 5 minutes, capturing each payment's final status without a bookmark
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.