Writing

Why Airflow runs your DAG "a day late"

Data intervals, logical_date versus trigger time, and catchup: the ideas behind almost every Airflow DAG that looks like it ran on the wrong day.

Every team hits this within a month of adopting Airflow. Someone schedules a daily DAG, watches it fire at midnight, opens the logs and finds it processing yesterday’s date. The verdict is “Airflow is a day behind”, a timedelta(days=1) gets added somewhere, and six months later there are four of those offsets scattered across the repo, two of them cancelling each other out, and nobody can say what a given run actually covers.

Airflow is not a day behind. It is doing what a batch scheduler has to do. Once the model clicks, every one of those offsets can come out.

A run is named after a window that has already closed

The sentence to hold on to is this: an Airflow run covers a window of time, it is named after the start of that window, and it is only queued once the window has ended.

Take schedule="@daily" with start_date at 2026-01-01 00:00 UTC. The first data interval is [2026-01-01 00:00, 2026-01-02 00:00). That interval is not finished until midnight on the 2nd, so the scheduler creates the run then. The run is labelled 2026-01-01.

This is not an implementation quirk. A daily job that aggregates a day of events cannot run at the start of that day, because the events have not happened yet. Airflow bakes the only sane choice into its scheduling model rather than leaving each DAG author to reinvent it.

So when you see a run labelled 2026-01-01 starting at 00:00 on the 2nd, nothing is late. That run is the 1st of January’s run, and it started as early as it possibly could.

logical_date is not “now”

Airflow 2.2 replaced the old execution_date name with logical_date and added explicit interval fields, precisely because execution_date read like “the time this executed” when it never meant that. For any time-based schedule:

  • logical_date equals data_interval_start, the beginning of the covered window
  • data_interval_end is the exclusive end of that window
  • the wall-clock moment the run actually started is dag_run.start_date, and it is deliberately not something you template into your SQL

The template variables follow the same split:

# Airflow 2.2+ — say what you mean
"{{ ds }}"                    # logical_date as YYYY-MM-DD == start of the window
"{{ data_interval_start }}"   # timezone-aware datetime, start of the window
"{{ data_interval_end }}"     # timezone-aware datetime, exclusive end
"{{ ts }}"                    # logical_date, ISO 8601

# Deprecated in 2.2, gone in Airflow 3 — do not add these to new DAGs
"{{ execution_date }}"
"{{ next_ds }}"
"{{ tomorrow_ds }}"
"{{ yesterday_ds }}"

Almost every “day late” bug is one of two mismatches:

  1. You want the window the run covers, and you used now() or current_date inside the query. The task then reads whatever the clock says at execution time, which silently breaks the moment you rerun or backfill.
  2. You want the calendar day on which the job runs, and you used {{ ds }}. That is off by one interval by design.

For case 2, {{ data_interval_end | ds }} gives you the day the run is triggered on for a daily schedule. Reach for it deliberately, not as a patch.

Write queries against the interval, not the clock

The half-open interval is the whole point. Filter with >= start and < end and a rerun of any window produces exactly the same rows, whether you run it now or in eighteen months.

-- both bounds come from the run, so this is reproducible and idempotent
SELECT
    order_date,
    count(*) AS orders,
    sum(gmv)  AS gmv
FROM   events.orders
WHERE  event_ts >= TIMESTAMP '{{ data_interval_start.strftime("%Y-%m-%d %H:%M:%S") }}'
  AND  event_ts <  TIMESTAMP '{{ data_interval_end.strftime("%Y-%m-%d %H:%M:%S") }}'
GROUP  BY order_date

Format the timestamps explicitly rather than interpolating the objects directly. {{ data_interval_start }} renders pendulum’s ISO string — 2026-03-01T02:00:00+00:00 — and that offset and T separator are not portable: Postgres and Redshift silently discard the offset when casting to timestamp, which shifts your window, and Trino and Athena reject the T outright.

Pair that with an idempotent write. On a partitioned table that means replacing the partition for the interval rather than appending to it, so a rerun does not double count:

@task
def write_daily(ds: str = None):
    # `ds` arrives as a real argument. Jinja is only rendered in an operator's
    # templated fields — put `{{ ds }}` inside a plain Python function body and
    # you will write a partition literally named "{{ ds }}".
    spark.sql(f"""
        INSERT OVERWRITE TABLE marts.orders_daily
        PARTITION (dt = '{ds}')
        SELECT ...
    """)

The combination of a half-open window and an overwrite by that window is what makes a backfill safe to run twice. Nothing else in Airflow will give you that.

Late-arriving data is a scheduling problem, not a template problem

A very common situation: upstream drops the previous day’s extract into S3 at about 02:00. The instinct is to schedule at midnight and add a day somewhere. Don’t. Schedule the DAG when the data is actually there:

