CASE STUDIES LIST/ HOME
← Case #18#19 / 25Case #20 →
CASE STUDY #19Phase 5: Cloud & OrchestrationCLASSIFICATION: ACTUAL

What Happens When an Airflow Task Fails Halfway Through?

Idempotent Pipeline Design and Dynamic Partition Overwrite Mechanics

Apache AirflowPySparkDelta LakePostgreSQL

1. The Problem

When an Airflow task crashed halfway through writing a 1M row batch to target storage, retrying the task appended duplicate records, creating 400,000 duplicate rows.

2. What I Initially Thought

"I thought Airflow's built-in task retries (`retries=3`) automatically handled failures safely. But Airflow retries execute the exact same python code without clearing partial writes."

3. What I Learned

Pipelines must be idempotent: executing a pipeline N times with the same input must produce the EXACT same result as running it once. Use atomic partition overwrites instead of appends.

Pipeline IdempotencyDynamic Partition OverwriteAtomic Output WritesRetries without Side-Effects

4. What I Built

Idempotent write module in PySpark using `spark.sql.sources.partitionOverwriteMode = dynamic` to overwrite target partitions atomically during task retries.

# Configure Spark for Idempotent Dynamic Partition Overwrite
spark.conf.set("spark.sql.sources.partitionOverwriteMode", "dynamic")

# Overwrites ONLY the target date partition atomically -> No duplicates on retry!
df.write \
  .mode("overwrite") \
  .partitionBy("process_date") \
  .parquet("abfss://silver/tasks/")

5. The Experiment

BEFORE

Task failure during write appended 400,000 duplicate rows on Airflow retry.

CHANGE APPLIED

Configured PySpark `partitionOverwriteMode=dynamic` for atomic partition replacement.

AFTER RESULT

Task retries execute safely 100% of the time with zero duplicate records created.

6. What Went Wrong

Used standard `mode('overwrite')` without dynamic partition mode, which wiped out the ENTIRE table directory instead of the specific date partition.

7. Engineering Decision & Trade-offs

Mandated dynamic partition overwrites or Delta Lake MERGE INTO statements as mandatory standard for all write operations.

8. What I Would Do Differently in Production

Ensure Airflow tasks pass execution date parameters (`{{ ds }}`) down to transformation scripts to maintain deterministic partition targets.

Questions I Can Now Answer Confidently in an Interview:

  • What does idempotency mean in the context of data engineering pipelines?
  • How does dynamic partition overwrite differ from static table overwrite in Apache Spark?
  • How do you design Airflow DAGs that can be safely backfilled or retried without duplicate data?

Expected / Verified Evidence

•Airflow DAG retry log showing zero duplicate row creation
•PySpark idempotent write script (pipelines/silver/idempotent_writer.py)
•Row count audit validation SQL query output
BACK TO ALL CASE STUDIESNEXT: CASE #20 (Hardcoding Credentials in Code Is a Security Disaster)