Skip to content
mentoor

Topic 4 of 5 · 5 min read

Orchestrating jobs with Airflow or Dagster

Goal: Make a pipeline task idempotent so a retry or backfill produces the same result as one clean run.

An orchestrator runs tasks in order on a schedule and retries them when they fail. Retries and backfills only help if each task is idempotent: running it twice for the same period leaves the same result as running it once.

Check yourself · 1 of 3

A task copies a day's orders into a table with a plain insert. It fails after writing half the rows and is retried. What is the most likely result?

Choose one answer

Why it matters

Tasks fail halfway. A network call times out after 40,000 of 100,000 rows were written. The orchestrator retries, as it should. If the task appends rows, you now have 140,000 rows. An assistant often writes tasks as "read, then insert", which passes the first run and breaks on the first retry.

The pieces

  • DAG (directed acyclic graph): tasks and their dependencies, with no loops. In Airflow you write tasks and link them; in Dagster you declare assets (tables or files) and Dagster works out the order.
  • Schedule: when a run starts. Each run belongs to a data interval, such as "the day of 2026-11-30". Use that interval, not "now", to decide which data to process.
  • Retries: automatic reruns after a failure, usually with a delay. Set a limit and an alert at the end.
  • Backfill: running the pipeline for past intervals, for example after fixing a bug, so each old day is recomputed.

How to make a task idempotent

  1. Partition by the run's interval. Process only order_date = {{ ds }}, never now().
  2. Overwrite, do not append. Delete the interval then insert, or use insert overwrite on a partition, or merge on a key. Do the delete and insert in one transaction.
  3. Write to a temporary location first, then swap or rename when complete, so a half-written output is never visible.
  4. Keep tasks small and separate extract, transform and load, so a retry repeats only the step that failed.

Before and after

# Before: appends, depends on the clock
def load_orders():
    rows = fetch_orders(since=yesterday_from_clock())
    db.insert("orders_daily", rows)

A retry duplicates rows, and a backfill of last week loads yesterday seven times.

# After: tied to the interval, replaces that day
def load_orders(ds):
    rows = fetch_orders(day=ds)
    with db.transaction():
        db.execute("delete from orders_daily where order_date = %s", [ds])
        db.insert("orders_daily", rows)

What goes wrong

  • Using the current time inside a task, so a backfill reprocesses today's data again.
  • Huge top-level code in DAG files, which slows the scheduler.
  • Retries with no cap that hide a real outage.
  • Backfilling over a full-load table without checking it overwrites correctly.

Examples

Repairing a duplicated load

A retry appended 40,000 rows twice into orders_daily. The fix is not to clean by hand. Change the task to delete the day and insert it in one transaction, then rerun that day. The table is correct again, and the same rerun is safe next time.

A backfill after a bug fix

A tax rule was wrong for November. Backfill 30 daily intervals. With an overwrite per interval, each day is rebuilt from source and replaces the bad rows. With an appending task, each day would hold both the old and new numbers.

Key points

  • Idempotent means running a task twice for the same interval gives the same result as running it once.
  • Take the data interval from the run, not from the current time, so backfills process the right days.
  • Overwrite or merge the interval in one transaction; appending makes every retry a duplicate.
  • Retries need a limit and an alert, or they hide real outages.
  • Small tasks for extract, transform and load mean a retry repeats only the failed step.

Common mistakes

  • Appending in a task that the orchestrator may retry.
  • Reading now() instead of the run's data interval.
  • Retrying forever with no alert.
  • Putting heavy work in DAG file top-level code instead of inside tasks.

Check yourself · 2 of 3

Which change makes a daily load task safe to backfill for last month?

Choose one answer

Check yourself · 3 of 3

Why keep extract, transform and load as separate tasks?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic