Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 108/ 160
06. Data Transformation (dbt) & Workflow Orchestration

Implementing SCD Type 2 with dbt Snapshots

Capture historical dimension changes with dbt snapshots configured for SCD2 semantics.

AdvancedSnapshots · SCD2 · History3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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_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.

Visual diagram

Loading interactive 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.

Implementing SCD Type 2 with dbt Snapshots in one concrete design

{% 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.

Key takeaway: 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

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Snapshots

1. 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.

stable, unique natural key
Key numbers at this step
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_at recreates 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 (check or timestamp), 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

Snapshots
Best for: A dbt materialization designed to preserve historical versions when configured source records change.

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.

SCD2
Best for: A historical dimension where an attribute change creates a new surrogate/effective-dated row rather than overwriting prior meaning.

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.

History
Best for: The source-to-snapshot record sequence retained through versions, current flags, and effective validity dates.

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_at recreates 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 %} creates dbt_valid_from/to history 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_at recreates 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?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
dbt Packages (dbt_utils, dbt_expectations, codegen)
Next topic
Built-in & Custom Data Tests, Sources & Freshness Checks