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.
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.
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.
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.
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
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
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.
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 DAG Execution & State Engine
Trace execution dependencies, simulate upstream rate-limit failures, and trigger automatic exponential backoff.
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:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Move the
psycopg2.connectcall inside the task functionextract_yesterdays_sales. - Replace
datetime.now()withdsordata_interval_startparameter. - Add
retries: 2and aretry_delayindefault_args.
5Recap
- Apache Airflow replaces uncoordinated time-based cron scheduling with stateful Directed Acyclic Graphs (DAGs).
- Data Intervals (
data_interval_startanddata_interval_end) guarantee deterministic historical backfilling without relying on wall-clocknow(). - 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: