0% complete

Delta Lake writes and maintenance · Lesson 2 of 3

MERGE upserts and idempotency

Use keys and deterministic source rows to update target tables safely.

Lesson

MERGE can match source rows to target rows and apply conditional updates, inserts, or deletes. It is useful for upserts when records have a stable business key. Before merging, reduce the source to a deterministic row per key; multiple source matches for one target can make the operation ambiguous or fail. Define how a newer record is identified, especially when input can arrive late or be replayed. Idempotency means rerunning the same batch does not create additional unintended effects. Use stable keys, ordered change records, and a repeatable batch boundary. Test duplicates and retries, and ensure delete semantics are explicit. Practice task: explain why a source deduplication step should happen before a MERGE with multiple changes per customer. Applying the pattern: Deduplicate by key and source sequence, merge with explicit conditions, and replay the same batch in a test to confirm no duplicate state. Checks and failure modes: A MERGE statement does not by itself solve duplicate source keys, bad ordering, or an incorrect business key. Before production, test both a representative input and a boundary case. Confirm the expected row grain, permissions, recovery behavior, and observable output for the task. Record the configuration needed to reproduce the run and compare its result with the agreed data contract. Practice task: What should a pipeline do before merging multiple changes per key?

Visual guide

An idempotent upsert flow

  1. Step 01 Stage

    Load a bounded change batch.

  2. Step 02 Deduplicate

    Choose ordered changes per key.

  3. Step 03 Merge

    Apply update, insert, or delete rules.

  4. Step 04 Replay test

    Verify retries preserve the same target state.

Key points

  • Deduplicate by key and source sequence, merge with explicit conditions, and replay the same batch in a test to confirm no duplicate state.
  • A MERGE statement does not by itself solve duplicate source keys, bad ordering, or an incorrect business key.
  • Re-run the included practice scenario after changing source data or transformation logic.

Check your understanding

Practice

What should a pipeline do before merging multiple changes per key?

Your learning progress

Finish this lesson