Writing Airflow DAGs (Operators, Sensors, Hooks, TaskFlow API @task)
Author DAGs with classic operators/sensors/hooks or the TaskFlow @task API.
In plain words
Schedule, catchup, owner, and defaults. Next, operators, TaskFlow callables, and edges. Finally, graph, one logical interval, retry, and idempotent output.
Think of it like…
A DAG file is a route plan, an Operator is the vehicle instruction, a Hook opens a named service door, and a Sensor is a staffed gate rather than the cargo itself.
Where you’ve seen it
@task def extract(ds: str): ... writes one dated partition; a PythonSensor(mode='reschedule') or deferrable sensor checks source readiness, and BashOperator invokes a container/CLI with explicit exit behavior.
Words you’ll hear
- Operators
- Task templates that perform an action, from Bash/Python to provider-specific warehouse, Spark, or transfer APIs.
- Sensors
- Tasks that wait on an external condition, ideally in reschedule or deferrable mode to avoid holding a worker.
- TaskFlow
- Airflow's Python-decorated task API that infers XCom task dependencies from function return values.
- Operational boundary
- A
PythonOperatorwith heavy imports at DAG-parse time slows every scheduler, a sensor inpokemode consumes a worker indefinitely, and an operator that defaults tonow()ignores retries/backfills. Keep callables/imports lazy, use deferrable sensors where available, passdata_interval_start/endords, make tasks idempotent, and prefer one observable external command/action per operator with timeout.
Visual diagram
Theory & deep dive
Operators: the working mechanism
Airflow DAG files declare operators, sensors, hooks, or TaskFlow functions with clear task IDs and XCom-sized outputs, while templates pass each run's logical date and config to code.
Schedule, catchup, owner, and defaults. Operators, TaskFlow callables, and edges, then connections, templates, timeout, retries; this exposes the state governed by TaskFlow.
@task def extract(ds: str): ... writes one dated partition; a PythonSensor(mode='reschedule') or deferrable sensor checks source readiness, and BashOperator invokes a container/CLI with explicit exit behavior.
Writing Airflow DAGs: failure mode and production response
A PythonOperator with heavy imports at DAG-parse time slows every scheduler, a sensor in poke mode consumes a worker indefinitely, and an operator that defaults to now() ignores retries/backfills.
Keep callables/imports lazy, use deferrable sensors where available, pass data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout. Track Parse time as "DAG import should stay network-free and fast", and make Sensor mode visible to the dataset owner.
data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.How it works, step by step
Step 1 of 41. Declare DAG
Schedule, catchup, owner, and defaults. Airflow DAG files declare operators, sensors, hooks, or TaskFlow functions with clear task IDs and XCom-sized outputs, while templates pass each run's logical date and config to code.
- Parse time
- DAG import should stay network-free and fast
- Sensors
- Tasks that wait on an external condition, ideally in reschedule or deferrable mode to avoid holding a worker.
When to use it & trade-offs
What it is
Airflow DAG files declare operators, sensors, hooks, or TaskFlow functions with clear task IDs and XCom-sized outputs, while templates pass each run's logical date and config to code. @task def extract(ds: str): ... writes one dated partition; a PythonSensor(mode='reschedule') or deferrable sensor checks source readiness, and BashOperator invokes a container/CLI with explicit exit behavior.
Why it matters: A PythonOperator with heavy imports at DAG-parse time slows every scheduler, a sensor in poke mode consumes a worker indefinitely, and an operator that defaults to now() ignores retries/backfills. Keep callables/imports lazy, use deferrable sensors where available, pass data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.
When to reach for it
- Author DAGs with classic operators/sensors/hooks or the TaskFlow @task API.
- Choose Operators because it provides this capability: Task templates that perform an action, from Bash/Python to provider-specific warehouse, Spark, or transfer APIs.
- Keep evidence for Parse time at "DAG import should stay network-free and fast" so the last production stage-Verify - graph, one logical interval, retry, and idempotent output-remains testable.
When to avoid or be careful
- A
PythonOperatorwith heavy imports at DAG-parse time slows every scheduler, a sensor inpokemode consumes a worker indefinitely, and an operator that defaults tonow()ignores retries/backfills. - Do not conflate Sensors with Operators. The second concept contributes different behavior: Tasks that wait on an external condition, ideally in reschedule or deferrable mode to avoid holding a worker.
- Release only after this guardrail is in place: Keep callables/imports lazy, use deferrable sensors where available, pass
data_interval_start/endords, make tasks idempotent, and prefer one observable external command/action per operator with timeout.
Operators / Sensors / TaskFlow - roles, limits, and operating cost
Strengths: Task templates that perform an action, from Bash/Python to provider-specific warehouse, Spark, or transfer APIs. Airflow DAG files declare operators, sensors, hooks, or TaskFlow functions with clear task IDs and XCom-sized outputs, while templates pass each run's logical date and config to code.
Watch out: A PythonOperator with heavy imports at DAG-parse time slows every scheduler, a sensor in poke mode consumes a worker indefinitely, and an operator that defaults to now() ignores retries/backfills.
Strengths: Tasks that wait on an external condition, ideally in reschedule or deferrable mode to avoid holding a worker. In this workflow it supports: Configure runtime - connections, templates, timeout, retries.
Watch out: The operating cost is real: poke holds a slot; reschedule/deferrable returns capacity.
Strengths: Airflow's Python-decorated task API that infers XCom task dependencies from function return values. Its output can be checked with Task timeout.
Watch out: Operating guardrail: Keep callables/imports lazy, use deferrable sensors where available, pass data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Operators, and how would Parse time reveal it?”
- “Why is Sensors distinct from this behavior: Airflow's Python-decorated task API that infers XCom task dependencies from function return values?”
- Production clue: A PythonOperator with heavy imports at DAG-parse time slows every scheduler, a sensor in poke mode consumes a worker indefinitely, and an operator that defaults to now() ignores retries/backfills.
- Design probe: Keep callables/imports lazy, use deferrable sensors where available, pass data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.
Benchmarks & rules of thumb
- Parse time
- DAG import should stay network-free and fast
- Sensor mode
- poke holds a slot; reschedule/deferrable returns capacity
- Task timeout
- execution SLA bounds retries and stuck external calls
Common mistakes & follow-ups
Common interview mistakes
- A
PythonOperatorwith heavy imports at DAG-parse time slows every scheduler, a sensor inpokemode consumes a worker indefinitely, and an operator that defaults tonow()ignores retries/backfills. - Reporting success from a job exit code without checking Parse time and Sensor mode.
- Treating this example as complete without replay, rollback, or access boundaries:
@task def extract(ds: str): ...writes one dated partition; aPythonSensor(mode='reschedule')or deferrable sensor checks source readiness, andBashOperatorinvokes a container/CLI with explicit exit behavior.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: Task templates that perform an action, from Bash/Python to provider-specific warehouse, Spark, or transfer APIs.
- What does the recovery plan do when this occurs? A
PythonOperatorwith heavy imports at DAG-parse time slows every scheduler, a sensor inpokemode consumes a worker indefinitely, and an operator that defaults tonow()ignores retries/backfills. - When is Operators insufficient for Sensor mode, and what does adding Sensors cost in Parse time?
data_interval_start/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Writing Airflow DAGs (Operators, Sensors, Hooks, TaskFlow API @task) with data engineer interview Q&A and hands-on practice.