LESSON 04 · Change data capture

Follow the database’s own log

Change data capture reads committed changes from a database’s replication log, so derived copies such as search indexes and caches can follow without extra writes in application code.

SYSTEM VIEW

The data flow

Diagram view
App writes—
Slot lag—
Search lag—

Watch one price change reach a search index and a cache, first by writing every copy, then by following Postgres’s own log.

  1. The App writes every copy itself. It commits price 19 for product 7 to Postgres, then crashes before updating the search index and the cache, which still say 24. Concurrent updates can also reach each copy in a different order.

    Postgres
    price 19
    Search, cache
    price 24

    What changed

    • Postgres: product 7 at 19
    • App crashes before the next write
    • Search and cache still at 24
  2. CDC reads the log Postgres already writes for replication, its write-ahead log (WAL). A connector such as Debezium reads the committed update at 882 and publishes it to a Kafka topic as an event keyed by primary key 7. The App writes only to Postgres.

    Log
    WAL; MySQL’s binlog
    Event key
    primary key 7

    What changed

    • Connector reads WAL 882
    • Key 7 at partition 2, offset 311
    • App writes Postgres only
  3. The search indexer reads the event and upserts document 7 at price 19. A cache consumer deletes the key product:7, so the next read misses and loads 19 from Postgres. Every copy now agrees, and the App wrote once.

    What changed

    • Search: doc 7 at 19
    • product:7 deleted from the cache
    • Every copy agrees with Postgres
  4. Product 7 is deleted, a change that polling the table would miss. The delete commits at WAL 883 and takes the same path: keyed by 7, it lands in partition 2 at offset 312, right after the update, so no consumer can apply the two out of order.

    Partition 2
    update, then delete
    Polling the table
    misses hard deletes

    What changed

    • 7 deleted at WAL 883
    • 7 deleted at partition 2, offset 312
    • Right after 7’s update
  5. The cache consumer reads offset 312 and deletes product:7 at once. The search indexer hasn’t reached it, so search still lists product 7. Every copy trails Postgres by however long this pipeline takes.

    Cache
    product:7 deleted
    Search
    1 record behind

    What changed

    • product:7 deleted from the cache
    • Search still lists product 7
  6. Another team’s service starts reading product changes. The WAL holds only recent changes, not every product, so the connector first snapshots the table, every row through the topic, and the team then follows the log from there. Search has caught up.

    What changed

    • Other team, a new consumer
    • Snapshot of every product
    • Search caught up: doc 7 removed
  7. Postgres keeps WAL for a replication slot until its consumer has read it, so the connector can resume without gaps. When the connector stalls and writes go on, unread WAL piles up behind the slot, 3 records here, until it can fill the disk.

    What changed

    • Connector stalls
    • 3 unread WAL records kept, 884–886
    • Postgres disk filling
  8. CDC events mirror table columns, so renaming one breaks the other team. For events others rely on, the App writes a designed event, PriceChanged, to an outbox table in the same transaction as its change (WAL 887 and 888), and the connector relays it. An outbox needs code in every writer; CDC also captures scripts.

    CDC
    copies you own
    Outbox
    events others rely on

    What changed

    • Connector back, slot read
    • Product 9 at 16 and outbox 9, one transaction
    • Other team reads PriceChanged, not columns

Space plays · ← → step · Home resets

THE IDEA

How it works

A search index and a cache are derived copies of your database. If the application updates all three, a crash between writes leaves them disagreeing, and concurrent updates can reach each copy in different orders. Polling a table for recent rows misses hard deletes and any value overwritten between polls. Change data capture, or CDC, instead reads the log the database already writes for replication: the write-ahead log (WAL) in Postgres or the binlog in MySQL. A connector such as Debezium turns committed changes into events, commonly published to Kafka.

UPDATE products SET price = 19 WHERE id = 7 commits. The connector reads the change and publishes an event keyed by primary key 7. The search indexer upserts document 7, and a cache consumer deletes the key product:7. Keying by row ID keeps each row’s changes in order, and a delete travels the same path. The copies lag by however long this pipeline takes.

The log holds only recent changes, so a new consumer needs an initial snapshot. In Postgres, a replication slot retains WAL until its consumer reads it, so a stalled connector can fill the disk. Events also mirror table columns, coupling consumers to your internal schema. The outbox pattern answers that: the application writes a designed event, such as OrderPlaced, to a table in the same transaction as its change. That gives a stable contract but needs code in every writer, while CDC also captures scripts. A common split is CDC for copies you own and an outbox for events other teams depend on.

DECISION POINT

Your orders table feeds a search index your team owns. Another department wants “order shipped” events, and you plan to split the orders table next quarter. How do you feed each consumer?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • CDC reads committed changes from the log, so application code needs no second write.
  • Key change events by primary key to keep each row’s changes in order.
  • Use CDC for copies you own and an outbox for contracts other teams rely on.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

Disk use on a Postgres primary climbs steadily though write volume is flat. The Debezium connector has been down for two days. What is the likely cause?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.