Chapter 6 of 12
Course Progress0 of 12 completed (0%)

Data Engineering · Chapter 6 · ~15 min · Intermediate

Orchestration & DAGs with Apache Airflow

Design resilient data workflow orchestration: DAG topology, TaskFlow API, schedule intervals, backfilling, idempotency, sensor polling, SLA alerts, and worker execution pools.

  • Design acyclic execution dependencies using Bitshift operators (>>) and the TaskFlow API
  • Master execution dates, data intervals, logical dates, and deterministic backfilling
  • Implement smart sensors and deferrable operators to avoid worker slot starvation
  • Handle failure modes with retries, exponential backoff, dead-letter queues, and Slack callbacks

Orchestration & DAGs with Apache Airflow: learn it visually

Design resilient data workflow orchestration: DAG topology, TaskFlow API, schedule intervals, backfilling, idempotency, sensor polling, SLA alerts, and worker execution pools.

Orchestration & DAGs with Apache AirflowDesign resilient data workflow orchestration: DAG topology, TaskFlow API, schedule intervals, backfilling, idempotency, sensor polling, SLA alerts, and worker execution pools.UNDERSTANDOrchestrationFOLLOW THE FLOWDAGs Apache AirflowAPPLY ITDesign resilientdataOrchestration & DAGs with Apache AirflowDesign resilient data workflow orchestration: DAG topology, TaskFlow API, schedule intervals, backfilling, idempotency, sensor polling, SLA alerts, and worker execution pools.UNDERSTANDOrchestrationFOLLOW THE FLOWDAGs Apache AirflowAPPLY ITDesign resilientdata

1Everyday Intuition: The Intelligent Air Traffic Controller

In early programming, scheduling data tasks with Linux cron seems simple: run Script A at 2:00 AM, Script B at 2:30 AM, and Script C at 3:00 AM.

However, cron is a blind alarm clock. It has no concept of state, dependencies, or failure. If Script A takes 45 minutes instead of 20 due to heavy network traffic, cron blindly launches Script B while Script A is still writing, corrupting data. If Script A crashes, Script B executes on missing data.

The Cron Danger

Uncoordinated Time-Based Firing

Zero state awareness. No built-in retry mechanisms, no centralized logging, and zero ability to backfill a missing month of historical data without manually editing cron tables.

The Airflow Standard

Directed Acyclic Graphs (DAGs)

An Airflow DAG guarantees causality: Task B only starts when Task A emits a successful exit status. If Task A fails, Airflow retries with exponential backoff and alerts on-call engineers.

Concept Check

Why does enterprise data engineering replace simple cron jobs with DAG orchestrators like Airflow?

2Under the Hood: Airflow Architecture & Anti-Patterns

An Apache Airflow cluster comprises five core components:

1. Scheduler

Evaluates DAG definitions, computes data intervals, checks dependency states, and submits ready tasks to the queue.

2. Workers (Celery / Kubernetes)

Execute the actual python functions, bash scripts, or container jobs. Workers stream logs in real-time to object storage.

3. Metadata Database (PostgreSQL)

Stores DAG metadata, task instance run states, execution dates, variables, and connection secrets.

4. Triggerer (Async Deferrable Daemon)

An event-loop engine that monitors long-running external tasks (e.g. waiting for a Snowflake warehouse or S3 file) without holding worker slots.

Wrong idea: Filter pipeline data using datetime.now() or CURRENT_DATE

Backfilling 2024-10-01 → Runs `WHERE date = datetime.now()` → Corrupts today!Non-deterministic execution · Impossible to reliably replay past dates

Writing queries like 'WHERE created_at = datetime.now()'. When a backfill is triggered for October 2024, the pipeline re-runs on today's data instead of historical partitions.

Real system: Filter using data_interval_start and data_interval_end

WHERE created_at >= '{{ data_interval_start }}' AND created_at < '{{ data_interval_end }}'Deterministic backfilling · Fully reproducible historical analytics

Airflow separates wall-clock execution time from the data interval boundaries. A backfill for 2024-10-01 injects that exact historical slice into your queries.

Concept Check

Why is executing database queries or HTTP requests in the top-level scope of an Airflow DAG file an anti-pattern?

3Hands-On: Test Airflow DAG Execution & Retry Logic

Trigger the DAG run below. Experience how parallel tasks branch, how transient API rate-limit errors enter the up_for_retry state, and how downstream tasks pause until retries succeed:

Airflow 2.9 · DAG: daily_ecom_pipeline
logical_date: 2026-11-20T00:00:00Z

Airflow DAG Execution & State Engine

Trace execution dependencies, simulate upstream rate-limit failures, and trigger automatic exponential backoff.

Source Stage
fetch_orders
none
Parallel Extraction
validate_schema
none
get_exchange_rates
none
Compute Marts
compute_marts
none
Notification
notify_slack
none
none running success up_for_retry failed
Concept Check

What technical guarantee is mandatory for an Airflow task to safely participate in automated historical backfills?

4Bug Hunt: Fix an Unstable Airflow DAG

A newly hired engineer deployed a DAG with critical stability bugs: top-level DB connections that hammer the scheduler, non-deterministic datetime.now() filters, and 0 retries. Fix all three issues:

Airflow Orchestration · DAG Optimization & Resilience
Python

🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.

Need a hint?
  • Move the psycopg2.connect call inside the task function extract_yesterdays_sales.
  • Replace datetime.now() with ds or data_interval_start parameter.
  • Add retries: 2 and a retry_delay in default_args.

5Recap

  • Apache Airflow replaces uncoordinated time-based cron scheduling with stateful Directed Acyclic Graphs (DAGs).
  • Data Intervals (data_interval_start and data_interval_end) guarantee deterministic historical backfilling without relying on wall-clock now().
  • Top-Level Code Discipline is crucial: never make network calls or query databases in global DAG script scope.
  • Deferrable Operators leverage the Triggerer daemon to release worker threads during long-running polls, preventing worker slot starvation.

Ready for Chapter 7? Mark this chapter as complete to proceed:

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us