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
- DAG (Directed Acyclic Graph): a set of tasks with arrows between them. Directed: each arrow points one way, from the task that must finish first to the one after it. Acyclic: no loops; a task can't wait for itself. One DAG is written as one Python file.
- Task: one unit of work in a DAG, such as "load one date of sales".
- Operator: a template for a task that performs an action:
BashOperatorruns a shell command,PythonOperator(or@task) calls a Python function,SQLExecuteQueryOperatorruns a query. - Sensor: a special operator that waits for a condition to be met, such as
FileSensorwaiting for a file from the marketplace to appear before the next task may run. - Scheduler: the process that reads DAGs, creates runs when their time comes, and hands tasks to workers. In Airflow 3 it works alongside the dag processor (reads DAG files), the API server (the UI and the API tasks talk to) and the triggerer.
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()
schedule=CronDataIntervalTimetable("0 0 * * *", timezone=LONDON): every day at 00:00 London time, and each run gets a whole day as its interval."0 0 * * *"is a cron expression: minute 0, hour 0, every day of the month, every month, every weekday.start_dateis where the first interval begins.catchupandretriescome below.- Calling
load_date(order_date)with the result ofpick_date()draws an arrow:pick_datemust finish first. The classic form writes it with>>, for exampleextract >> transform >> load. data_interval_start=Noneamong a task's parameters makes Airflow fill it in from the current run.
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:
- data interval (
data_interval_starttodata_interval_end): the stretch of data that run is responsible for. - logical date (
logical_date): the run's label, equal to the start of the interval. - when it runs: a run may only start once its interval is over, at
data_interval_end.
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:
dsis a UTC date. The template variable{{ ds }}islogical_datein UTC. 1 October 00:00 London time = 30 September 23:00 UTC, because London is on summer time (UTC+1) until 25 October, sodssays2026-09-30. That's why the DAG above convertsdata_interval_startto London time before taking the date.- In Airflow 3,
schedule="@daily"has no interval. A plain cron now usesCronTriggerTimetable: the run at 2 October 00:00 is labelled 2 October 00:00, anddata_interval_startequalsdata_interval_end, both 2 October 00:00. The task above would load 2 October, a day that has just begun: 0 rows. In Airflow 2,"@daily"still used an interval. So name the timetable outright, as the DAG does, or giveCronTriggerTimetableaninterval=timedelta(days=1).
Catchup and backfill
The DAG above was switched on at 21:00 London time on 11 October, though its start_date is 28 September.
catchup=True: the scheduler at once creates a run for every interval that has finished: 13 runs, 28 September to 10 October, run in order (max_active_runs=1).catchup=False(the default in Airflow 3): only the last interval, 10 October. The history before it stays empty.
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:
- A new UI, and DAG versioning: each run records the version of the DAG code that ran it.
- Assets (formerly datasets): a DAG can be scheduled to "run whenever this table is updated" (
schedule=[Asset(...)]), not only by the hour. - Imports for DAG authors move to
airflow.sdk, and tasks may no longer touch Airflow's metadata database directly. - Backfills are run by the scheduler (
airflow backfill create),catchupdefaults toFalse, and a plain cron becomes aCronTriggerTimetable. - Removed: SubDAGs (use TaskGroups or Assets), SLAs (use Deadline Alerts),
execution_date(uselogical_date), and theschedule_intervalargument (useschedule).
⚠️ What makes a DAG load the wrong day, or weighs Airflow down
- Taking
dsfor a London date.dsis UTC: while London is on summer time (UTC+1), a 00:00 run has adsone day earlier. Take the date fromdata_interval_startconverted to your time zone. "@daily"in Airflow 3 for a daily load. There's no interval, so the task loads the day that has just begun. UseCronDataIntervalTimetableor give it an interval.- Big data through XCom. Pass a date or a path, not a DataFrame.
- Heavy work at the top level of a DAG file. Code outside the task functions runs every time the DAG file is parsed again, far more often than the schedule. Put queries and API calls inside a task.
- Tasks that aren't idempotent. Retries, catchup and backfills rerun tasks; without
DELETEthenINSERT, the numbers double.
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.