The pipeline ran twice. Why did revenue double too?
Idempotency, retries, and backfills help recover from failure without creating a new problem.
Failure can happen after success.
A job writes 100 rows, then the connection drops before the scheduler receives the success signal. The scheduler retries. Blind append leaves 200 rows in the target even though the source had only 100 events. The business appears to have grown by one hundred percent. Congratulations, it was the retry that grew. Uncertainty about network operations is enough to cause this.
Idempotency means repeating the same operation does not change the final result once its intended effect has been applied. This is different from determinism: a computation may always produce the same 100 rows, while the write step still duplicates the table. Check both the calculation and its side effects.
Batch identity is not machine time.
Airflow recommends making tasks safe to rerun, reading specific partitions, and avoiding blind inserts when an upsert is appropriate. A batch for a particular interval needs inputs that can be identified again. Reading “the latest file” or using now() as a boundary means a retry processes a world that has already changed. The second attempt may not even be the same job.
Store the data interval, input version, and run_id. Do not confuse run_id with event_id. The first identifies a job attempt. The second identifies a business event. One event can pass through multiple runs without becoming multiple sales. This metadata matters when logs and tables disagree.
Upsert is not a magic spell.
PostgreSQL ON CONFLICT lets you choose an action when a key conflicts. The key still has to be right, and the update must account for versions. An older snapshot that arrives late should not casually overwrite a newer status. Deletion also needs a rule. A record missing from the next batch does not always mean it was deleted.
For a partition snapshot, one alternative is to write the full result to a staging area, validate it, then replace the partition atomically if the platform supports that. For events, deduplication on a stable identity may fit better. Choose based on the shape of the data instead of forcing everything into a long MERGE statement.
A backfill is a controlled revision.
Suppose a discount rule was wrong from Monday through Wednesday. State that range, the correction version, and which consumers need recalculation. Start with a small scope, compare keys and totals, then expand. Make sure the daily job and the backfill do not overwrite each other. Reprocessing all history without limits can drain capacity and revise reports before anyone is ready.
Keep the difference as review material: which partitions changed, how many rows, and why. If validation fails, keep the old result or restore a known version. A half-finished table is not a required price of progress.
Test the failure boundaries.
Run the same batch twice. Compare keys, row counts, and amounts. Then simulate failure after the write but before the checkpoint, late input, and two overlapping runs. Without boundary tests, “idempotent” is still just good intentions in a README.
Monitor recovery time, duplicates, and backfill cost. An exactly-once guarantee from one component does not automatically cover a database, email, and the API at the other end. External effects still need a request identity or reconciliation. The scheduler does not understand the full business semantics.
An example to inspect.
-- PostgreSQL, an illustration of state per identity
INSERT INTO order_items (order_id, line_id, amount_idr)
VALUES ('O-01', 1, 40000)
ON CONFLICT (order_id, line_id)
DO UPDATE SET amount_idr = EXCLUDED.amount_idr;
-- Production also needs rules for versions, deletes, and concurrency.
The job failed after the write but before its checkpoint. What is the most informative recovery test?
Replay the same input and interval, then check keys, count, total, and external effects. The result should not change without a business reason.
Keep these three things.
- Keep
event_id, interval, andrun_idseparate. - Test failure after a write.
- Prepare atomic publishing or reconciliation.
Sources, not slogans.
Primary references reviewed on 7 November 2026. The explanation, examples, and visual were prepared by RapiApp. The diagram simplifies the topic for learning. The article explains its assumptions and limits.
- Airflow: Best Practices
- PostgreSQL: INSERT and ON CONFLICT
Continue the foundations.
- Who is this row about? Get identity right before building the pipeline.
- The pipeline ran twice. Why did revenue double too?
- The data arrived late. That does not mean the event just happened.