Change Data Capture
Sooner or later, every design has data in more than one place. Posts live in Postgres, but search runs on Elasticsearch. Product prices live in MySQL, but reads come from a Redis cache. Orders live in the order service's database, but the analytics team wants them in the warehouse and the email service wants to know when one ships. Every time the source of truth changes, those other copies need to change too.
The obvious approach, having the application write to each place, turns out to be one of the most common sources of subtle bugs in distributed systems. Change data capture (CDC) is the alternative: treat the database's own change log as a stream of events, read it, and use it to update everything downstream. The application writes to one place, and the rest follows.
CDC shows up in interviews whenever you draw an arrow from a database to a search index, cache, warehouse or queue. Interviewers want to know how that arrow actually works, what happens when something crashes, and whether the copies can drift.
We'll build this up in steps:
- Why writing to several systems from the application breaks, and why "just poll the table" isn't much better.
- How log-based CDC works: reading the write-ahead log or binlog, what a change event looks like, and how tools like Debezium run it.
- The hard parts: ordering, duplicates, initial snapshots, schema changes and operations, then how CDC compares with the outbox pattern.
The Problem: Dual Writes
Suppose we want posts to be searchable. The first version most people write:
def update_post(post_id, title, body):
db.execute("UPDATE posts SET title = %s, body = %s WHERE id = %s", (title, body, post_id))
search.index("posts", post_id, {"title": title, "body": body})
This is a dual write, and it fails in several ways:
- Partial failure. The database update commits, then the process crashes, or Elasticsearch is briefly unavailable. The search index is now permanently stale for that post, and nothing will ever fix it.
- Reordering. Two requests update the same post at nearly the same time. Request A writes the database first, request B writes it second, so the database holds B's version. But B's index call happens to reach Elasticsearch first, then A's arrives. The index now holds A's version, and the two copies disagree indefinitely. No error was raised anywhere.
- Rollbacks. If the index write happens inside a database transaction that later rolls back, the index contains data that never existed.
- Every writer must remember. A batch job or an admin script that updates
postsdirectly bypasses the index update entirely.
The root issue is that two systems are being updated without any shared notion of order or atomicity. /system-design/transactions covers why distributed transactions aren't a practical fix across a database and a search engine. CDC sidesteps the problem: the database is the only thing the application writes, and the database's commit order becomes the order everyone downstream sees.