Skip to the workspace
Pebellslearn by building
Ingestion and Pipelines  ›  Lesson

Idempotent Loads

Load Data 12 minA load is idempotent when running it again leaves the same result, achieved by replacing a partition in one transaction or merging on a unique key and pinning each run to its logical date.

data engineer / Idempotent Loads

Explore each scene at your own pace.

01 · The rule

The rule

Idempotent Loads

At 02:10 PedalCity's nightly load failed halfway through writing yesterday's rides. The scheduler retried it automatically, and the retry succeeded. The dashboard then showed 61,000 rides for a day that had 40,000. Pipelines will be rerun, whether by retries, by backfills or by a tired engineer. They must be safe to rerun.

The rule

Running a load twice must leave the same result as running it once.

This property is called idempotency. A plain INSERT is not idempotent: every rerun appends another copy. Two patterns make loads safe.

1. Replace the whole partition. Delete the window's rows and insert them again, inside one transaction so nobody ever sees a half-finished state:

BEGIN;
DELETE FROM fact_rides WHERE ride_date = '2026-09-24';
INSERT INTO fact_rides
SELECT ... FROM stg_rides WHERE ride_date = '2026-09-24';
COMMIT;

Run it once or five times, and 24 September always ends with exactly one copy of each ride.

2. Merge on a key. Match incoming rows to existing rows on a unique key such as ride_id. Update the rows that match and insert the ones that do not:

MERGE INTO fact_rides AS t
USING stg_rides AS s ON t.ride_id = s.ride_id
WHEN MATCHED THEN UPDATE SET fare = s.fare, ended_at = s.ended_at
WHEN NOT MATCHED THEN INSERT (ride_id, fare, ended_at) VALUES (s.ride_id, s.fare, s.ended_at);

One more rule: a load must depend only on its window, never on the clock. A job that filters on "yesterday" computed from now() loads a different day if it is rerun a day later. Pass the run's logical date (the day being processed) into the job instead.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.