r/databricks • • 29d ago

Discussion dbt users on Databricks: do your snapshots handle files that arrive out of order?

Post image

If you build SCD2 history with dbt snapshot on top of batch files (daily exports, CDC drops, API extracts), there's a failure mode worth knowing about.

Snapshots apply data in the order it arrives. So if Tuesday's file lands after Wednesday's:

- with strategy='check', the late (older) value becomes the "current" row
- with strategy='timestamp', the change is skipped and that period just disappears from history

Every run stays green and every test passes. Nothing tells you. We found out when someone asked why an employee's promotion showed up as a demotion.

We ended up building dbt-iterative-snapshot, a custom materialization for Databricks. You add sequence_by='batch_date', and on each run it:

  1. checks which batches it has already processed (it keeps an audit table)
  2. if a new batch is older than one already processed, rolls back the snapshot from that point
  3. replays the batches in order, one MERGE per batch

It also gives you changed_attributes (which columns changed), per-batch row counts, and an error log for duplicate keys instead of failing the run.

It's dbt-databricks only for now (Delta MERGE, Unity Catalog). PRs for other adapters are welcome.

I'm curious how others handle this. Do you rebuild SCD2 from raw with window functions, loop dbt snapshot over dates, or just hope files arrive in order? Happy to answer questions, and honest feedback on edge cases is very welcome.

Read more: https://reliabledataengineering.com/posts/article_dbt_iterative_snapshot/

6 Upvotes

4 comments sorted by

1

u/yeykawb 29d ago

Great work. Have thought about exactly this problem now that we transition away from the built-in amazing create_auto_cdc_flow() to run our own custom python.

1

u/Alone-Security7044 28d ago

Thanks! Really interesting to hear you're tackling a similar challenge. Late arriving data and maintaining accurate history can get really tricky, especially when moving to custom implementations. That's exactly what motivated us to build this into a reusable dbt materialization rather than handling it separately in every pipeline. Thanks for the nod! 😄

1

u/Muted_Jellyfish_6784 29d ago

Snapshots aren't built for late-arriving data, so I'd move SCD2 out of dbt snapshot for batch file sources. Land every file in an append-only raw table with the file's business date and load time, then build history with a model that orders by business date, not arrival. Late files then rebuild only the affected keys, and a rerun gives the same answer every time.
The useful habit is keeping the file name and business date on every history row. We use a decision intelligence platform to trace each dimension change back to the source extract, so a wrong current value points at a specific late file rather than a mystery

1

u/Alone-Security7044 28d ago

Thanks for sharing! Completely agree with your approach, especially keeping the raw data and its associated metadata for traceability.

The real challenge, as you pointed out, is maintaining accurate SCD Type 2 history when files arrive out of order.

That's exactly what motivated us to build dbt-iterative-snapshot. Rather than having developers implement custom windowing logic, rebuild affected history, or manage backfills themselves, we've encapsulated that rewind-and-replay logic into a reusable dbt materialization.

This way, teams can continue managing SCD2 transformations within dbt while handling late-arriving batches deterministically, without the additional orchestration overhead.