Batch · Open reference architecture

Change data capture with SCD type 2

AUTO CDC in Spark Declarative Pipelines, from a Kafka change feed into Apache Iceberg

Keep the current state and every past version of each row, without hand-written MERGE logic.

Laptop (Docker)Updated 2026-11-21

In one paragraph

Change data capture (CDC) with slowly changing dimensions type 2 turns a feed of row-level changes from an operational system into a table that holds both the current state and the full history of each row. In open source Apache Spark 4.3, an AUTO CDC flow in Spark Declarative Pipelines reads the change feed, orders events by a sequence column, applies inserts, updates and deletes by key, and stores the result as SCD type 2: a change to a tracked column closes the old version and opens a new one, while changes to untracked columns update the current version in place. The target must support row-level operations, which Apache Iceberg provides.

Batch

AUTO CDC in Spark Declarative Pipelines, from a Kafka change feed into Apache Iceberg

Sources

Ingestion

Open lakehouse platform

Engine access

Execution

Data foundation

Serving

Teams

Lit boxes are part of this architecture, with the versions its reference repo pins. Switch to Full platform to see every capability. Select any box or team for details. marks a swappable choice.

When to use it

  • An operational database or service emits change events and you need its tables in the lakehouse.
  • Auditors or analysts ask what a row looked like at a past point in time.
  • You want to stop maintaining hand-written MERGE statements for every replicated table.

When to pick something else

  • Targets without row-level operations: AUTO CDC needs a DSv2 table that supports them.
  • Spark releases before 4.3 for this exact flow; the reference demo is verified on Spark 4.3.0.

Walkthrough

How data moves through it

  1. 1

    Changes are published

    Each insert, update or delete arrives on a Kafka topic as one JSON event with a key, a sequence number and an operation.

  2. 2

    Read the feed

    A Python table in the pipeline reads the topic with Structured Streaming and parses each event.

  3. 3

    Apply by key, in order

    The AUTO CDC flow groups events by key and orders them by the sequence column, so late or out-of-order events land correctly.

  4. 4

    Track history

    A change to a tracked column (price or station in the demo) closes the current version and opens a new one; a change to another column updates the current version in place; a delete closes the row's window.

  5. 5

    Query any point in time

    The Iceberg table holds every version with its validity window, so you can read the current state or the state at any past sequence.

Bill of materials

Components and versions

Versions are the ones pinned in the reference repositories below.
CapabilityComponentRole in this architectureVersionLicense
SourcesSource systemsOperational database or serviceEmits one event per insert, update or delete
IngestionEvent streamingApache KafkaThe change feed topic (menu-changes in the demo)apache/kafka 4.0.0 imageApache-2.0
IngestionChange data captureDebeziumTurns database transaction logs into change eventsApache-2.0
IngestionChange data captureAUTO CDC (Spark Declarative Pipelines)KEYS, SEQUENCE BY, APPLY AS DELETE WHEN, STORED AS SCD TYPE 2Apache-2.0
ExecutionDeclarative pipelinesSpark Declarative PipelinesRuns the pipeline with spark-pipelines runpyspark==4.3.0Apache-2.0
Data foundationCatalog and credentialsIceberg hadoop catalogor Unity Catalog OSS, Apache Polaris, LakekeeperDeclared in the pipeline spec
Data foundationOpen table formatApache IcebergSCD type 2 target with row-level operationsSpark 4.3 runtime built from sourceApache-2.0
Data foundationObject storageLocal filesystemor Amazon S3, SeaweedFS, MinIO, RustFSIceberg warehouse directory in the demo

People and agents

Who works with it

Data engineers

Pipelines that are correct, idempotent and cheap to rerun.

Works with: Event streaming, Change data capture, Declarative pipelines, Open table format

AI agents

Tools with clear arguments, a sandbox to write into, fast feedback from dry runs, and version-pinned context.

Works with: Declarative pipelines

Platform engineers

A platform that is boring to run: deployable from code, observable, with identities for every person, service and agent.

Works with: Object storage, Catalog and credentials

Governance and security

Policies that hold in every engine and for every agent, lineage for audits, and least privilege by default.

Works with: Catalog and credentials

Run it

Reference code

lisancao/spark-demosLaptop (Docker)

Demo 16 (Spark 4.3 guide), scd2/ folder: a ghost-kitchen menu change feed on Kafka applied with AUTO CDC as SCD type 2 into Iceberg.

scd2/reset.sh       # Kafka up, an empty menu-changes topic, no table, no checkpoints
scd2/publish.sh scd2/incoming/batch-1.json && scd2/run.sh && scd2/history.sh
scd2/publish.sh scd2/incoming/batch-2.json && scd2/run.sh && scd2/history.sh
scd2/publish.sh scd2/incoming/batch-3.json && scd2/run.sh && scd2/history.sh
docker compose -f scd2/compose.yaml down -v   # when done

Caveats from the reference repos

  • Iceberg has not published a Spark 4.3 runtime (Maven Central stops at iceberg-spark-runtime-4.1); the demo builds iceberg-spark-runtime-4.3_2.13 from an Iceberg checkout with Spark 4.3 support.
  • AUTO CDC needs a target that implements DSv2 row-level operations.
  • The jars and spark.sql.extensions are static configs, so they are passed to spark-pipelines run, not set in the pipeline spec.
  • Pipeline storage must be an absolute URI.

Industry lenses

Where this architecture shows up

Each card opens the board with that industry's sources. Full industry pages: Banking.

Answers

Frequently asked questions

What is SCD type 2?
Slowly changing dimension type 2 keeps every version of a row: when a tracked attribute changes, the current version is closed and a new one is opened, so the table answers both what is true now and what was true at any earlier time.
Can open source Spark do CDC without hand-written MERGE?
Yes. Spark Declarative Pipelines in Spark 4.3 has AUTO CDC flows: you declare the keys, the sequence column, the delete condition and STORED AS SCD TYPE 1 or 2, and Spark applies the change feed.
How does AUTO CDC handle out-of-order events?
It orders events per key by the SEQUENCE BY column, so an event that arrives late is applied in sequence order rather than arrival order.