# AUTO CDC Replaces Your Hand-Written SCD Type 2 Merge, Not Your Judgment

> AUTO CDC removes the hand-written MERGE for slowly changing dimensions, but sequencing, tombstones and compute edition still decide whether it holds.

- Canonical: https://lakenaut.dev/blog/auto-cdc-replaces-your-hand-written-scd-type-2-merge-not-your-judgment-28bj/
- Author: Fernando De Nitto
- Published: 2026-11-18
- Kind: pattern
- Tags: Databricks, Data Engineering, Lakeflow, Delta Lake, Data Platform
- Concepts: [Change data capture with AUTO CDC](https://lakenaut.dev/concepts/pipelines-auto-cdc.md), [Upsert with MERGE INTO](https://lakenaut.dev/concepts/merge-upsert.md)
- Licence: CC BY-NC-SA 4.0

A dimension table that several gold tables and a couple of dashboards join against usually starts as a plain `MERGE INTO`: match on the key, `UPDATE SET *` when it exists, `INSERT *` when it doesn't. Then someone asks what a customer's plan tier was last quarter, and the same table grows a second job: close the previous version of a row, open a new one, and keep the validity dates straight through every case nobody drew on the whiteboard — two updates to the same key in one micro-batch, a delete that overtakes the insert it undoes, a backfill that replays history in a different order than it happened. The team that owns the table ends up maintaining exactly the deduplication-and-sequencing logic that every other team building a slowly changing dimension has already written once, and gotten subtly wrong once.

## The merge everyone writes first

A hand-rolled upsert for current state alone is a solved problem: a [`MERGE INTO`](https://lakenaut.dev/concepts/merge-upsert/) statement that matches on a key, updates on match, inserts otherwise, all inside one atomic commit. Databricks' own documentation on `MERGE` describes that same statement for ordinary upserts and for deduplicating an append-only log, and then says directly that for SCD Type 1 and Type 2 processing from a change feed, the recommended tool is AUTO CDC inside a Lakeflow pipeline, specifically so that out-of-order records are handled correctly — not a bigger `MERGE` statement ([docs.databricks.com/aws/en/delta/merge](https://docs.databricks.com/aws/en/delta/merge)).

The reason shows up the first time two change records for the same key land in one batch: `MERGE` only tolerates one source row matching a given target row. A second match on an update clause fails the statement outright, with no built-in tiebreaker for which of the two should win — you're expected to deduplicate the source first, typically with a window function that keeps the latest row per key by some sequence column. Do that correctly for current state and you still haven't solved Type 2, where "latest" also has to become a closed-and-reopened pair of rows, with validity columns you name and maintain yourself, consistent across every edge case a late-arriving event introduces. This is the design Databricks is naming when it says CDC logic built as custom `MERGE INTO` statements requires staging tables, window functions and sequencing assumptions that are "difficult to reason about and maintain as pipelines evolve" ([docs.databricks.com/aws/en/data-engineering/what-is-cdc](https://docs.databricks.com/aws/en/data-engineering/what-is-cdc)).

![Hand-written MERGE vs. AUTO CDC](https://lakenaut.dev/social/35b5e6cfd5c2bf73f01a1ed2d457d.png)
## What AUTO CDC declares instead

[AUTO CDC](https://lakenaut.dev/concepts/pipelines-auto-cdc/) (formerly APPLY CHANGES) turns that logic into a declaration inside a Lakeflow pipeline: keys, a sequencing column, and whether the target keeps current state (SCD Type 1) or full history (SCD Type 2). It needs serverless compute or the Pro or Advanced edition of Lakeflow pipelines — it isn't part of open-source Apache Spark Declarative Pipelines, so adopting it is a compute-edition decision as much as a code change.

```sql
CREATE OR REFRESH STREAMING TABLE main.silver.accounts_history;

CREATE FLOW accounts_cdc AS AUTO CDC INTO main.silver.accounts_history
FROM stream(main.bronze.accounts_cdf)
KEYS (account_id)
APPLY AS DELETE WHEN change_type = "DELETE"
SEQUENCE BY STRUCT(change_ts, change_seq)
COLUMNS * EXCEPT (change_type, change_ts, change_seq)
STORED AS SCD TYPE 2
TRACK HISTORY ON * EXCEPT (last_touched_at);
```

Four things happen here that a hand-written `MERGE` would need separate code for. Events are applied in the order `SEQUENCE BY` defines rather than arrival order, so an update stamped earlier than the current version is dropped instead of silently overwriting something newer. Closing and reopening rows for Type 2 is automatic, with the validity columns populated from the sequencing values. `TRACK HISTORY ON` lets a column such as a last-touched timestamp update the current row in place instead of minting a new version on every heartbeat. And the whole thing is declared once, as a flow, instead of re-derived from scratch inside a scheduled job.

## Where it still breaks in production

AUTO CDC removes the `MERGE`, not the judgment. A few places that still show up on-call:

![Where AUTO CDC still needs judgment](https://lakenaut.dev/social/35b5e6cfd5c2bf73f11a1ed2d457d.png)
The sequencing column has to be the order the source assigns to events, not the order they reach bronze. Ingestion time is tempting because it's always there, but it isn't the source's truth — a batch that lands late because of a retry gets sequenced as if it happened late, and a genuinely later change can be discarded by one that only arrived first. Databricks' own worked example shows exactly this shape: a later update with a higher sequence number is kept, and an earlier update that happens to arrive last is dropped — correct for the sequence number, confusing if you were assuming arrival order ([docs.databricks.com/aws/en/ldp/cdc](https://docs.databricks.com/aws/en/ldp/cdc)). A null in that column isn't handled gracefully either — it's unsupported, and nothing in the pipeline UI flags it until a run fails.

`APPLY AS TRUNCATE WHEN` only exists for SCD Type 1. A truncate event against a Type 2 target has no defined meaning — you can't clear a history table and keep its history — so a source that occasionally emits a full-reload signal needs a separate plan for the Type 2 copy of the same data.

Deletes against an SCD Type 2 target aren't gone from storage right away. The row is kept as a tombstone in the underlying Delta table so a late-arriving event for the same key can still be sequenced correctly against it, with a view filtering the tombstones out for ordinary queries. How long that tombstone survives is controlled by the `pipelines.cdc.tombstoneGCThresholdInSeconds` table property, and the default is worth checking against how late your source can realistically run rather than assumed ([docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-apply-changes-into](https://docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-apply-changes-into)).

And combining a periodic authoritative snapshot with a faster change feed on the same target — a common shape when a source gives both a nightly export and a near-real-time feed in between — only works for SCD Type 1. It needs matching key names and casing across every flow, identical data types between the snapshot version and the sequencing column, triggered rather than continuous pipeline mode, and neither `IGNORE NULL UPDATES` on the change-feed side nor expectations on the snapshot side. None of that extends to SCD Type 2 or bitemporal targets, so a design that assumes it can bolt a snapshot safety net onto a Type 2 table later has to be rethought now, not then.

## Operating it

AUTO CDC reports `num_upserted_rows` and `num_deleted_rows` on every pipeline run, and that's the first thing worth alerting on: a dimension flow that suddenly upserts far more or far fewer rows than its usual run is either catching up on a backlog or quietly dropping events, and the two look identical from the outside without that number. Watch it alongside ordinary pipeline run history, and separately watch whether the source's own change feed is healthy — AUTO CDC can only sequence what it receives, and a gap upstream shows up downstream as rows that simply stop changing, not as a failed run.

The target table also isn't a plain streaming table once AUTO CDC owns it. `INSERT`, `UPDATE`, `DELETE` and `MERGE` against it are allowed even while the pipeline runs, which is useful for a one-off correction but also means someone can write directly against a table your flow assumes it owns exclusively — worth a grant review, not only a pipeline review. And reading downstream from an AUTO CDC target has to go through its own change data feed rather than a plain streaming read, since the table is rewritten in place by the flow underneath any reader.

## What to do about it this week

Pull up whatever dimension table in your lakehouse is still kept current by a hand-written `MERGE`, and check two things before touching anything: what column that merge actually sorts or deduplicates on, and whether it's genuinely monotonic per key or just usually so. If it's the second, that's the exact design AUTO CDC was built to replace. Before migrating a Type 2 table specifically, check how your source signals a full reload and whether anything downstream has quietly started reading the table with `foreachBatch` instead of through its change feed — both are the kind of thing that breaks on the day you cut over, not before.

## Sources

- https://docs.databricks.com/aws/en/ldp/cdc
- https://docs.databricks.com/aws/en/data-engineering/what-is-cdc
- https://docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-apply-changes-into
- https://docs.databricks.com/aws/en/delta/merge
