CDC (Change Data Capture) — Data Engineering

CDC captures every INSERT, UPDATE and DELETE from a source database and passes it on in near real time, without copying whole tables. A sync with WHERE…

Bookshop's warehouse syncs every hour with WHERE updated_at > last_run. On Saturday at noon, the source and the warehouse both have 19 rows, and everything looks fine. Yet two rows differ: one the admin deleted is still in the warehouse, and one new order hasn't arrived. CDC (Change Data Capture) closes holes like these: it captures every change in a source database (INSERT, UPDATE, DELETE) as it happens and passes it on, so the copy in the warehouse changes along with it without copying whole tables. It's like a bank account. Checking the balance only gives you today's number. The account statement lists every transaction in order: money in, money out, reversals. From the opening balance plus the statement, today's balance can be worked out again, and no transaction is skipped. CDC reads the database's "statement".

Why copying again isn't enough

The simplest way: every night, copy all of order_items to the warehouse. For 19 rows that's fine. For 50 million rows, every night reads 50 million rows when perhaps a thousand changed, and the warehouse is still a day late.

The second way is incremental: fetch only the rows that changed since the last sync. Laravel tables have an updated_at column from $table->timestamps(), so the query looks easy:

INSERT INTO wh_order_items
SELECT * FROM order_items
WHERE updated_at > (SELECT last_run FROM sync_state)
ON CONFLICT (id) DO UPDATE SET
  order_id = excluded.order_id, book_id = excluded.book_id,
  qty = excluded.qty, price = excluded.price, updated_at = excluded.updated_at;

UPDATE sync_state SET last_run = (SELECT MAX(updated_at) FROM wh_order_items);

Syncing on updated_at: what slips through

Saturday 10 October in Bookshop's MySQL, with the last sync on Friday at 23:00:

Time What happens updated_at
09:00:00 an admin deletes item 9, the doubled line (the row is gone)
10:15:30 an admin fixes item 16's price: 190 becomes 19 10:15:30
10:15:30 the sync runs: updated_at > '2026-10-09 23:00:00' fetches item 16, last_run becomes 10:15:30
10:15:30 Siti checks out: order 15, item 20 (This Earth of Mankind, 24) 10:15:30
11:00:00 the sync runs: updated_at > '2026-10-10 10:15:30' fetches 0 rows

Then the source is compared with the warehouse (FULL OUTER JOIN on id):

id source warehouse problem
9 NULL 25 deleted at the source, still in the warehouse
20 24 NULL in the source, not yet in the warehouse

Both have 19 rows, yet two rows differ. The two holes:

Laravel has a middle way: soft deletes (deleted_at), so a delete becomes an update. But that only helps the tables that use it.

Log-based CDC: reading the binlog

MySQL already records every change in its binary log (binlog), the statement used for replication. With binlog_format=ROW and binlog_row_image=FULL (the defaults in MySQL 8), each changed row is recorded in full: its content before and after, deleted rows included.

Debezium (latest stable release 3.7.0.Final, 29 September 2026) reads the binlog the way a replica does, then sends one change event per row change to Kafka through Kafka Connect. The topic is <topic.prefix>.<database>.<table>, e.g. bookshop.bookshop.order_items, and the key is that row's primary key: {"id": 16}. The event for item 16's price fix, shaped like the example in Debezium's documentation (shortened: the updated_at column and part of source are left out):

{
  "before": { "id": 16, "order_id": 12, "book_id": 4, "qty": 1, "price": 190 },
  "after":  { "id": 16, "order_id": 12, "book_id": 4, "qty": 1, "price": 19 },
  "source": {
    "version": "3.7.0.Final",
    "connector": "mysql",
    "name": "bookshop",
    "ts_ms": 1791623730000,
    "db": "bookshop",
    "table": "order_items",
    "file": "mysql-bin.000003",
    "pos": 484,
    "row": 0
  },
  "op": "u",
  "ts_ms": 1791623730412
}

In the Lab, the binlog is a SQLite table filled by triggers, so you can see what's in it. MySQL writes its binlog itself, with no triggers and no load on the app's queries.