with DAG(
    dag_id="orders_daily",
    schedule="0 2 * * *",                 # 02:00 every day
    start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
    catchup=True,
    max_active_runs=3,
    default_args={"retries": 2, "retry_delay": timedelta(minutes=10)},
) as dag:
    ...

The run labelled 2026-03-01 now fires at 02:00 on 2026-03-02 and {{ ds }} is 2026-03-01, which is exactly the day whose data just landed. If the arrival time is unreliable, add a sensor in reschedule mode with a timeout rather than moving the schedule later and hoping.

One caveat that bites people who schedule in a local timezone: logical_date is stored in UTC, and {{ ds }} is that UTC value formatted as a date. Keep start_date in tz="UTC" above and the two agree. Switch it to tz="Asia/Kolkata" and the same 02:00 schedule gives a logical_date of 20:30 UTC on the previous day, so {{ ds }} renders 2026-02-28 — reintroducing the exact off-by-one this post is about. If you must schedule locally, derive your partition value from data_interval_start in the timezone you care about rather than trusting ds.

Catchup is not the villain

catchup=True means: if the scheduler notices that intervals between start_date and now have no runs, create them. That is what you want for a table that is partitioned by day, and it is what makes a fresh deployment fill in history on its own.

It becomes a problem in two situations. The first is an old start_date on a DAG that does not need history, which stampedes hundreds of runs on first deploy. The second is any DAG whose task bodies are not idempotent, where the stampede also corrupts data. Cap concurrency with max_active_runs before you reach for catchup=False.

And know what catchup=False actually does: it does not run “for today”. It runs the single most recently completed interval and ignores everything older. A daily DAG unpaused at 15:00 with catchup=False still gets a run labelled with yesterday’s date. People are often surprised twice by the same mechanism.

The default is version dependent. Airflow 2 ships [scheduler] catchup_by_default = True; Airflow 3 flipped that default to False. Don’t rely on memory here, check the deployment:

airflow config get-value scheduler catchup_by_default

On MWAA the same value is exposed as the scheduler.catchup_by_default environment configuration option, and it is worth setting it explicitly so the behaviour does not change under you at the next version bump.

Time zones, and the one that bites

Airflow stores everything in UTC and converts for display. Give start_date a timezone-aware pendulum datetime and the cron expression is then evaluated in that zone, including across daylight saving transitions. Two rules that save real incidents:

  • Never compute start_date dynamically. datetime.now() - timedelta(days=1) or the old days_ago() helper (deprecated in 2.x, removed in 3) makes the first interval move every time the file is parsed, which can leave a DAG that never triggers at all. Hard-code a date.
  • If your cron time falls inside a DST gap or repeat, decide what should happen. In zones without DST, such as Asia/Kolkata, this never arises; in Europe/London it arises twice a year.

Cross-DAG waits inherit the same confusion

ExternalTaskSensor matches on logical_date by default. Two DAGs on different schedules never share a logical date, so the sensor waits forever and someone eventually deletes it. Give it the offset explicitly:

wait_for_orders = ExternalTaskSensor(
    task_id="wait_for_orders",
    external_dag_id="orders_daily",
    external_task_id="publish",
    # this DAG runs hourly, the upstream one daily: line the windows up
    execution_date_fn=lambda dt: dt.replace(hour=0, minute=0, second=0, microsecond=0),
    mode="reschedule",      # frees the worker slot while waiting
    timeout=60 * 60 * 4,
    poke_interval=120,
)

Use execution_delta for a fixed offset and execution_date_fn when the mapping is not a constant. Better still, if both DAGs are yours, move to asset (dataset) scheduling and let the producer signal the consumer instead of both guessing at each other’s calendar.

When the interval model does not fit

Some work genuinely has no window: a manual reprocessing DAG, or something triggered by a file landing. In Airflow 3 those runs may carry no logical date at all, so any code that assumes logical_date is always a timestamp needs a guard. For schedules that are irregular but still interval-shaped, such as trading days or a fiscal calendar, write a custom Timetable rather than a cron expression plus a short-circuit task. The Timetable API exists exactly so the run’s name and its window stay truthful.

The short checklist

  • Every run covers [data_interval_start, data_interval_end) and is queued at the end of it.
  • {{ ds }} and logical_date are the start of that window, never “now”.
  • Query with >= start and < end; write by overwriting the partition for that window.
  • Handle late data by scheduling later or by sensing, never by adding a day to a template.
  • Set catchup and max_active_runs on purpose, and check the config default for your version.
  • Give ExternalTaskSensor an explicit execution_delta or execution_date_fn.

Get these right once and the phrase “the DAG is a day behind” disappears from your standups.

All writing