Databricks Change Data Capture

Change Data Capture, often shortened to CDC, is a technique that tracks which rows in a table were inserted, updated, or deleted. Pipelines then move only those changes downstream instead of copying the entire table again. CDC saves time and computing cost, and it keeps reports close to real time.

The Shopping List Analogy

Imagine a family shopping list on the fridge. Every evening, one person could copy the whole list onto a fresh sheet. A smarter habit records only the changes: add eggs, remove bread, change milk to two liters. A short list of changes costs less effort and shows exactly what happened.

FULL COPY VS CHANGES ONLY
──────────────────────────────────────────────────────────
FULL COPY (every night):
  Source table: 50 million rows ──► copy all 50 million rows
  Time: long      Cost: high      Delay: hours

CDC (every few minutes):
  Source table: 50 million rows ──► 1,200 rows changed
                                    copy only those 1,200
  Time: short     Cost: low       Delay: minutes

Three Types of Changes

Every change falls into one of three groups. An insert adds a new row. An update modifies an existing row. A delete removes a row. A CDC feed labels each change so the target table can mirror the source exactly.

Delta Change Data Feed

Delta Lake includes a built-in feature named Change Data Feed. When you switch it on, the table records row-level changes next to its normal data. Readers then ask for the changes between two versions.

Turn On the Feed

ALTER TABLE main.shop.customers
SET TBLPROPERTIES (delta.enableChangeDataFeed = true);

The feed records changes made after you enable it. Earlier history does not appear.

Read the Changes

-- Changes from version 5 to version 8
SELECT * FROM table_changes('main.shop.customers', 5, 8);
spark.read.format("delta") \
  .option("readChangeFeed", "true") \
  .option("startingVersion", 5) \
  .table("main.shop.customers")

Extra Columns in the Feed

ColumnMeaning
_change_typeinsert, update_preimage, update_postimage, or delete
_commit_versionThe table version that holds the change
_commit_timestampThe time of the change

An update produces two rows. The preimage row shows the old values, and the postimage row shows the new values.

CHANGE FEED EXAMPLE
──────────────────────────────────────────────────────────
Customer 77 moves from Pune to Mumbai.

customer_id | city   | _change_type      | _commit_version
77          | Pune   | update_preimage   | 6
77          | Mumbai | update_postimage  | 6

Applying Changes with MERGE

A pipeline often receives a batch of changes from a source system. The MERGE command applies inserts, updates, and deletes to a target table in a single step.

MERGE INTO main.shop.customers_silver AS target
USING customer_changes AS source
ON target.customer_id = source.customer_id

WHEN MATCHED AND source.change_type = 'delete' THEN
  DELETE
WHEN MATCHED AND source.change_type = 'update' THEN
  UPDATE SET target.city = source.city, target.phone = source.phone
WHEN NOT MATCHED AND source.change_type != 'delete' THEN
  INSERT (customer_id, city, phone)
  VALUES (source.customer_id, source.city, source.phone);

Handling Out-of-Order Changes

Network delays sometimes deliver changes in the wrong order. An update could arrive before the insert it belongs to, or two updates could swap places. Pipelines solve this problem with a sequence column, such as a timestamp or a version number. The pipeline applies only the newest change for each key.

OUT-OF-ORDER EXAMPLE
──────────────────────────────────────────────────────────
Arrival order:     Update (time 10:05)   then   Update (time 10:01)
Wrong result:      the older update overwrites the newer one
Right result:      keep the change with the highest sequence value (10:05)

Delta Live Tables simplifies this work with the APPLY CHANGES feature. You declare the key, the sequence column, and the target table, and the pipeline handles ordering, updates, and deletes for you.

Slowly Changing Dimensions

Some reports need history, such as the cities where a customer lived over time. A Type 1 table overwrites old values and keeps only the latest state. A Type 2 table keeps every version of a row and adds start and end dates.

TYPE 2 HISTORY TABLE
──────────────────────────────────────────────────────────
customer_id | city   | start_date  | end_date    | is_current
77          | Pune   | 2024-01-10  | 2025-03-02  | false
77          | Mumbai | 2025-03-02  | null        | true

Example: Bank Branch Directory

A bank keeps a branch directory in an operations database. Branches open, close, and change phone numbers every week. The data team enables the change feed on the lakehouse copy and runs a job every ten minutes. The job reads new changes and updates a silver table, and dashboards always show the current branch list.

BRANCH DIRECTORY FLOW
──────────────────────────────────────────────────────────
Operations database ──► change records ──► bronze table (feed on)
                                                │
                                      job every 10 minutes
                                                ▼
                                    MERGE into silver table
                                                ▼
                                       Branch dashboard

Key Points

  • CDC moves only changed rows, which saves time and cost.
  • Every change is an insert, an update, or a delete.
  • Delta Change Data Feed records row-level changes when you enable the table property.
  • The table_changes function and the readChangeFeed option read those changes.
  • MERGE applies a batch of changes to a target table in one step.
  • A sequence column protects against out-of-order changes.
  • Type 2 history tables keep every past version of a row.