This walk-through follows one night’s data load from the moment it breaks to the moment it is repaired, and uses it to explain a property that data engineers care about a great deal: idempotency. An operation is idempotent if running it twice leaves the system in the same state as running it once. Pipelines that have this property can be retried, rescheduled and backfilled without anyone holding their breath.
The setup
A grocery delivery company in Coimbatore loads yesterday’s orders into a reporting table every night at 2 am. Yesterday had 48,000 order lines. The job reads them from the source system and writes them into fact_orders, which feeds the morning sales dashboard that the regional head opens at 9.
Tonight the job dies at 30,000 rows because the source database restarted for maintenance. The scheduler is configured to retry once. It retries, and this time all 48,000 rows go through. Everything is green. The retry did what it was supposed to do.
What the dashboard shows at 9 am
The first attempt had already written 30,000 rows before dying. The retry then appended all 48,000 again. The table now holds 78,000 rows for a day that had 48,000 order lines, an overstatement of 62.5%. Revenue for the day looks like a record. The regional head congratulates the team. Two days later finance reconciles to the payment gateway and finds the gap. Trust takes a month to rebuild.
The root problem is that the write step was a plain append. Appending is not idempotent: each run adds to whatever the previous run left behind. The retry logic was right; the write was unsafe to repeat.
Fix one: replace the whole slice
The simplest repair is to treat each day as a unit. Before loading a given date, remove whatever rows already exist for that date, then insert the new ones, all inside one transaction where the platform supports it. Run it once: 48,000 rows. Run it after a partial failure: the 30,000 stragglers are cleared, then 48,000 go in. Run it five times: still 48,000.
Many warehouses make this cheap by partitioning the table by date and letting you overwrite one partition atomically. The key requirement is that the job’s output is fully determined by the date it was told to process, not by the time it happened to run. A job that says “load everything since I last ran” is much harder to make safe than one that says “load 14 November”.
Fix two: merge on a key
If you cannot isolate a clean slice, for example because late updates arrive for old orders, use an upsert, often called a merge. Each incoming row is matched to existing rows by a business key such as order ID plus line number. If the key exists, the row is updated; if not, it is inserted. Rerunning produces the same final rows because the second pass finds every key already present and rewrites identical values.
This only works if the key really is unique. If the source can legitimately send two lines with the same order ID and line number, a merge will either fail or silently collapse them. Confirm uniqueness in the source before trusting it, and know what your platform does when several incoming rows match one target row, because the behaviour differs between systems.
Fix three: stage, validate, swap
A third pattern writes the new data to a staging table first, checks it (row count matches the source, no null keys, totals close to expectations), and only then replaces the target’s slice from staging. A failure mid-way leaves the real table untouched. The dashboard keeps showing yesterday’s correct picture instead of half of today’s.
Backfills are just reruns across many days
Suppose that three weeks later the product team corrects the way discounts are computed and asks you to reload the last 90 days. If the pipeline is idempotent and takes the date as a parameter, a backfill is nothing more than running the same job 90 times with different dates, possibly in parallel. If it is not, a backfill becomes a delicate manual operation with hand-written deletes, which is exactly when people make mistakes.
Two cautions. First, a backfill reads source data as it is now, which may differ from what it was then. If the source overwrites records in place, the history you rebuild reflects today’s values. Second, downstream tables built from this one must be refreshed too, or they will disagree with it.
What breaks idempotency without anyone noticing
- Using the current timestamp as a value inside the data, so each run writes a different load time and cannot be compared with the previous output.
- Generating new random identifiers on every run, so a merge never finds a match.
- Sending emails or API calls from inside the job; the data may be repeatable but the side effects are not.
- Incrementing counters in place, such as adding the day’s total to a running balance row, instead of recomputing from the underlying events.
A cheap test catches most of these. In a test environment, run the job for one date, record row counts and a checksum of the totals, run it again for the same date, and compare. If anything changed, the job is not safe to retry. Do that once before you go live, and again after any change to the write step.
Figures and names in the examples are illustrative.
Leave a Reply