The naive rerun gave $4,100 in 16 rows. The idempotent load gave $2,050 in 8, and Oct 6 stayed untouched.
python3 --version.pip install "duckdb>=1.0"git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
pip install -r requirements.txt # or just this lesson: pip install "duckdb>=1.0"
cd data-engineering/15-idempotent-pipelines python3 src/load.py
from job import naive, crash_and_rerun
def idempotent(db, day, rows):
db.begin() # all or nothing
db.execute("DELETE FROM revenue "
"WHERE day = ?", [day])
naive(db, day, rows) # the same insert
db.commit()
crash_and_rerun(naive)
crash_and_rerun(idempotent)Your job crashed halfway. The rerun doubled the revenue. A naive load just adds rows. Rerun it, and the same eight orders land twice.
The fix: inside one transaction, delete the whole day first, then insert it fresh, and commit. Now crash and rerun both loads, on a warehouse that already holds Oct 6. Naive: $4,100 in sixteen rows. Idempotent: $2,050 in eight.
Oct 6 untouched. Reruns happen. Make every load safe to run again.
Read the lesson on GitHub →