Change Data Capture (CDC) Architecture Explained: Streaming PostgreSQL & MySQL to Databricks & Snowflake with Debezium & Kafka
Why nightly `SELECT * WHERE updated_at > yesterday` batch jobs miss hard deletes and overload production OLTP databases—and how Log-Based CDC works.

- Query-based polling (`SELECT * FROM orders WHERE updated_at > :last_run`) has three fatal flaws in production: it hammers your live customer OLTP database with heavy scans, it relies on developers never forgetting to update `updated_at`, and it is 100% blind to physical `DELETE` statements.
- Log-Based Change Data Capture (Debezium) reads directly from the database's internal transaction log (PostgreSQL Write-Ahead Log `WAL` or MySQL `binlog`) with near-zero CPU impact on the primary database, capturing every `INSERT (c)`, `UPDATE (u)`, and `DELETE (d)` event in milliseconds.
- Downstream in Databricks Delta Lake or Apache Iceberg, CDC streams are deduplicated per batch window (`ROW_NUMBER() OVER (PARTITION BY id ORDER BY lsn DESC) = 1`) and applied via atomic `MERGE INTO` upserts.
1. Query-Based Polling vs Triggers vs Log-Based CDC (Debezium)
When an interviewer asks 'How will you replicate 50 million operational rows from production PostgreSQL into our Lakehouse with < 1 minute freshness?', walk through this comparison matrix:
| Ingestion Method | How It Captures Changes | Captures Hard Deletes? | Production OLTP Impact & Verdict |
|---|---|---|---|
| 1. Full Table Dump (`overwrite`) | Copies entire table nightly via JDBC | Yes (by replacing everything) | Fails above a few million rows; wastes massive network & compute I/O |
| 2. Watermark Polling (`updated_at`) | Queries `WHERE updated_at > last_watermark` every 15 mins | No (deleted rows vanish without a trace) | Adds index/CPU load on OLTP DB; misses intermediate state transitions between runs |
| 3. Database Triggers | Fires SQL trigger on every write to copy row into a shadow audit table | Yes | Anti-pattern at scale: slows down every customer write transaction by 20–40% |
| 4. Log-Based CDC (Debezium + Kafka) | Tails PostgreSQL `WAL` / MySQL `binlog` asynchronously as a replication slot | Yes (`op: 'd'` includes `before` row image) | Production Standard: <1% OLTP overhead, sub-second latency, captures every state transition |
2. End-to-End CDC Pipeline: Postgres WAL → Debezium → Kafka → Delta Lake
Here is how a change event flows from a microservice database commit to your Medallion Lakehouse:
[PostgreSQL Primary DB] ──► App writes UPDATE orders SET status='SHIPPED' WHERE id=501
──► [Postgres Write-Ahead Log (WAL) records LSN=984120: { op: 'u', before: {...}, after: {...} }]
──► [Debezium Connector (Kafka Connect)] reads WAL replication slot & publishes to Kafka `dbserver1.public.orders`
──► [Spark Structured Streaming / Databricks Auto Loader] lands raw CDC events in Bronze Table (Append-Only)
──► [Silver Layer Deduplication]: Keep latest event per `id` using `ROW_NUMBER() OVER (PARTITION BY id ORDER BY lsn DESC) = 1`
──► [Delta Lake MERGE INTO silver.orders]:
WHEN MATCHED AND src.op = 'd' THEN DELETE
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED AND src.op != 'd' THEN INSERT *3. The #1 Spark/Delta Lake CDC Interview Bug: Multiple Updates in One Micro-Batch
Suppose `order_id = 501` is updated twice within the same 5-minute Spark micro-batch (`CREATED → PAID → SHIPPED`). If you run `MERGE INTO silver.orders USING raw_cdc_batch ON target.id = src.id` without deduplicating `raw_cdc_batch` first, Delta Lake / Snowflake will throw an error: `UnsupportedOperationException: Cannot perform Merge as multiple source rows matched and attempted to modify the same target row`.
Always deduplicate the incoming micro-batch by primary key ordered by the database log sequence number (`lsn` or `source_ts_ms DESC`) before executing `MERGE INTO`!
Frequently Asked Questions
What risk must you monitor when enabling PostgreSQL Replication Slots for Debezium?
If the Debezium connector stops consuming and remains down for days, PostgreSQL will retain WAL segments on disk waiting for the replication slot to catch up—which can fill up the primary database disk. Always set `max_slot_wal_keep_size` in Postgres and alert on replication slot lag.
Where does Uncorp teach Kafka streaming and Lakehouse pipelines?
In Uncorp's Data Engineering Module 3 (Big Data, Spark, Databricks, Airflow, Kafka & dbt) and the 120-hour Data Engineering Master Program.
Naitik Tailor & Uncorp Engineering Faculty
Founder & Principal Engineering Mentors (12+ Yrs Enterprise Experience) · VIT CSE '21 · Symbiosis MBA '24 · Ex-Delhivery & Ex-Aditya Birla Capital
Continue Reading in This Topic Cluster
All 73 Guides
Apache Kafka Event Streaming & Open Table Formats (Delta Lake vs Apache Iceberg) Explained for Data Engineers
How real-time event streaming (Kafka Partitions, Consumer Groups, Offset Commits) connects to ACID Lakehouse tables (Delta Lake & Iceberg Time-Travel, Compaction & Upserts).

How Production Lakehouse Pipelines Work: PySpark, Apache Airflow, Kafka & dbt Architecture
Walk through a real-world enterprise data architecture—from Kafka event ingestion and S3 Bronze storage to PySpark Silver transformations, dbt Gold marts, and Airflow orchestration.

dbt (data build tool) + Apache Airflow in Production: Incremental Models, SCD Type 2 Snapshots & DAG Orchestration
How modern data teams combine Apache Airflow scheduling with dbt SQL transformations, automated schema testing, and Slowly Changing Dimension (SCD Type 2) history tracking.

