Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 39/ 160
02. Advanced SQL & Relational Databases

Change Data Capture (CDC) Fundamentals (Logical Replication & Binlogs)

Stream row changes from OLTP using logical replication or binlogs instead of heavy batch extracts.

AdvancedCDC · Logical Replication · Binlogs3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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

Loading interactive 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.

Change Data Capture Fundamentals in one concrete design

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.

Key takeaway: 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

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
CDC

1. 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.

configure binlog or logical decoder slot
Key numbers at this step
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

CDC
Best for: An event feed of committed inserts, updates, and deletes derived from database logs rather than periodic table diffs.

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.

Logical Replication
Best for: A PostgreSQL change protocol exposed as transactional output at a replication slot, with a consumer-confirmed flush position.

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.

Binlogs
Best for: MySQL's ordered transaction log used by native replication and CDC readers.

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?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Graph Databases Basics (Neo4j & Cypher)
Next topic
Database Migration Tools (Alembic, Flyway, Liquibase)