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.
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?
CDC suits the search index, because your team owns both ends and can update the indexer when the table splits. The other department should get an outbox event with a documented contract, because raw row changes would break for them at the split. Both can share one pipeline, since a connector can read the outbox table from the log too.
Use this as a guide for self review; written answers are not graded.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.