← Back to System Design, page 5

system-design-0088 · Medium fresher

What is change data capture (CDC), and how do tools like Debezium stream database changes to caches, search indexes and analytics without dual writes?

CDC reads the database's own change log (the MySQL binlog or PostgreSQL WAL) and turns every committed insert, update and delete into an event on a stream such as Kafka. Downstream consumers update caches, search indexes or warehouses from that stream. Because events come from committed transactions, in commit order, it avoids the dual-write problem where the application writes to the database and to another system and one of them fails.

Full answer

The dual-write problem

An application saves a course to PostgreSQL and then updates Elasticsearch. If the second call fails or the process crashes between them, the search index is wrong forever. Doing it in the other order risks indexing something that was never saved. Two writes to two systems cannot be made atomic without distributed transactions.

How CDC works

  1. The database already writes every committed change to its log for crash recovery and replication.
  2. A CDC connector (Debezium, or managed services from cloud providers) connects like a replica, reads the log and converts each change into an event: table, primary key, before and after values, operation type, transaction ID and log position.
  3. Events go to a stream, typically one Kafka topic per table, partitioned by primary key so changes to one row stay in order.
  4. Consumers apply events: update the cache, upsert into the search index, load into the warehouse.

The connector stores its log position, so after a restart it continues where it stopped. Delivery is at-least-once, so consumers must be idempotent (an upsert by primary key is naturally idempotent).

Initial snapshot

A new consumer needs existing data too. Connectors take a consistent snapshot of the tables, stream it, then switch to the log from the snapshot's position, with no gap.

The outbox pattern

Raw table changes expose internal schemas. With an outbox, the application writes its business row and an outbox row (a clean event such as CourseCreated) in the same transaction. CDC streams the outbox table. Consumers get stable, meaningful events, still without dual writes.

Uses

  • Keeping caches and search indexes in sync.
  • Feeding a data warehouse in near real time instead of nightly dumps.
  • Migrating between databases with live replication before the switch.
  • Building event-driven services from an existing monolith.

Trade-offs

  • Consumers are eventually consistent, usually seconds behind.
  • Schema changes in the source must be handled by consumers (a schema registry helps).
  • The database must keep log files until the connector reads them; a stopped connector can fill the disk, so monitor its lag.

By level

Fresher: Explain that the database already records every change in a log, and CDC turns that log into messages other systems can use. A fresher should describe the problem of writing to two systems.

Mid: Describe connectors, log positions, per-key ordering, snapshots and idempotent consumers. Introduce the outbox pattern.

Senior: Discuss schema evolution, log retention risks, migration use, ordering across tables and lag monitoring. Expect a clear comparison of raw CDC and the outbox.

Key points

  • CDC reads the database's change log and emits an event per committed change
  • Avoids dual writes to the database and another system
  • Partition by primary key to keep per-row order
  • At-least-once delivery needs idempotent consumers
  • Outbox pattern gives clean business events through CDC

Common mistakes

  • Writing to the database and the search index from application code and hoping both succeed
  • Assuming CDC events are exactly-once
  • Exposing internal table schemas as public events with no outbox
  • Forgetting that a stopped connector can fill the database disk

Follow-ups

  • How does the initial snapshot avoid missing changes?
  • How would you handle a column rename in the source table?
  • When would you use the outbox instead of raw table CDC?

Tips

  • Start with the dual-write failure
  • Describe one event's fields
  • Mention idempotent consumers

Evaluation rubric

  • Explains dual-write risk
  • Correct CDC mechanics with logs and positions
  • Idempotency, ordering and snapshots
  • Outbox and operational trade-offs