Catchup and backfill
Part of the free LogicWiz Data Engineering course, Module 9: Orchestration. The SQL and Python run in the app, on real Postgres and real Python in the browser.
It is 09:01 on 10 June. One minute ago you switched on the new daily_revenue DAG, with a start_date of 1 June. Dana is already in the channel: the shop's API is refusing requests. The grid shows nine runs, all started at 09:00. You expected one.
You'll learn to
- Predict what catchup on and off do when a DAG starts late
- Backfill a date range and choose which dates to rerun
- Write tasks that give the same answer whenever a date is rerun
- Find the limit that sets how fast a backfill goes
Learn
Nine runs at 09:00
You expected one run and got nine. Here is why.
The DAG's start_date is 1 June, and you switched it on at 09:00 on 10 June. By then nine daily intervals had already ended without a run: 1 June to 9 June. One flag decides what the scheduler does with them. It is called catchup.
catchup=True: create a run for every missed interval, oldest first, straight away.catchup=False: create a run only for the latest interval. The older ones are left alone.
In miniflow, a run_until() that jumps several days is the same situation: the scheduler was off, and it wakes up with intervals behind it.
One flag: nine runs or one
import miniflow as mf
def make(catchup):
dag = mf.DAG('daily_revenue', start_date='2025-06-01', schedule='@daily', catchup=catchup, log=False)
@dag.task()
def call_api(ctx):
pass
@dag.task()
def load(ctx):
pass
call_api >> load
return dag
for catchup in (True, False):
dag = make(catchup)
started = dag.run_until('2025-06-10 09:00') # switched on at 09:00 on 10 June
print(f"catchup={catchup}: started {len(started)}, from {started[0]} to {started[-1]}")
dag.grid()
The two DAGs differ by one flag. With catchup on, nine runs start at the same moment and every one calls the same API. That is how the shop's API got flooded.
Switched on at 09:00 on 10 June
Ten day boxes for 1 to 10 June: first nine ended intervals with no run, then with catchup on all nine start at once, then with catchup off only 9 June starts.
Switched on late. Nine daily intervals have ended and none has a run. 10 June is still going, so nothing is due for it yet.
With
catchup=True. The scheduler creates a run for every missed interval, oldest first, straight away. Nine runs, and nine calls to the same API in the same minute.With
catchup=False. Only the latest interval that has ended, 9 June, gets a run. 1 to 8 June never run unless you backfill them yourself.
Each small box is one daily interval, named by its logical date. The colour says what the scheduler did with it at 09:00.
The same morning, both ways
| catchup=True | catchup=False | |
|---|---|---|
| Runs started at once | 9 (1 to 9 June) | 1 (9 June) |
| 1 to 8 June | loaded now, all together | never run unless you backfill |
| Load on sources | a burst of 9 runs | normal |
| Good for | a new DAG that must build history | a DAG where only the latest data matters |
Neither is "right". Choose on purpose, before you deploy, and not after a message from the API's owner.
Set catchup on purpose, in every DAG. Airflow 2 defaulted to catchup=True, so a DAG with an old start_date could launch hundreds of runs the moment it was deployed. Airflow 3 changed the default to False, so the opposite surprise is now common: history that is silently never loaded. Write the flag in the DAG file, and pick a start_date you mean.
A week later: recompute 1 to 7 June
You set catchup=False and deploy again. A week later Tess finds a bug in the revenue rule: it counted cancelled and returned orders. She asks for 1 to 7 June to be recomputed.
Running a DAG again for a range of past logical dates is called a backfill. You need one when a new DAG must build history, when late data arrived for old days, or when a bug fix means old results are wrong.
The choice that matters is what to do with dates that already ran. Airflow 3 calls it the reprocess behaviour: only dates with no run, also the failed ones, or every date including the successful ones. Tess's recompute needs the last one.
Which dates a backfill runs
| reprocess | Runs dates that... | Use it when |
|---|---|---|
| none | never ran | filling a gap (catchup was off, a new DAG) |
| failed | never ran or failed | recovering from an outage |
| completed | every date in the range | recomputing after a bug fix or a logic change |
Recompute a week: append against delete-then-insert
import duckdb
import miniflow as mf
def revenue_of(ctx):
return duckdb.sql(f"""
select coalesce(sum(i.qty * i.unit_price), 0)
from 'data/orders.csv' as o join 'data/order_items.csv' as i on i.order_id = o.order_id
where o.order_ts >= '{ctx['data_interval_start']}' and o.order_ts < '{ctx['data_interval_end']}'
and o.status in ('placed', 'shipped', 'delivered')
""").fetchone()[0]
def run(idempotent):
duckdb.sql("create or replace table daily_revenue (day date, revenue double)")
dag = mf.DAG('daily_revenue', start_date='2025-06-01', schedule='@daily', catchup=False, log=False)
@dag.task()
def load(ctx):
if idempotent:
duckdb.sql(f"delete from daily_revenue where day = '{ctx['ds']}'")
duckdb.sql(f"insert into daily_revenue values ('{ctx['ds']}', {revenue_of(ctx)})")
dag.run_until('2025-06-08') # deployed on 8 June: only 7 June runs
dag.backfill('2025-06-01', '2025-06-07') # fill 1 to 6 June
dag.backfill('2025-06-01', '2025-06-07', reprocess='completed') # recompute everything
rows, days = duckdb.sql("select count(*), count(distinct day) from daily_revenue").fetchone()
print(f"idempotent={idempotent}: {rows} rows for {days} days")
run(idempotent=False)
run(idempotent=True)
Both versions ran the same three steps: deploy, fill the gap, recompute. The append version now has every day twice, so any weekly total is doubled. The idempotent one has exactly one row per day.
A backfill fills a week, then recomputes it
Seven day boxes for the daily_revenue table: after deploying only 7 June has a row, a backfill fills 1 to 6 June, a recompute with a plain insert gives every day two rows, and the same recompute with delete-then-insert leaves one row a day.
Deployed on 8 June, catchup off. Only the latest interval runs, so 7 June has its row. 1 to 6 June are empty.
Backfill 1 to 7 June. A plain backfill runs only the dates that never ran. It fills 1 to 6 June and leaves 7 June alone: seven rows, one per day.
Recompute everything, with a plain INSERT. The backfill reruns all seven dates with
reprocess='completed'. Each run appends a second row. Fourteen rows, and every weekly total is doubled.The same recompute, delete then insert. Each run deletes its day before it inserts. Seven rows for seven days, however many times you recompute.
Each box is one day of the table, with the rows it holds. Backfills are the most common way idempotency bugs reach production: the daily runs look fine for months, then the first big recompute doubles history.
Catchup and backfill on the run grid
A run grid over a week of June where the scheduler is down for three days: with catchup on every missed day fills in when it returns, with catchup off only the latest runs, and a backfill reruns 2 to 4 June.
Press Play to 8 June with Catchup after the outage ticked, then untick it and compare the three days the scheduler was down. At the end, press Backfill 2 to 4 June and watch those columns run again.
Remember: catchup is the scheduler filling missed dates by itself. A backfill is you choosing a range on purpose. Both rerun the past, so both need tasks that are safe to run twice.
A backfill of 1 June runs today
One more thing can go wrong, and idempotency does not save you from it. A backfill of 1 June runs today. So a task that asks the clock, or asks the data for "the latest day", processes the wrong day.
A favourite bug is a task that loads "the most recent day in the source" with max(order_ts). Run daily, it looks perfect, because the most recent day is the right day. Backfill a week, and it loads the same latest day seven times.
The "latest day" bug in a backfill
import duckdb
import miniflow as mf
dag = mf.DAG('daily_orders', start_date='2025-06-01', schedule='@daily', catchup=False, log=False)
@dag.task()
def latest_day(ctx): # wrong: ignores ctx, asks the data for "the latest day"
day, n = duckdb.sql("""
select cast(order_ts as date), count(*) from 'data/orders.csv'
group by 1 order by 1 desc limit 1
""").fetchone()
print(f" run {ctx['ds']} loaded {day}: {n} orders")
dag.backfill('2025-06-01', '2025-06-03')
Three runs, three different logical dates, and every one loaded 14 June. The fix is the habit from Lesson 9.1: filter on data_interval_start and data_interval_end.
Ninety days through four slots
Tess's week was small. Next, finance wants 90 days recomputed. Each run is call_api (10 minutes), then load (5 minutes), and the partner API must not get more than 4 calls at once. How long will it take?
The tightest limit decides. A ready task still has to pass four gates:
[core] parallelism: task instances running at once per scheduler, across every DAG.max_active_runs: runs of this DAG active at once.max_active_tasks: tasks active at once in one run (Airflow 3).- A pool: a named number of slots shared by every DAG. It is how you say "at most 4 calls to the partner API at once, whoever makes them".
A ready task stopped at a gate stays scheduled. It is queued when a slot frees.
The gates a backfill task passes through
Nested boxes: the scheduler with parallelism 32 holds a DAG with max_active_runs 8, whose runs each allow 16 active tasks; beside them a shared partner_api pool with 4 slots that every call_api task must take.
Read it from the outside in: each box is a limit, and a task must fit inside all of them. A 90-day backfill with max_active_runs=8: 8 runs are active, so up to 8 call_api tasks are ready, but only 4 hold pool slots. The other 4 wait in scheduled.
Count the waves
Work it out for 8 active runs and a 4-slot pool.
- 8 runs are active, so 8
call_apitasks are ready. - Only 4 get a pool slot. The other 4 wait.
- Each call takes 10 minutes, so every 10 minutes four calls finish and four more start: a wave.
- 90 calls, 4 at a time: 90 / 4 = 22.5, so 23 waves.
- 23 waves x 10 minutes = 230 minutes.
- The last run still needs its 5-minute load: 235 minutes.
Look at what is missing from the sum: max_active_runs. The pool is the tightest gate, so the pool sets the time.
The first 40 minutes of the backfill
A timeline of the four partner_api pool slots: each slot runs one 10-minute call_api task after another, oldest dates first, so the 90 runs advance four at a time.
Each row is one pool slot. Every 10 minutes four calls finish and four more take the slots, while each finished run's 5-minute load runs in default_pool alongside. 90 calls need 23 waves.
Turn the dials and time the backfill
def backfill_minutes(days, max_active_runs, api_slots, api_min=10, load_min=5):
"""Minute by minute: runs start oldest first, up to max_active_runs at once.
Each run is call_api (needs a slot in the partner_api pool), then load."""
not_started = list(range(days))
running = {} # run -> [task, minutes left]; None = waiting for a pool slot
minute = finished = 0
while finished < days:
while not_started and len(running) < max_active_runs:
running[not_started.pop(0)] = ['call_api', None] # state: scheduled
in_pool = sum(1 for task, left in running.values() if task == 'call_api' and left is not None)
for run, (task, left) in sorted(running.items()):
if task == 'call_api' and left is None and in_pool < api_slots:
running[run][1] = api_min # got a slot: queued, then running
in_pool += 1
minute += 1
for run in list(running):
task, left = running[run]
if left is None:
continue
if left > 1:
running[run][1] = left - 1
elif task == 'call_api':
running[run] = ['load', load_min] # the pool slot is free again
else:
del running[run]
finished += 1
return minute
for runs, slots in [(8, 4), (16, 4), (8, 8), (16, 16)]:
print(f"max_active_runs={runs:>2}, partner_api slots={slots:>2}: {backfill_minutes(90, runs, slots)} minutes")
With 4 pool slots, doubling max_active_runs changes nothing: it is still 235 minutes. The pool is the bottleneck, and it is also what protects the API. Raise the dial that is actually binding, and only as far as the source allows.
Remember: before a long backfill, run one day and check it. Then limit how much runs at once to protect the source, and compare counts with the source when it ends.
When each day needs the day before
Some tables build on yesterday: a running balance, or a dimension that keeps history (SCD type 2, Module 5). Then the order of a backfill matters too.
depends_on_past=True makes a task wait until its own previous run succeeded or was skipped, so the dates go one by one, oldest first. wait_for_downstream=True also waits for the tasks right after it in the previous run.
Airflow 3 can run a backfill newest dates first with --run-backwards. It refuses to do that when a task has depends_on_past.
Backfilling from the command line
Runs in Airflow:
# Airflow 3: the scheduler runs the backfill; choose what to reprocess
airflow backfill create --dag-id daily_revenue \
--from-date 2025-06-01 --to-date 2025-06-07 \
--reprocess-behavior completed --max-active-runs 2
# Airflow 2: the CLI ran the backfill itself; --reset-dagruns reran existing runs
airflow dags backfill --start-date 2025-06-01 --end-date 2025-06-07 \
--reset-dagruns daily_revenue
Both create runs with logical dates 1 to 7 June, each with its own data interval, so an idempotent task that reads its interval recomputes exactly those days. In Airflow 3 you can also start a backfill from the UI or the REST API.
Numbers to remember
| Setting | Default | What it limits or does |
|---|---|---|
| [core] parallelism | 32 | task instances running at once per scheduler, across every DAG |
| maxactiveruns on the DAG | 16 (maxactiverunsperdag) | runs of this DAG active at once; an Airflow 3 backfill takes its own --max-active-runs |
| maxactivetasks on the DAG | 16 | per run in Airflow 3; across all runs of the DAG in Airflow 2 |
| maxactivetisperdag on a task | you set it | that one task, across runs |
| defaultpool | 128 slots | every task that names no pool |
| poolslots on a task | 1 | how many slots of its pool the task takes |
| catchup | True on Airflow 2, False on Airflow 3 | whether missed intervals get runs |
| Backfill reprocess behaviour | none | only dates that never ran; failed and completed rerun more |
Every orchestrator has this idea. Dagster models dates as partitions and backfills a range of them; Databricks Jobs can be run for past dates with parameters; Azure Data Factory's tumbling window trigger keeps a window per interval and can rerun old windows (Lesson 9.5); dbt's microbatch strategy backfills a date range of an incremental model.
What changed at SwiftKart
daily_revenue now says catchup=False in its file, so switching it on starts one run, not nine. The history was filled with a backfill, two runs at a time, and Dana's API never noticed.
Tess's week was recomputed with reprocess='completed'. The load deletes its day before it inserts, so 1 to 7 June still have one row each, now with the right rule.
So far you have used miniflow, a scheduler small enough to read. In Lesson 9.4 you open a real Airflow DAG file: the one behind a ticket that says "the payments DAG is stuck again".
In one line
Catchup decides what happens to the dates a DAG missed, and a backfill reruns a range you choose; both are safe only when each task reads its own interval and can run twice, and the tightest limit (often a pool) sets the speed.
Interview corner
What is the difference between catchup and backfill in Airflow?
Catchup is a DAG setting: when the DAG is switched on or unpaused, it decides whether the scheduler creates runs for every missed interval since the start date (True) or only the latest (False). A backfill is an action you take: run the DAG for a chosen range of past logical dates, optionally reprocessing dates that already ran, for example after a bug fix.
How do you backfill a year of data safely?
Make sure every task reads only its data interval and writes idempotently, so reruns replace rather than add. Test a single day, then run the range with limited concurrency (max_active_runs) to protect sources and the warehouse, in order if days depend on each other. Afterwards reconcile row counts and totals with the source.
A 90-day backfill is crawling: 16 runs are active but only 4 tasks are running. What do you check?
Which state the waiting tasks are in. In scheduled, a limit in the scheduler holds them: a pool with free slots used up, max_active_tasks for the run (per run in Airflow 3, per DAG in Airflow 2), a task-level max_active_tis_per_dag, or [core] parallelism (32 per scheduler). In queued, the executor or the workers are the bottleneck. Often a pool of 4 slots on the source API is binding on purpose; then the backfill takes ceil(calls / 4) waves, and the honest fix is to agree a higher rate with the source owner, not to raise max_active_runs.
Why might a backfill load the wrong data even though every run succeeded?
The task probably depends on the clock or on "the latest data" instead of its interval: now(), CURRENT_DATE, or max(order_ts) from the source. Each backfill run then processes today or the newest day instead of its own date. The fix is to parameterise every query with data_interval_start and data_interval_end.