Change Data Capture (CDC)
How change data capture turns every committed write in a database into an ordered stream of events, so search indexes, caches and warehouses stay in step without dual writes.
Overview
The problem: one write, many copies
Most applications keep the same facts in several places. The rows live in a relational database, a search index holds a copy for full-text search, a cache holds hot rows, and a warehouse holds everything for reports. Each copy is derived data: it can be rebuilt from the database, which is the system of record (the one place whose answer wins).
The hard part is keeping the copies current. There are three common ways, and only one of them is reliable.
- Nightly dump: copy whole tables on a schedule. The copies are hours old, and large tables take long to scan.
- Dual write: the app writes the database, then the index or broker. One write can fail after the other succeeds, and two writers can race.
- Change data capture (CDC): read the database's own change log and publish each change. It needs access to the log and care with the log's retention.
Key concepts
- Change data capture
- Dual write
- Write-ahead log (WAL)
- Replication slot
- Replica identity
- Initial snapshot
- Tombstone
- Outbox pattern
Interview questions
- How would you keep Elasticsearch in sync with a PostgreSQL database?
- What is the dual-write problem, and how do CDC and the outbox pattern solve it?
- What can go wrong operationally with PostgreSQL CDC?
- How is CDC different from event sourcing?
Sign up free to read the full topic
More DDIA topics
- ACID Properties
- B-Trees and Write-Ahead Logs
- Beyond MapReduce: Dataflow Engines
- Clocks and Time in Distributed Systems
- Column-Oriented Storage and Compression
- Consensus Algorithms: Paxos and Raft
- Consistency Guarantees: Eventual, Causal and Linearizable
- Declarative vs Imperative Query Languages
- Encoding Formats: JSON, Protobuf, Avro, Thrift
- End-to-End Correctness and the Exactly-Once Myth
- Event Streams and Message Brokers
- Fan-Out and the Twitter Timeline Problem
Start free on Cariara · Coding problems · System design studio