← All lessons

Patterns

Change data capture

Turn every committed change in your database into an event, without the application having to remember to publish it. The pattern behind search indexes, caches, analytics and outboxes that never drift.

18 min readUpdated November 2026Premium

Your product data lives in Postgres. Search needs it in Elasticsearch, the cache needs to know when it changes, the analytics warehouse wants all of it, and the recommendations service wants to react to new orders. The naive answer is that the application writes to all of them. The question that sinks it: what happens when one of those writes fails?

The dual-write problem

UPDATE products SET price = 899 WHERE id = 7;   -- succeeds
index_into_elasticsearch(product_7)              -- times out

Now the database says ₹899 and search says ₹999, and nothing will ever reconcile them. Retrying in the application helps until the process crashes between the two calls. Writing to the index first is worse — the index can show a change that the database then rolled back.

Every variation has the same flaw: two systems, no transaction spanning them, so they can disagree. And every write path must remember to do it — the admin panel, the migration script, the on-call engineer’s one-off UPDATE.

What CDC is

The database already writes every committed change, in commit order, to its own log — the Postgres WAL, the MySQL binlog, the MongoDB oplog. Replication works by shipping that log to replicas. Change data capture reads the same log and turns each change into an event:

{
  "op": "u",
  "source": { "table": "products", "lsn": 23849120 },
  "before": { "id": 7, "price": 999 },
  "after":  { "id": 7, "price": 899 }
}

Because it reads the log, it sees every committed change and only committed changes, from every write path, in commit order, with no application code involved. Debezium is the standard open-source tool; it publishes to Kafka, one topic per table. Managed equivalents include AWS DMS and Google Datastream.

The point

CDC makes the database the single source of truth and everything else a derived view of it. Search, caches and the warehouse become things you can always rebuild from the log, rather than things the application must keep in sync by hand.

Where it gets used

The members’ section covers how it works underneath and everything that goes wrong in production.

Premium

The rest of this lesson is ₹1

Logical decoding and replication slots, snapshots and the initial load, the outbox via CDC, ordering and schema changes, the replication-slot disk trap, and the interview follow-ups.

PhonePe, Google Pay, Paytm or any UPI app. Cards and net banking work too. One payment unlocks this lesson on this device.