SI Database / Journal

What Is Change Data Capture? How Databases Stay in Sync

On this page
A source database with a log strip of insert, update, and delete markers streaming into a second database copy.
Schematic of the streaming phase after any initial copy. Capture scope, delivery, and downstream processing determine freshness. Diagram by SI Database.

Change data capture, or CDC, is a technique that detects the inserts, updates, and deletes happening in a database and delivers those changes to other systems. Instead of copying an entire table repeatedly, CDC passes along only what changed, so a copy elsewhere, such as a reporting database or a search index, stays close to the original.

Take an online store. Order 1042 moves from “packed” to “shipped” at 2:14 p.m. The store’s database records the change at once. A reporting warehouse and a search index each hold their own copy of orders, and each needs to learn about the change without reloading millions of rows. CDC is one way to get it there. (The store is our illustration.)

From full copies to change streams

The simplest alternative to CDC copies everything on a schedule. It works for small tables and grows slow and expensive as tables get larger.

A second method asks the table: fetch the rows whose “updated at” timestamp is newer than the last run. It is cheap, but it depends on every table having a trustworthy timestamp, and a deleted row leaves nothing behind to query. Unless deletions are tracked separately, a nightly job built this way would keep showing orders that someone had removed. It can also miss updates if timestamps or polling boundaries are handled incorrectly. (This is our reasoning about the method, not a claim from a source.)

The third method reads the database’s own record of changes. Many transactional databases keep a recovery log. PostgreSQL calls its log the write-ahead log, or WAL. With logical replication configured, log-based CDC can read committed row changes for the tables included in capture, including deletes, without adding timestamp columns to those tables.

How PostgreSQL exposes its log

PostgreSQL’s documentation refers to the technique as logical decoding. It extracts persistent changes to a database’s tables from the WAL and presents them in a form that a program can read without needing to know how the database stores data internally. An output plugin decides the format.

A replication slot holds a consumer’s place in that stream. The documentation describes a slot as a stream of changes that can be replayed to a client in the order they were made. Slots persist independently of any connection, and they survive crashes. Two details matter in practice:

  • Duplicates are possible. In normal operation, a slot emits each change once. After a crash, it may resend recent changes, so the receiving software has to tolerate duplicates.
  • An idle slot costs disk space. A slot prevents the server from deleting the log files it still needs, even when nothing is connected. In extreme cases, the documentation warns, this could shut down the database. Its advice is direct: “So if a slot is no longer required, it should be dropped.”

What a CDC tool adds

Debezium is a CDC tool that streams changes from source databases into messaging systems, most commonly Apache Kafka. Its PostgreSQL connector reads from a logical replication stream, and its MySQL connector reads the MySQL binary log. By default, Debezium’s documentation says, “changes from one database table are written to a Kafka topic whose name corresponds to the table name.” Downstream programs, such as a warehouse loader or a search indexer, subscribe to the topics they need.

On its first start without a saved position, the PostgreSQL connector in its default initial snapshot mode copies the existing rows from the tables it captures, then streams changes from the recorded log position. On later starts with a usable saved position, it normally resumes streaming without repeating that snapshot. Other snapshot modes change that behavior; no_data, for example, skips copying existing rows. Events use before and after fields, but those names do not guarantee complete row images. Available old values depend on the table’s REPLICA IDENTITY setting: the default usually supplies only key columns, while FULL requests the old row. Snapshot rows arrive with an operation code of r, for read, so a consumer can distinguish an existing copied row from a new insert.

A connector that stays offline for too long can lose the log position it needs. A replication slot retains required WAL, but server retention limits, a dropped slot, or an invalidated slot can make older changes unavailable. An inactive slot can instead let WAL accumulate and exhaust storage. Recovery depends on the configuration: Debezium’s when_needed mode can take a new snapshot if the saved position is unavailable, but a connector is not guaranteed to recover automatically. Someone must monitor retention and plan how the downstream copy will be rebuilt.

Why CDC matters for AI systems

Many AI retrieval systems maintain search indexes, sometimes including vectors, derived from source documents. When a source document changes and nobody updates the index, the assistant keeps answering from the old version. For documents stored in captured tables, CDC provides one way to notice an edit; downstream software still has to regenerate any affected embeddings and update or remove the matching index entries. Our guides to vector embeddings and vector databases explain what is stored, and our explanation of retrieval-augmented generation shows why a stale copy produces a stale answer. (The link between CDC and index freshness is our application of the technique, not a statement from the documentation.)

The same logic applies to analytics. A warehouse that loads once a night is up to a day behind its source, and CDC can shrink that gap. Our overview of warehouses, lakes, and lakehouses describes the stores that often sit at the receiving end.

Three questions before adopting it

  • What is the source of truth? In a one-way copy, downstream edits need an explicit policy; otherwise the copy can drift from the source or be overwritten by the next update.
  • How late can the copy be? A dashboard that refreshes daily needs less than a fraud alert does.
  • What happens when the stream stops? Decide who is alerted, how long the log is kept, and how to rebuild from a snapshot.

CDC does not make a copy correct. It can keep the copy current when capture, delivery, and downstream updates all succeed.

Sources and further reading

Read next

Know someone who would find this useful?

Share it with a colleague or post it where your team talks about data.

Cover of The Data Behind AI

The Data Behind AI

A 48-page illustrated guide to databases, search, and intelligent systems. No coding required.

Explore the book →

← Back to the journal

Get new SI Database articles by email

One short email when we publish, plus news from the other Neura Collective sites. No spam.