Read every change made in a database and send it on to other systems.
Change Data Capture (CDC) means watching a database and turning every insert, update and delete into an event that other systems can read.
Most systems keep the same data in more than one place. The orders live in a database, but the website also needs them in a search index, the analysts need them in a warehouse, and a cache must not show old prices. The question is: how do these copies find out that something changed?
One way is to ask the database again and again: "What is new since last time?" (polling). Another is to change the application so it also writes to every other system (a dual write). CDC offers a third way: let the database tell you, by reading its own record of changes.
Databases already keep such a record. PostgreSQL has the write-ahead log (WAL), MySQL has the binlog. Before a change is made to a table, it is first written to this log, so the database can recover after a crash. A CDC tool such as Debezium reads this log like a reader following a diary, and publishes one event per change, often to a message system such as Kafka.
| Method | How it works | Good | Bad |
|---|---|---|---|
| Polling (query-based) | A job runs a query such as WHERE updated_at > last run | Simple, works on any database | Misses deletes, merges changes between polls, adds load, needs a reliable timestamp column |
| Triggers | A database trigger copies each change into an audit table | Sees all changes, including deletes | Every write does extra work in the same transaction, so writes slow down |
| Log-based | Read the WAL or binlog and turn entries into events | Complete, ordered, very light on the tables, low delay | Needs database settings and permissions, depends on the database's log format |
| Dual write (not CDC) | The app writes to the database and to another system | No extra tool | Can leave the two out of step if one write fails (see Outbox Pattern) |
| Phase | What happens | Why |
|---|---|---|
| Snapshot | Read all existing rows once and emit them as events | A new consumer needs the data that was there before the first log entry it sees |
| Streaming | Follow the log and emit each new change | Keeps the copy up to date from then on, starting from a saved log position |
A job runs SELECT ... WHERE updated_at > last_run every few changes. It only sees the rows that exist at that moment.
A small model with eight fixed changes and no random numbers. In a real system the log reader adds a little delay, and triggers and polling have more edge cases. The comparison of what each method can and cannot see is the part that carries over.
| Term | Meaning in easy words |
|---|---|
| CDC | Change Data Capture: turning each database change into an event |
| WAL / binlog | The log a database writes before changing tables (PostgreSQL calls it WAL, MySQL calls it binlog) |
| Log-based CDC | Reading that log to find changes, without querying the tables |
| Debezium | A well-known open-source CDC tool, often run on Kafka Connect |
| Change event | A message with the operation, the row before and the row after |
| Snapshot | The first full copy of existing rows, before the stream of changes starts |
| Log position (offset) | A marker saying how far in the log the reader has read |
| Replication slot | A PostgreSQL feature that remembers the reader's place so the log is kept for it |
| Tombstone | A special event used after a delete, so downstream stores can remove the key |
| Dual write | Writing to two systems from the app, which can leave them in disagreement |
| Lag | How far the copy is behind the source, in time or in log position |
| Choice | We gain | We lose |
|---|---|---|
| Log-based instead of polling | Complete data, deletes, low delay and less load | More set-up and a more complex stack |
| Triggers instead of log-based | Works where the log is not available | Slower writes and more load on the database |
| CDC instead of dual write | No chance that the two systems drift apart because of a failed second write | A new component to operate |
| Stream every table | Easy to start, flexible later | More data, more cost, more privacy risk |
| CDC events as the public API | Quick to build | Consumers depend on your table design; changing it breaks them |
| Short retention of the log | Less disk used | A consumer that is down too long must take a new snapshot |