Apache Airflow Basics — Data Engineering

Airflow runs pipelines on a schedule: a DAG (Directed Acyclic Graph) is tasks and their order with no loops; operators perform actions, sensors wait for a…

pipeline.py from the previous lesson is safe to run again. But someone still has to run it every night, run it again when it fails, and remember which dates are already loaded. That's the job of an orchestrator such as Apache Airflow: a program that runs your pipeline's steps on a schedule, in the right order, retries what fails, and records every run. It works much like a railway station. The timetable says the parcel train leaves every day at 00:00. The station master watches the clock, sends the train off, holds the next one if the track isn't clear yet, and writes every departure in the log book. The train that leaves on 2 October at 00:00 carries the parcels of 1 October, so its ticket says 1 October. The timetable is the DAG, the station master is the scheduler, one departure is one run.

DAGs, tasks, operators, the scheduler

The latest Airflow as of 11 October 2026 is 3.3.2 (released 17 September 2026). After it came 3.4.0 (6 November 2026), which needs Python 3.11 or later. For this lesson Airflow was installed in a Python 3.13 virtual environment with pip install "apache-airflow==3.3.2" --constraint ... (the official constraints file for that version), about 185 MB. Every output below comes from that install.

Bookshop's first DAG

This DAG runs pipeline.py from the previous lesson, one date per run. It's written with the TaskFlow API: plain Python functions with @dag and @task decorators.

from datetime import date, timedelta

import pendulum
from airflow.sdk import CronDataIntervalTimetable, dag, task

import pipeline  # pipeline.py from the previous lesson, in the same folder as this file

LONDON = "Europe/London"


@dag(
    schedule=CronDataIntervalTimetable("0 0 * * *", timezone=LONDON),
    start_date=pendulum.datetime(2026, 9, 28, tz=LONDON),
    catchup=True,
    max_active_runs=1,
    default_args={"retries": 2, "retry_delay": timedelta(minutes=5)},
)
def bookshop_daily():
    @task
    def pick_date(data_interval_start=None) -> str:
        return data_interval_start.in_timezone(LONDON).date().isoformat()

    @task
    def load_date(order_date: str) -> int:
        day = date.fromisoformat(order_date)
        clean = pipeline.transform(pipeline.extract(day))
        pipeline.load(clean, day)
        return len(clean)

    @task
    def report(order_date: str, row_count: int) -> None:
        print(f"sales {order_date}: {row_count} rows")

    order_date = pick_date()
    report(order_date, load_date(order_date))


bookshop_daily()

Run once without the scheduler through airflow dags test bookshop_daily 2026-10-02T00:00:00+01:00 (a manual run processes the last complete interval before that time, which is 1 October), the load_date task logs the same pipeline.py lines as the previous lesson: extract 2026-10-01, rows: 3, doubled rows dropped: 1, load 2026-10-01, rows: 2. Then report prints sales 2026-10-01: 2 rows.

XCom: for small values only

A value a task returns is stored by Airflow as an XCom (cross-communication) in its metadata database, then handed to the tasks that need it. In that run, Airflow's log records Done. Returned value was: 2026-10-01 for pick_date and Done. Returned value was: 2 for load_date: two XComs, a date and a number.

XCom is designed for small values: a date, a row count, a file path. The big data stays in the warehouse or in files, and the next task reads it from there. A DataFrame of millions of rows passed through XCom bloats Airflow's metadata database.

Data intervals and logical dates

Every scheduled run has four times:

Computed by Airflow 3.3.2 for the DAG above (all times London time):

Run starts logical_date Data interval ds
29 Sep 00:00 28 Sep 00:00 28 Sep 00:00 to 29 Sep 00:00 2026-09-27
2 Oct 00:00 1 Oct 00:00 1 Oct 00:00 to 2 Oct 00:00 2026-09-30
11 Oct 00:00 10 Oct 00:00 10 Oct 00:00 to 11 Oct 00:00 2026-10-09

The run labelled 1 October starts on 2 October at 00:00, after its day is over, because only then do all of 1 October's orders exist, like the parcel train. A label one day behind the time it runs is Airflow's classic confusion.

Two things make it slipperier:

Catchup and backfill

The DAG above was switched on at 21:00 London time on 11 October, though its start_date is 28 September.

Old dates can be filled at any time with a backfill:

airflow backfill create --dag-id bookshop_daily \
  --from-date 2026-09-28T00:00:00+01:00 --to-date 2026-10-09T00:00:00+01:00

That gives 12 runs, logical dates 28 September to 9 October, which the scheduler runs and the UI shows. Write the time and the zone: a bare 2026-09-28 is read as midnight UTC (01:00 London time), and --dry-run shows the run for 28 September London time being skipped. Backfills and catchup are safe because the load in pipeline.py is idempotent: a date loaded again is deleted first.

Retries and states

default_args={"retries": 2, "retry_delay": timedelta(minutes=5)} applies to every task: a failed one is tried again 2 times (3 attempts in all) with 5 minutes between. While it waits, its state is up_for_retry; if it still fails, failed, and the tasks after it become upstream_failed without running. Careful, retries stack: pipeline.extract itself tries to connect 3 times, so a source that is down for good is tried 3 × 3 = 9 times before the run is declared failed.

💡 What's new in Airflow 3

Airflow 3.0 came out in April 2025. The changes a DAG author feels, according to the official "Upgrading to Airflow 3" guide:

⚠️ What makes a DAG load the wrong day, or weighs Airflow down

TL;DR: Airflow runs pipelines on a schedule: a DAG (Directed Acyclic Graph) is tasks and their order with no loops; operators perform actions, sensors wait for a condition; the scheduler creates runs. The version in this lesson is 3.3.2. Bookshop's DAG uses TaskFlow (@dag, @task, imports from airflow.sdk) and CronDataIntervalTimetable("0 0 * * *", timezone="Europe/London"), so the run labelled 1 October (logical_date = start of the interval) runs on 2 October at 00:00, after its interval is over. ds is a UTC date (2026-09-30 for that run), and "@daily" in Airflow 3 has no interval. XCom is for small values such as a date and a row count. catchup=True creates a run for every interval missed since start_date (13 runs), catchup=False (the default) only the last one, and airflow backfill create with a time and a zone fills old dates. retries=2 means 3 attempts; tasks must be idempotent.

⚡ DAG: an order with no loops

Meaning: A DAG is a set of tasks with one-way arrows, from the task that must finish first to the one after it, and no loops. One DAG is written as one Python file, and the scheduler creates its runs on schedule.

⚡ Operator versus Sensor

Meaning: An operator performs an action: BashOperator runs a shell command, SQLExecuteQueryOperator runs a query. A sensor waits for a condition to be met, such as FileSensor waiting for a file from the marketplace to appear.

⚡ A schedule with an interval

Write:

schedule=CronDataIntervalTimetable("0 0 * * *", timezone=LONDON),
Guess first: when does the run labelled 1 October start?

On 2 October at 00:00 London time, after its interval is over. Only then do all of 1 October's orders exist.

Meaning: Every day at 00:00 London time, and each run gets a whole day as its interval.

⚡ ds is a UTC date

Meaning: {{ ds }} is logical_date in UTC. 1 October 00:00 London time = 30 September 23:00 UTC, so ds says 2026-09-30. Take the date from data_interval_start converted to London time.

What you will learn