Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Before shipping any load, run this thought experiment: "If this runs twice in a row, what changes?" If the answer is anything other than "nothing", make it replace a partition or merge on a key, and pin it to its logical date.

Making the ride load safeStep 1 of 4
  1. Spot the danger

    The job does a plain INSERT of yesterday's rides. Running it twice doubles them.

Drills

Answer fast — trust the rule.

quick drill

A plain INSERT of 40,000 rides is run twice by mistake. How many rows does that day end up with?

quick drill

Why wrap the DELETE and INSERT of a partition in one transaction?

quick drill

Which load is idempotent?

quick drill

A job loads "yesterday" using now(). It fails on 25 September and is rerun on 27 September. Which day does the rerun load?

Remember

  • Running a load twice must leave the same result as running it once.
  • Replace a partition in one transaction, or merge on a unique key.
  • Pin every run to its logical date, never to now().

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFix the double count

Explain why PedalCity's dashboard showed 61,000 rides for a 40,000-ride day after an automatic retry, and rewrite the load's approach so that retries and backfills are safe.