Skip to content
Charan Kumar Reddy
Data & MLDistributed Systems / SDECompleteSynthetic data

LakeFlow

Replay-safe CDC lakehouse: PostgreSQL → Debezium → Kafka → Spark → Iceberg → Trino

Medallion-style CDC lakehouse: PostgreSQL changes flow through Debezium, Kafka and Spark Structured Streaming (5 s micro-batches) into bronze and silver Iceberg tables on MinIO, with LSN-guarded idempotent MERGEs, a DLQ, Trino reconciliation, Airflow maintenance and CI that runs the full stack. Source data is synthetic.

A read-only recording of a real CI end-to-end run; the full stack runs locally with Docker or in Codespaces.

Spark micro-batches into bronze and silver Iceberg tables
5 s
spark/lakeflow_stream/config.py
duplicate rows after a crash between the Iceberg and offset commits
0
tests/integration/test_data_plane.py
LakeFlow control plane overview: change events per second, end-to-end freshness, consumer lag and component health

The problem

Change data capture runs on at-least-once delivery, so redelivered, late or out-of-order row changes can duplicate or corrupt lakehouse tables.

The approach

Debezium streams PostgreSQL changes through Kafka into Spark Structured Streaming, which decodes them against a contract and applies LSN-guarded MERGEs into Iceberg; malformed records go to a dead-letter queue.

Architecture

  1. 01

    PostgreSQL CDC source

    PostgreSQL 16 logical replication (pgoutput, slot lakeflow_cdc) is read by Debezium 2.7 running on Kafka Connect, which publishes one change event per row change.

  2. 02

    Kafka topics as code

    Kafka 3.9 in KRaft mode with messages keyed by primary key and 3 partitions per CDC topic; topics are created from a config file with broker auto-creation disabled, plus a separate replay topic for DLQ replays.

  3. 03

    Contract decoder

    A single pure-Python decoder in libs/lakeflow_core validates each event against versioned JSON Schema 2020-12 contracts, HMAC-pseudonymizes or drops PII fields, and computes a content-derived event_id.

  4. 04

    Spark streaming sink

    Spark 3.5 Structured Streaming (foreachBatch) appends unseen events to Iceberg bronze, MERGEs the batch-latest change per key into silver guarded by source LSN, writes rejects to an Iceberg DLQ table, and upserts a per-batch commit record.

  5. 05

    Iceberg on MinIO

    Iceberg tables stored on MinIO and registered in an Iceberg REST catalog backed by PostgreSQL, so Spark and Trino read and write the same snapshots.

  6. 06

    Query and control plane

    Trino 476 serves queries; a FastAPI control plane and Next.js UI expose a per-event trace timeline, a recovery lab for fault injection, live quality checks, lineage and DLQ replay.

  7. 07

    Maintenance and monitoring

    Airflow 2.10 DAGs run Iceberg optimize, snapshot expiry, orphan-file removal, quality checks, DLQ replay and backfill through Trino; Prometheus alert rules and a Grafana dashboard cover the stream.

Key decisions

  • Treats delivery as at-least-once and makes the sink idempotent instead of claiming exactly-once: event_id is a SHA-256 over table, key, LSN, transaction id, operation and row images, so a redelivered event gets the same id and bronze only appends ids it has not seen (ADR-0003). The cost is that each batch reads the bronze partitions and silver keys it touches.
  • Applies a silver MERGE only when the incoming source LSN is greater than the stored one, and turns deletes into tombstone rows that keep their LSN, so a replayed or late older update cannot overwrite or resurrect a row. Consumers must filter on is_deleted, and correctness depends on a strictly increasing per-row ordering key.
  • Keys Kafka records by primary key with a fixed partition count and makes topic init fail if a topic's partitions differ from the config, because adding partitions remaps keys and breaks per-row ordering (ADR-0001). A single hot key cannot be parallelised, and raising throughput needs a planned repartition.
  • Lays out silver as bucket(4, pk) with merge-on-read and bronze as days(source_ts) with a Parquet bloom filter on event_id (ADR-0002). Micro-batches write small delete files cheaply, but reads pay the merge cost until scheduled compaction runs.
  • Enforces BACKWARD_TRANSITIVE contract compatibility at load time and in CI and sends violations to the DLQ with reason codes while the stream continues; one Python validator is shared by the Spark UDF, the API and the tests so they cannot disagree, at the cost of Python UDF serialization (ADR-0004).
  • Benchmarks only the running stack with a seeded workload and stores every raw sample with environment metadata, and the README deliberately publishes no numbers because they depend on hardware (ADR-0005).

Results

  • CI starts the data plane and the full stack in Docker Compose and runs the integration and end-to-end suites on pushes to main and on pull requests.
  • Integration tests assert that a crash injected between the Iceberg commit and the offset commit is replayed without duplicate rows, with the replay recorded in the batch commit table.
  • The public demo is a read-only recording of API responses captured during a CI end-to-end run and published to GitHub Pages, mirrored on Render.
Measured results and the files they come from
MetricValueSource
Spark micro-batch trigger5 sspark/lakeflow_stream/config.py
Duplicate rows after a crash between the Iceberg commit and the offset commit0 (asserted in CI)tests/integration/test_data_plane.py

How it is tested

  • 130 pytest functions plus 9 Playwright UI tests: unit tests for the decoder, contracts, delivery semantics and maintenance logic, FastAPI tests with fake dependencies, and PySpark sink tests run inside the Spark runtime image.
  • Integration tests run in CI against the Docker Compose data plane (PostgreSQL, Debezium, Kafka, Spark, Iceberg, Trino) with no mocks, covering redelivered events, out-of-order updates, malformed records, a crash after the Iceberg commit, a connector restart and contract drift.
  • A full-stack CI job adds Airflow DAG integrity checks, API, Airflow and observability end-to-end scenarios, a benchmark run against CI thresholds, and Playwright tests against the running UI.
  • Prometheus configuration and alert rules are unit-tested with promtool, and contracts are validated as JSON Schema 2020-12 in CI.
  • The Debezium incremental-snapshot backfill tests are marked xfail until CI proves them.

Limits, stated plainly

  • Single-node setup: Spark runs local[2], MinIO and Kafka are single nodes (replication factor 1), and there is no TLS, SSO or multi-tenant isolation.
  • No benchmark results are committed (benchmarks/results holds only .gitkeep), so there are no published throughput or freshness figures.
  • Debezium incremental-snapshot backfill is implemented but experimental; its end-to-end tests are expected failures.
  • The guarantee is an effectively-once outcome, not exactly-once delivery; Kafka Connect exactly-once source support is left as an option.
  • Roadmap items not yet done: soak tests under sustained load, Kubernetes deployment, TLS and OIDC, and OpenLineage events.

Stack

PostgreSQLDebeziumApache KafkaApache SparkApache IcebergTrinoFastAPINext.jsApache Airflow