Change Data Capture (CDC) Fundamentals (Logical Replication & Binlogs)
Stream row changes from OLTP using logical replication or binlogs instead of heavy batch extracts.
In plain words
Configure binlog or logical decoder slot. Next, consistent snapshot plus starting log position. Finally, durable sink checkpoint, schema, lag, retention.
Think of it like…
CDC is a bank's signed change ledger rather than repeatedly photographing the whole branch account book; the ledger is compact, but falling behind can make the bank retain every unclaimed page.
Where you’ve seen it
PostgreSQL logical replication publishes (txid, lsn, table, op, before, after) from a slot; Debezium advances its database offset only after Kafka records are acknowledged, and a compaction key stores the latest row state.
Words you’ll hear
- CDC
- An event feed of committed inserts, updates, and deletes derived from database logs rather than periodic table diffs.
- Logical Replication
- A PostgreSQL change protocol exposed as transactional output at a replication slot, with a consumer-confirmed flush position.
- Binlogs
- MySQL's ordered transaction log used by native replication and CDC readers.
- Operational boundary
- A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules. Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay.
Visual diagram
Theory & deep dive
CDC: the working mechanism
Change data capture reads committed database transaction logs or a logical replication stream and emits row before/after images plus transaction metadata without polling every operational row.
Configure binlog or logical decoder slot. Consistent snapshot plus starting log position, then ordered transaction events with offsets; this exposes the state governed by Binlogs.
PostgreSQL logical replication publishes (txid, lsn, table, op, before, after) from a slot; Debezium advances its database offset only after Kafka records are acknowledged, and a compaction key stores the latest row state.
Change Data Capture Fundamentals: failure mode and production response
A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay. Track LSN/binlog lag as "durable source position minus acknowledged consumer position", and make WAL retention visible to the dataset owner.
How it works, step by step
Step 1 of 41. Capture
Configure binlog or logical decoder slot. Change data capture reads committed database transaction logs or a logical replication stream and emits row before/after images plus transaction metadata without polling every operational row.
- LSN/binlog lag
- durable source position minus acknowledged consumer position
- Logical Replication
- A PostgreSQL change protocol exposed as transactional output at a replication slot, with a consumer-confirmed flush position.
When to use it & trade-offs
What it is
Change data capture reads committed database transaction logs or a logical replication stream and emits row before/after images plus transaction metadata without polling every operational row. PostgreSQL logical replication publishes (txid, lsn, table, op, before, after) from a slot; Debezium advances its database offset only after Kafka records are acknowledged, and a compaction key stores the latest row state.
Why it matters: A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules. Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay.
When to reach for it
- Stream row changes from OLTP using logical replication or binlogs instead of heavy batch extracts.
- Choose CDC because it provides this capability: An event feed of committed inserts, updates, and deletes derived from database logs rather than periodic table diffs.
- Keep evidence for LSN/binlog lag at "durable source position minus acknowledged consumer position" so the last production stage-Commit and monitor - durable sink checkpoint, schema, lag, retention-remains testable.
When to avoid or be careful
- A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
- Do not conflate Logical Replication with CDC. The second concept contributes different behavior: A PostgreSQL change protocol exposed as transactional output at a replication slot, with a consumer-confirmed flush position.
- Release only after this guardrail is in place: Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay.
CDC / Logical Replication / Binlogs - roles, limits, and operating cost
Strengths: An event feed of committed inserts, updates, and deletes derived from database logs rather than periodic table diffs. Change data capture reads committed database transaction logs or a logical replication stream and emits row before/after images plus transaction metadata without polling every operational row.
Watch out: A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
Strengths: A PostgreSQL change protocol exposed as transactional output at a replication slot, with a consumer-confirmed flush position. In this workflow it supports: Stream - ordered transaction events with offsets.
Watch out: The operating cost is real: active slot can hold disk until decoded.
Strengths: MySQL's ordered transaction log used by native replication and CDC readers. Its output can be checked with Envelope.
Watch out: Operating guardrail: Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for CDC, and how would LSN/binlog lag reveal it?”
- “Why is Logical Replication distinct from this behavior: mySQL's ordered transaction log used by native replication and CDC readers?”
- Production clue: A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
- Design probe: Coordinate snapshot plus low-watermark streaming, monitor slot/binlog retention and consumer lag, acknowledge only durable downstream checkpoints, and version schemas and source transaction IDs for idempotent replay.
Benchmarks & rules of thumb
- LSN/binlog lag
- durable source position minus acknowledged consumer position
- WAL retention
- active slot can hold disk until decoded
- Envelope
- source transaction and primary-key metadata must survive compaction
Common mistakes & follow-ups
Common interview mistakes
- A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
- Reporting success from a job exit code without checking LSN/binlog lag and WAL retention.
- Treating this example as complete without replay, rollback, or access boundaries: PostgreSQL logical replication publishes
(txid, lsn, table, op, before, after)from a slot; Debezium advances its database offset only after Kafka records are acknowledged, and a compaction key stores the latest row state.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: An event feed of committed inserts, updates, and deletes derived from database logs rather than periodic table diffs.
- What does the recovery plan do when this occurs? A lost binlog/slot or abandoned consumer retains database WAL indefinitely, initial snapshots and live logs can overlap, and schema DDL may invalidate a decoder that lacks envelopes and compatibility rules.
- When is CDC insufficient for WAL retention, and what does adding Logical Replication cost in LSN/binlog lag?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Change Data Capture (CDC) Fundamentals (Logical Replication & Binlogs) with data engineer interview Q&A and hands-on practice.