josiete.com

CDC: keep data in sync without copying everything again

A transactional database sends small blocks of changes to an analytical system

Imagine a shop with ten million orders in its database. The analytics team needs to query today’s sales, so it copies those orders into an analytical warehouse every night. If only ten thousand changed today, transferring all ten million again is slow and expensive. Change Data Capture (CDC) identifies the changes so the pipeline can send only what is needed.

The idea is simple: after creating an initial copy, the pipeline observes which records are inserted, updated, or deleted at the source. Each change becomes an event that the destination can apply. This updates the affected part of the data without rebuilding the entire dataset on every run.

From the transactional system to analytics

The shop uses an OLTP database to record orders and payments. It is optimized for many short transactions: creating an order, changing its status, or cancelling a purchase. The business team uses an OLAP system — a data warehouse, for example — to aggregate sales by day, product, and region. These are different workloads, and analytical queries should not compete with the shop’s operations.

Without CDC, a simple solution would periodically export the entire orders table. With CDC, the flow changes:

Diagram: an initial copy moves data from OLTP to OLAP; afterwards the transaction log feeds a CDC capture process that sends only inserts, updates, and deletes to the analytical system

  1. Initial copy: existing orders are loaded into OLAP. This step is still needed when the destination starts empty.
  2. Capture: the CDC process reads committed changes from the database’s transaction log, when the engine and connector support it. PostgreSQL uses its WAL, for example, while MySQL uses its binlog.
  3. Apply: the pipeline transforms events and updates analytical tables. It must preserve the order key and handle deletions correctly.

Suppose the ten million orders have already been copied. Over the next hour, 8,000 are created, 1,500 are updated, and 500 are cancelled. The next step processes 10,000 changes, rather than another ten million rows. If order P-42 moves from “pending” to “paid,” the destination updates that order. If a record is deleted, it receives a deletion event instead of waiting to discover the missing row in a new full export.

What are the benefits?

  • Less data transferred: when only a small part of a table changes, there is no need to move the whole dataset on every cycle.
  • Less repeated work: full reads at the source and reloads at the destination are reduced, although capture still uses resources.
  • Fresher data: changes can flow continuously or in frequent batches, depending on the design and capacity of the systems. CDC does not guarantee zero latency.
  • Inserts, updates, and deletes are visible: a cursor such as updated_at usually finds new and modified rows, but needs another mechanism to detect physical deletes; the transaction log can include them.
  • More possible destinations: the same events can feed analytics, search, or caches, provided each consumer applies its own logic.

What needs attention

CDC is not a “set and forget” feature. The initial load and log reading must be coordinated so changes made during the copy are not lost. The process must also store its log position to resume after an interruption, and the source must retain logs long enough for it to catch up.

At the destination, applying events by key should be idempotent: retrying a delivery must not duplicate an order. Teams also need to monitor source-to-destination lag, schema changes, and the meaning of a deletion in analytical tables. If the saved log position is lost or the log expires before it is read, a new load or reconciliation may be necessary.

Common tools

Debezium provides connectors that capture database changes and publish them as events. Airbyte offers CDC synchronization in supported connectors to move data between a source and a destination. AWS Database Migration Service (DMS) can combine an initial load with ongoing replication, or capture only changes when the destination is already populated. Exact support and setup depend on the database and destination.

CDC does not remove the first copy or replace analytical data modeling. Its value lies in what happens afterwards: replacing millions of repeated row copies with a manageable stream of changes, so analytics can keep up with the business without starting over every time.