Project 03 · Change data capture

CDC Pipeline: Postgres to Snowflake

The order service writes to PostgreSQL. Debezium reads the database's write-ahead log through logical replication, so every insert, update and delete becomes a Kafka event without touching the application or querying its tables. A Flink job flattens and cleans the changes while keeping them in order per row, Snowpipe Streaming lands them in Snowflake within seconds, and Dynamic Tables merge them into copies of the source tables that stay in sync row for row.

  • PostgreSQL
  • Debezium
  • Apache Kafka
  • Apache Flink
  • Snowflake
Sample data Tuesday, Oct 6, 2026 · 24 hours, US Central Download data (CSV)

Replication lag

Commit in PostgreSQL to change row in Snowflake, p50 and p95

Changes by table

Change events per source table today

Change operations per hour

Inserts, updates and deletes

Pipeline components

State at 23:59

Source vs target row counts

Checked every 15 minutes. The 3 inventory rows in flight matched at 00:01.

Schema changes today

New columns land in RAW on their own

Latest change events

The last ten changes committed on Oct 6

How it works

Log-based capture: the source database never sees an extra query.

  1. 01 · Source

    PostgreSQL 16

    The order service's database. Logical replication with the pgoutput plugin; one publication covers the 12 tables.

  2. 02 · Capture

    Debezium on Kafka Connect

    Takes an initial snapshot, then reads the write-ahead log and emits every insert, update and delete with its LSN.

  3. 03 · Transport

    Apache Kafka

    One topic per table, keyed by primary key so changes to a row stay in order. Each event carries the row before and after.

  4. 04 · Normalize

    Apache Flink

    Reads all twelve topics and keeps what the merge needs: key, operation, LSN, commit time and the row, written exactly once.

  5. 05 · Land

    Snowpipe Streaming

    The Snowflake Kafka connector appends every change to RAW.CHANGES within seconds.

  6. 06 · Merge

    Snowflake Dynamic Tables

    One per source table: the newest version of each row, deleted rows removed, refreshed every minute.

  • No load on the source. Debezium reads the WAL instead of polling tables, so the order service sees no extra queries. A heartbeat keeps the replication slot moving when tables are quiet.
  • Order per key. Topics are partitioned by primary key and every change carries its LSN, so the merge always keeps the newest version, even if a batch arrives late.
  • Deletes are real. A delete arrives with op = 'd' and the merge removes the row, so Snowflake never keeps rows the source has deleted.
  • Trust, then verify. Every 15 minutes a job compares row counts and a hash of key columns between PostgreSQL and Snowflake. That feeds the reconciliation table.

Data model

The same order row at three points: the Debezium event in Kafka, the change row in Snowflake, and the merged table.

pg.public.ordersKafka topic · Debezium event (JSON)
before / afterstruct
Row before and after the change; before is null for inserts
opstring
c insert, u update, d delete, r snapshot read
source.lsnbigint
Position in the write-ahead log
source.ts_msbigint
Commit time in PostgreSQL
ts_msbigint
When Debezium processed the change
RAW.CHANGESSnowflake table · append-only, all 12 tables
record_content:table_name / pkvariant
Source table and primary key
record_content:op / is_deletedvariant
What happened to the row
record_content:lsnvariant
Orders the changes to one row
record_content:committed_msvariant
Commit time in PostgreSQL, epoch ms
record_content:row_jsonvariant
The whole row as JSON, so new columns arrive automatically
record_metadatavariant
Kafka topic, partition, offset and connector push time
CORE.ORDERSDynamic table · target lag 1 minute
order_idnumber
One row per live order
customer_idnumber
Customer
statusvarchar
Latest status
total_usdnumber(12,2)
Order total
updated_at / lsntimestamp_ltz / number
Version of the row that is current

Key code

The Debezium connector, the Flink SQL that normalizes changes, and the Snowflake merge.

Kafka Connect configuration for the Debezium PostgreSQL source connector. A heartbeat keeps the replication slot advancing on quiet tables.

{
  "name": "debezium-orders-pg",
  "config": {
    "connector.class": "io.debezium.connector.postgresql.PostgresConnector",
    "tasks.max": "1",
    "database.hostname": "orders-db.internal",
    "database.port": "5432",
    "database.user": "debezium",
    "database.password": "${secrets:orders-db/debezium-password}",
    "database.dbname": "orders",
    "topic.prefix": "pg",
    "plugin.name": "pgoutput",
    "slot.name": "debezium_orders",
    "publication.name": "dbz_orders",
    "publication.autocreate.mode": "disabled",
    "table.include.list": "public.orders,public.order_items,public.payments,public.shipments,public.inventory,public.prices,public.customers,public.addresses,public.returns,public.promotions,public.products,public.carriers",
    "snapshot.mode": "initial",
    "heartbeat.interval.ms": "10000",
    "decimal.handling.mode": "string",
    "time.precision.mode": "connect",
    "tombstones.on.delete": "false",
    "key.converter": "org.apache.kafka.connect.json.JsonConverter",
    "key.converter.schemas.enable": "false",
    "value.converter": "org.apache.kafka.connect.json.JsonConverter",
    "value.converter.schemas.enable": "false"
  }
}

Engineering notes

The 2 a.m. bulk update

A nightly job rewrote 1.2 million prices in one transaction. Throughput peaked near 1,400 events a second, p95 lag reached 7.9 s and drained within 15 minutes. The SLA held.

A restart nobody noticed

At 14:32 the Debezium task restarted after a network blip. It resumed from the last LSN it had confirmed to PostgreSQL, so nothing was lost and the merge ignored the few replayed events.

Schema changes without a deploy

Each change carries the whole row as JSON, so promo_code, a wider carrier_code and marketing_opt_in reached RAW.CHANGES the moment they existed in PostgreSQL. Exposing promo_code in CORE.ORDERS took one line.

The WAL stays small

The replication slot holds about 200 MB of write-ahead log. An alert fires at 5 GB, long before a stuck consumer could fill the source database's disk.

About the data. Every number on this page is sample data from a seeded generator written for this project. It is shaped to behave like a real workload, with daily peaks, late events and the odd incident, but it does not come from any employer or client. Download it as change_events_5min.csv or read data.json.