Uncorp
Log In
Data Engineering4 min readUpdated 2026-10-27

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.

Naitik Tailor & Uncorp Engineering Faculty
Log-Based Change Data Capture (CDC) Pipeline with Debezium, Kafka & Delta Lake
TL;DR — Key Engineering & Career Takeaways
  • 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:

Database Ingestion Patterns Comparison for Data Engineers
Ingestion MethodHow It Captures ChangesCaptures Hard Deletes?Production OLTP Impact & Verdict
1. Full Table Dump (`overwrite`)Copies entire table nightly via JDBCYes (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 minsNo (deleted rows vanish without a trace)Adds index/CPU load on OLTP DB; misses intermediate state transitions between runs
3. Database TriggersFires SQL trigger on every write to copy row into a shadow audit tableYesAnti-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 slotYes (`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:

Enterprise Log-Based CDC & Lakehouse Upsert Architecture
[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.

Written & Reviewed By Uncorp Engineering Faculty

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

About Uncorp

Continue Reading in This Topic Cluster

All 73 Guides