Change data capture (CDC)
Change data capture (CDC) reads a database’s own record of changes, its transaction log, and publishes each committed insert, update and delete as an event. Other systems subscribe to those events to keep a search index, cache, data warehouse or another service’s data in step with the source, usually within seconds.
It’s the most reliable way to get changes out of a database you can’t or don’t want to modify, and the usual first step in moving from nightly batch extracts to near-real-time data. See also change data capture in the glossary.
Diagram
Section titled “Diagram”- The application writes to its database as usual; nothing in it changes.
- The connector reads committed changes from the database's transaction log.
- Each change is published as an event, in commit order per row or table.
- Consumers such as the warehouse, search index and caches apply the changes independently.
How it works
Section titled “How it works”There are three common ways to detect changes; only one is usually worth building on.
| Approach | How | Problems |
|---|---|---|
| Polling queries | Repeatedly select rows with updated_at newer than last time |
Misses deletes, misses rows whose timestamp isn’t updated, loads the database |
| Triggers | Database triggers write changes to a side table | Adds work to every transaction; triggers are fragile to maintain |
| Log-based | Read the database’s transaction log (for example the PostgreSQL write-ahead log or MySQL binlog) | Needs log access and retention; schema changes need care |
Log-based CDC captures every committed change, including deletes, in commit order, with almost no extra load on the source. Tools such as Debezium (opens in a new tab) and most cloud data services’ replication features work this way.
A typical change event carries the operation (create, update, delete), the row’s values before and after the change, the source table, and the log position and transaction it came from.
Initial snapshot. A new consumer needs the existing data as well as future changes. CDC tools take a consistent snapshot of the tables, then continue from the log position where the snapshot was taken, so nothing is missed or doubled.
- No changes to the source application.
- Captures every change, including deletes, in order.
- Low latency, typically seconds.
- Low load on the source compared with polling or triggers.
- Events describe table rows, not business events. “Row in
order_linesupdated” isn’t “order shipped”. Consumers become coupled to the source’s internal schema. - Schema changes in the source can break consumers without warning.
- Needs access to the database log, which managed databases and DBAs may restrict, and log retention long enough to survive connector downtime.
- Transactions spanning several tables arrive as several events, which consumers may need to regroup.
When to use it
Section titled “When to use it”- Feeding a warehouse, lakehouse or search index from operational databases in near real time.
- Migrating data from an old system to a new one while the old one is still live (see the strangler fig pattern).
- Keeping caches and read models in step with their source.
- Publishing changes from a legacy system you can’t modify.
When to avoid it
Section titled “When to avoid it”- When other teams need stable business events. Publish those deliberately from the owning service, for example with a transactional outbox (which is itself often relayed by CDC).
- When a nightly batch is genuinely good enough and simpler.
Common pitfalls
Section titled “Common pitfalls”- Exposing raw table changes as a public contract, so the source team can never refactor its schema. Put a translation layer or an outbox in between.
- No plan for schema changes. Use a schema registry and agree how column changes are rolled out.
- Losing the log position after a long outage, when the database has already discarded the log. Monitor connector lag and size log retention for your worst outage.
- Assuming exactly-once delivery. Connectors can redeliver after a restart; consumers must be idempotent.
Related patterns
Section titled “Related patterns”- Message queue vs event stream
- Lambda and Kappa architectures
- Event-driven architecture
- Data migration and cutover checklist
Further reading
Section titled “Further reading”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.