Topic 119 / Advanced
Change data capture (CDC)
Chapter 7.4 · Data Consistency Across Systems
What and why
The outbox pattern (→ 118) works when you control the writing service's code; many events need to be derived from a database whose write paths you don't control, or from every table's changes without hand-instrumenting each one. Change data capture tails a database's own write-ahead log (→ 68) and turns each row-level insert, update, and delete into an event on a stream, with no extra application code per write. Builds on: → 68 (write-ahead logging and durability — CDC's source of truth is that log).
Diagram
app writes a row ──► database WAL ──► CDC connector reads the log
│
▼
emits {table, op, before, after}
│
▼
stream ──► cache invalidation (→ 85)
──► search index update (→ 65)
──► analytics feed (Project 19)
How it works
- A CDC connector (Debezium is the conceptual example) attaches to the database's replication log stream — the same mechanism used for physical replication (→ 72), not a separate polling query against the tables.
- Every committed row change (insert, update, delete) appears in the log and is turned into a structured event: table, operation, before/after values.
- The connector publishes these events to a stream, commonly one topic per source table (→ 111).
- Downstream consumers react — invalidating a cache entry (→ 85), updating a search index (→ 65), feeding an analytics pipeline (Project 19) — without the original application ever publishing anything itself.
- Because it reads the committed log, CDC sees every change exactly as the database applied it, including changes made by other processes or direct SQL — which an application-level outbox (→ 118) would simply never see.
Practical example
(illustrative). A product-catalog database updates price both
through the normal application and through an occasional bulk-import script that writes
SQL directly. An outbox table populated only by application code would miss every
bulk-import price change; a CDC connector tailing the database log captures both equally,
because it only cares what changed in the table, not which code path changed it.
When to use it
- Use when events must be derived from a database whose write paths aren't fully controlled, or every table's changes need capturing without instrumenting each write site.
- Use the outbox (→ 118) instead when the writes are controlled and intentional, business-meaningful events are wanted rather than raw row diffs.
- Failure scenarios: CDC emits what changed in the table, which is not the same as the business event a consumer actually wants ("price changed from 10 to 9" is not "a flash sale started") — mapping row diffs to meaningful events is extra work; a connector that falls behind the database's log retention loses changes permanently, the same risk as a consumer falling behind Kafka's retention (→ 111).
- Simpler alternative: if all the write paths are owned, the outbox pattern gives cleaner, intentional events with far less infrastructure than a log-tailing connector.
Practice
(pattern — implement minimal; 20–30 min, lab Postgres + Kafka if a CDC connector is available, otherwise simulate by hand). Enable logical replication on a Postgres table and consume the resulting change stream (or, without a connector, diff two successive dumps of the table to produce before/after change records). Update a row via direct SQL, outside any "application" path, and confirm the change event is indistinguishable from one made through a normal update statement. Done when a direct-SQL change and an application-code change produce the same shape of CDC event.
Shared lab setupKey takeaway
CDC derives events from the database's own committed log, catching every change regardless of which code path made it, at the cost of raw row diffs instead of intentional business events. Done when you can: state the boundary rule between outbox and CDC, and name one change CDC would see that an outbox table would miss. Used next in → 111 (CDC output commonly lands in a Kafka-style log), → 124 (stream processing often consumes a CDC stream); Project 19.
References
Designing Data-Intensive Applications — see references.md.
Source directory