In brief
Change Data Capture (CDC) reads changes in a database, meaning inserts, updates, and deletes, and passes them on to other systems. The best variant reads the database transaction log rather than the tables themselves, so it adds no queries to the production system and does not miss deletes. This is how you can feed a data warehouse, a search engine, or an event platform without changing the code of a legacy application. The typical open source tool is Debezium running on Kafka Connect.
How log-based CDC works
Every relational database writes changes to the transaction log first, so that it can restore its state after a failure. CDC uses that same log: a reader process (the connector) reads successive entries, turns them into “row inserted”, “row updated”, and “row deleted” events, and publishes them onward, usually to Kafka topics.
Each database exposes its log differently:
- MySQL and MariaDB: the binlog in row format;
- PostgreSQL: logical decoding of the WAL through a replication slot;
- SQL Server: a built-in CDC mechanism with change tables;
- Oracle: reading redo logs, for example through LogMiner;
- Db2 and others: the vendor's own mechanisms or replication tools.
A CDC event usually contains the row state before and after the change, the table name, the time, and the position in the log. Thanks to the position, the connector knows where to resume reading after a restart.
CDC compared with other ways of extracting changes
Before log-based CDC appeared, changes were extracted with queries or triggers. Both methods work, but they have limitations that show up under heavy traffic.
| Method | How it works | Database load | Deletes | Latency |
|---|---|---|---|---|
| Query by modification date | every few minutes a SELECT of records with a newer modification date | grows with table size and polling frequency | not visible | minutes |
| Triggers | a trigger writes every change to an auxiliary table | an extra write on every transaction | visible | seconds |
| Log-based CDC | a connector reads the transaction log | small, no queries against tables | visible | usually seconds |
Querying by modification date has one more weakness: it depends on every application and every script setting the modification date column correctly. A single manual fix in the database that skips this column is enough for a change never to reach the data warehouse.
Debezium and Kafka Connect in practice
Debezium is a set of open source connectors for Kafka Connect, the Apache Kafka framework for moving data between Kafka and other systems. A Debezium source connector reads the database log and publishes changes to Kafka topics, usually one topic per table. On the other side, sink connectors write them to a data warehouse, a search engine, or another database.
A typical deployment goes through three stages:
- Preparing the database. Enabling the right log format, creating a user with permission to read the log, and in PostgreSQL creating a publication and a replication slot.
- Initial snapshot. The connector first reads the current state of the selected tables, then switches to reading the log from the point where the snapshot began. For large tables the snapshot is planned outside peak hours.
- Change stream. From then on, every transaction reaches Kafka with a latency usually measured in seconds.
We write more about designing topics, partitions, and retention for such streams in the article Apache Kafka in the enterprise.
The outbox pattern: business events instead of raw rows
CDC events describe changes in tables, not business facts. A consumer that receives “row changed in table PLATNOSCI_POZ” has to know the internal model of the sender's database. When the sender changes its table structure, consumers stop working.
The transactional outbox pattern solves this problem. In the same transaction in which it saves data, the application inserts a business event into an outbox table, for example “payment posted”, in an agreed format. CDC reads only that table and publishes the events to the right topics. Debezium has a ready-made transformation for this (the outbox event router). The gain is twofold: an event exists if and only if the transaction succeeded, and consumers get a stable contract instead of a table structure.
Outbox requires a change in the application, so for legacy systems that cannot be changed, what remains is CDC from tables and a layer that turns changes into events. This is often the first stage of replacing an integration bus, described in the article ESB or event-driven architecture and in the scenario replacing an ESB with a Kafka event platform.
What to watch for during deployment
CDC is light on the database, but it is not maintenance-free. The most common problems:
- A stopped connector in PostgreSQL. The replication slot holds WAL files until the connector reads them. A connector that stays down for several days can fill the database server's disk. You need an alert on the size of the accumulated WAL.
- Log retention in the database. If the log is deleted faster than the connector reads it, for example after a longer outage, a new snapshot is needed.
- Schema changes. Adding a column usually goes smoothly, but changing a type or a name breaks consumers. Schema changes to tables covered by CDC must be agreed like API changes.
- Personal data. CDC copies everything in the table, including data the consumer does not need. Columns can be excluded or masked already in the connector.
- Ordering and duplicates. After a restart some events may arrive again, so consumers must process them idempotently.
CDC also works well for migrating data from one system to another without a long outage: the new system receives a snapshot, then the ongoing changes, until the moment of cutover. This approach is developed further in the article Migrating customer data without service interruption.
Questions and answers
Does CDC slow down the production database?
Log-based CDC adds little work, because it does not query the tables but reads a log that the database writes anyway. There is some cost in enabling a fuller log format and in the initial snapshot, which for large tables is planned outside peak hours. The biggest risk is not performance but a disk filling up with logs while a connector is stopped.
Will CDC replace API-based integration?
Not in every case. CDC is good at distributing data changes to many consumers and feeding data warehouses. It is not suited to synchronous queries, where the caller waits for a response, or to operations that are meant to trigger a process in another system with validation. For such cases an API remains the answer, described in the article REST, GraphQL, or gRPC.
Can you use CDC for IBM i systems?
Yes, but with different tools than for the popular open source databases. IBM i records changes in journals, which replication tools use, both the vendor's and third parties'. The choice depends on the system version, the volume, and where the changes are going. The specifics of this database are described in the article Db2 for i.
How fast do changes reach consumers?
With a correctly working connector, latency is usually seconds or fractions of a second from the transaction commit. It grows with large batch transactions, an overloaded Kafka cluster, or a slow consumer. That is why connector and consumer lag must be monitored the same way as application response time.