Catchup and backfill behavior in Apache Airflow - Time & Space Complexity
When Airflow runs tasks for past dates, it uses catchup and backfill features.
We want to know how the number of runs grows as the date range grows.
Analyze the time complexity of this Airflow DAG scheduling snippet.
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from datetime import datetime, timedelta
default_args = {'start_date': datetime(2023, 1, 1)}
dag = DAG('example_dag', default_args=default_args, schedule_interval='@daily', catchup=True)
task = DummyOperator(task_id='dummy_task', dag=dag)
This DAG runs daily starting from Jan 1, 2023, with catchup enabled to run missed dates.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Airflow schedules one task run per missed day.
- How many times: Once for each day from start_date until today.
As the number of missed days grows, the number of task runs grows linearly.
| Input Size (days missed) | Approx. Operations (task runs) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of task runs grows directly with the number of missed days.
Time Complexity: O(n)
This means the work Airflow does grows in a straight line as the number of missed days increases.
[X] Wrong: "Catchup runs all missed tasks instantly with no extra cost as days increase."
[OK] Correct: Each missed day adds a separate task run, so more days mean more work and time.
Understanding how Airflow schedules past runs helps you explain system behavior clearly and shows you grasp task scaling.
"What if catchup was set to False? How would the time complexity of task runs change when starting the DAG late?"
Practice
catchup=True do in an Airflow DAG?Solution
Step 1: Understand catchup parameter
Thecatchupparameter controls whether Airflow runs missed DAG runs automatically.Step 2: Effect of catchup=True
Whencatchup=True, Airflow schedules and runs all DAG runs that were missed since the last run.Final Answer:
It runs all missed DAG runs automatically to catch up. -> Option DQuick Check:
catchup=True means run missed DAG runs [OK]
- Confusing catchup with manual backfill
- Thinking catchup disables scheduling
- Assuming catchup deletes runs
Solution
Step 1: Identify catchup parameter usage
Thecatchupparameter is used to enable or disable catchup behavior in DAGs.Step 2: Disable catchup by setting catchup=False
Settingcatchup=Falsedisables automatic running of missed DAG runs.Final Answer:
dag = DAG('my_dag', catchup=False) -> Option CQuick Check:
Disable catchup with catchup=False [OK]
- Using backfill parameter instead of catchup
- Setting schedule_interval to None disables scheduling, not catchup
- Setting catchup=True enables, not disables
start_date='2024-04-01', schedule_interval='@daily', and catchup=True, if the DAG was paused and resumed on 2024-04-05, which DAG runs will Airflow execute immediately after resuming?Solution
Step 1: Understand catchup=True behavior
Withcatchup=True, Airflow runs all missed DAG runs fromstart_dateto current date.Step 2: Identify missed runs
The DAG was paused from 2024-04-01 to 2024-04-04, so runs for 2024-04-01, 02, 03, 04 were missed. On resuming 2024-04-05, Airflow runs all missed plus current.Final Answer:
Runs for 2024-04-01 to 2024-04-05 -> Option AQuick Check:
Catchup runs all missed dates including current [OK]
- Thinking only current date runs after resume
- Ignoring runs before resume date
- Assuming no runs run automatically after pause
Solution
Step 1: Identify manual run command for date range
Theairflow dags backfillcommand runs DAG runs manually for a specified date range.Step 2: Confirm correct syntax
Using-sand-eflags withbackfillspecifies start and end dates for manual runs.Final Answer:
airflow dags backfill -s 2024-03-01 -e 2024-03-05 my_dag -> Option AQuick Check:
Backfill runs DAG manually for date range [OK]
- Using trigger command with date range flags (not supported)
- Trying to use catchup command (does not exist)
- Using dags run with invalid date range syntax
catchup=False and a start_date set to 2024-01-01. You want to run all DAG runs from 2024-01-01 to 2024-01-10 manually. What is the best approach to achieve this without enabling catchup?Solution
Step 1: Understand catchup=False effect
Withcatchup=False, Airflow skips running missed DAG runs automatically.Step 2: Use backfill to run missed DAG runs manually
To run missed DAG runs without enabling catchup, use thebackfillcommand specifying the date range.Final Answer:
Use the backfill command for the date range 2024-01-01 to 2024-01-10. -> Option BQuick Check:
Backfill runs missed DAG runs manually without catchup [OK]
- Enabling catchup when not desired
- Assuming one trigger runs all missed dates
- Changing start_date to skip missed runs
Ready to prove it?
Pass the test and this pattern moves unknown
