Implementing SCD Type 2 with dbt Snapshots
Capture historical dimension changes with dbt snapshots configured for SCD2 semantics.
In plain words
Stable, unique natural key. Next, deterministic check columns or updated_at. Finally, retain, hard-delete marker, or stale policy.
Think of it like…
A snapshot is a dated archive shelf: unchanged folders keep their open validity, changed folders receive a closed old edition and a new open edition.
Where you’ve seen it
{% snapshot customer_snapshot %} config(target_schema='history', unique_key='customer_id', strategy='check', check_cols=['segment','region']) {% endsnapshot %} creates dbt_valid_from/to history on observed changes.
Words you’ll hear
- Snapshots
- A dbt materialization designed to preserve historical versions when configured source records change.
- SCD2
- A historical dimension where an attribute change creates a new surrogate/effective-dated row rather than overwriting prior meaning.
- History
- The source-to-snapshot record sequence retained through versions, current flags, and effective validity dates.
- Operational boundary
- Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile
updated_atrecreates every version, and duplicate natural keys fail to resolve one current row. Give snapshots a stable unique key and change policy (checkortimestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.
Visual diagram
Theory & deep dive
Snapshots: the working mechanism
dbt snapshots compare a business unique key and selected columns or an updated timestamp, then close a prior row and insert a new valid-time record for SCD Type 2 history.
Stable, unique natural key. Deterministic check columns or updated_at, then non-overlapping dbt validity fields; this exposes the state governed by History.
{% snapshot customer_snapshot %} config(target_schema='history', unique_key='customer_id', strategy='check', check_cols=['segment','region']) {% endsnapshot %} creates dbt_valid_from/to history on observed changes.
Implementing SCD Type 2 with dbt Snapshots: failure mode and production response
Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile updated_at recreates every version, and duplicate natural keys fail to resolve one current row.
Give snapshots a stable unique key and change policy (check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally. Track Snapshot cadence as "short enough to observe required state transitions", and make Open versions visible to the dataset owner.
check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.How it works, step by step
Step 1 of 41. Select identity
Stable, unique natural key. dbt snapshots compare a business unique key and selected columns or an updated timestamp, then close a prior row and insert a new valid-time record for SCD Type 2 history.
- Snapshot cadence
- short enough to observe required state transitions
- SCD2
- A historical dimension where an attribute change creates a new surrogate/effective-dated row rather than overwriting prior meaning.
When to use it & trade-offs
What it is
dbt snapshots compare a business unique key and selected columns or an updated timestamp, then close a prior row and insert a new valid-time record for SCD Type 2 history. {% snapshot customer_snapshot %} config(target_schema='history', unique_key='customer_id', strategy='check', check_cols=['segment','region']) {% endsnapshot %} creates dbt_valid_from/to history on observed changes.
Why it matters: Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile updated_at recreates every version, and duplicate natural keys fail to resolve one current row. Give snapshots a stable unique key and change policy (check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.
When to reach for it
- Capture historical dimension changes with dbt snapshots configured for SCD2 semantics.
- Choose Snapshots because it provides this capability: A dbt materialization designed to preserve historical versions when configured source records change.
- Keep evidence for Snapshot cadence at "short enough to observe required state transitions" so the last production stage-Handle deletes - retain, hard-delete marker, or stale policy-remains testable.
When to avoid or be careful
- Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile
updated_atrecreates every version, and duplicate natural keys fail to resolve one current row. - Do not conflate SCD2 with Snapshots. The second concept contributes different behavior: A historical dimension where an attribute change creates a new surrogate/effective-dated row rather than overwriting prior meaning.
- Release only after this guardrail is in place: Give snapshots a stable unique key and change policy (
checkortimestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.
Snapshots / SCD2 / History - roles, limits, and operating cost
Strengths: A dbt materialization designed to preserve historical versions when configured source records change. dbt snapshots compare a business unique key and selected columns or an updated timestamp, then close a prior row and insert a new valid-time record for SCD Type 2 history.
Watch out: Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile updated_at recreates every version, and duplicate natural keys fail to resolve one current row.
Strengths: A historical dimension where an attribute change creates a new surrogate/effective-dated row rather than overwriting prior meaning. In this workflow it supports: Close/open - non-overlapping dbt validity fields.
Watch out: The operating cost is real: one current row per unique key unless deletion invalidates.
Strengths: The source-to-snapshot record sequence retained through versions, current flags, and effective validity dates. Its output can be checked with History overlap.
Watch out: Operating guardrail: Give snapshots a stable unique key and change policy (check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Snapshots, and how would Snapshot cadence reveal it?”
- “Why is SCD2 distinct from this behavior: the source-to-snapshot record sequence retained through versions, current flags, and effective validity dates?”
- Production clue: Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile updated_at recreates every version, and duplicate natural keys fail to resolve one current row.
- Design probe: Give snapshots a stable unique key and change policy (check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.
Benchmarks & rules of thumb
- Snapshot cadence
- short enough to observe required state transitions
- Open versions
- one current row per unique key unless deletion invalidates
- History overlap
- validity intervals never overlap for one natural key
Common mistakes & follow-ups
Common interview mistakes
- Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile
updated_atrecreates every version, and duplicate natural keys fail to resolve one current row. - Reporting success from a job exit code without checking Snapshot cadence and Open versions.
- Treating this example as complete without replay, rollback, or access boundaries:
{% snapshot customer_snapshot %} config(target_schema='history', unique_key='customer_id', strategy='check', check_cols=['segment','region']) {% endsnapshot %}createsdbt_valid_from/tohistory on observed changes.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A dbt materialization designed to preserve historical versions when configured source records change.
- What does the recovery plan do when this occurs? Snapshots over a source that already deletes missing rows cannot distinguish no-change from disappearance, a volatile
updated_atrecreates every version, and duplicate natural keys fail to resolve one current row. - When is Snapshots insufficient for Open versions, and what does adding SCD2 cost in Snapshot cadence?
check or timestamp), run at a cadence smaller than the required history granularity, reject duplicates upstream, and use hard-delete/invalidate policy intentionally.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Implementing SCD Type 2 with dbt Snapshots with data engineer interview Q&A and hands-on practice.