Applying events to the warehouse

A consumer reads the events and applies them: c, u and r become an upsert of after; d becomes a DELETE by key. When one id has several events in a batch, the last event wins:

INSERT INTO wh_order_items (id, order_id, book_id, qty, price, updated_at)
SELECT e.id, json_extract(e.after, '$.order_id'), json_extract(e.after, '$.book_id'),
       json_extract(e.after, '$.qty'), json_extract(e.after, '$.price'), json_extract(e.after, '$.updated_at')
FROM binlog e
WHERE e.op IN ('c', 'u') AND e.pos = (SELECT MAX(pos) FROM binlog WHERE id = e.id)
ON CONFLICT (id) DO UPDATE SET
  order_id = excluded.order_id, book_id = excluded.book_id,
  qty = excluded.qty, price = excluded.price, updated_at = excluded.updated_at;

DELETE FROM wh_order_items
WHERE id IN (SELECT e.id FROM binlog e
             WHERE e.op = 'd' AND e.pos = (SELECT MAX(pos) FROM binlog WHERE id = e.id));

Saturday's binlog holds 3 events: d for item 9, u for item 16, c for item 20. Once they are applied, comparing the source with the warehouse: 0 rows differ. In BigQuery, Snowflake or DuckDB these two steps are usually one MERGE.

Outbox pattern

CDC straight from a table sends the app's raw rows; rename a column and the consumers break too. When the app wants to send a tidy business event ("order created"), don't save to the database and then send to Kafka as two separate steps: if the second fails, the two no longer agree. Write the event to an outbox table in the same transaction as the order, and let Debezium read that table:

DB::transaction(function () use ($order) {
    $order->save();

    DB::table('outbox')->insert([
        'id' => (string) Str::uuid(),
        'aggregatetype' => 'order',
        'aggregateid' => (string) $order->id,
        'type' => 'OrderCreated',
        'payload' => json_encode(['order_id' => $order->id, 'customer_id' => $order->customer_id]),
    ]);
});

⚠️ Changes that quietly never reach the warehouse

TL;DR: CDC captures every INSERT, UPDATE and DELETE from a source database and passes it on in near real time, without copying whole tables. A sync with WHERE updated_at > last_run misses DELETEs (item 9 stays behind) and changes in the same second as the sync (item 20 is skipped), although both sides have 19 rows. Log-based CDC reads MySQL's binlog (binlog_format=ROW): Debezium 3.7 sends one event per row to Kafka with before, after, source, op (c, u, d, r, t) and ts_ms, keyed by the primary key. The warehouse applies them as an upsert for c/u/r and a DELETE for d, the last event per key wins, and that is idempotent. For business events, use the outbox pattern: write the event to an outbox table in the same transaction, and Debezium sends it.

⚡ Syncing on updated_at

Write:

INSERT INTO wh_order_items
SELECT * FROM order_items
WHERE updated_at > (SELECT last_run FROM sync_state)
ON CONFLICT (id) DO UPDATE SET
  order_id = excluded.order_id, book_id = excluded.book_id,
  qty = excluded.qty, price = excluded.price, updated_at = excluded.updated_at;

Meaning: Fetch only the rows that changed since the last sync, then upsert them into the warehouse. It looks like enough, but things slip through.

⚡ What slips through

Guess first: an admin deletes item 9 with a DELETE. Does the next updated_at sync remove it from the warehouse too?

No. A deleted row has no updated_at left to read, so item 9 lives on in the warehouse. A change in the same second as the sync is missed too with >.

Meaning: A sync based on updated_at is blind to DELETEs.

⚡ CDC reads the binlog

Meaning: CDC captures every INSERT, UPDATE and DELETE from the database's log (MySQL's binlog) and passes it on in near real time, without copying whole tables. Debezium sends one change event per row: before, after and op (c, u, d).

⚡ The last event wins

Meaning: c and u become an upsert of after, d becomes a DELETE by key. When one id has several events, only its last event is applied. An upsert and a DELETE by key are safe to run twice.

What you will learn