Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 113/ 160
06. Data Transformation (dbt) & Workflow Orchestration

Writing Airflow DAGs (Operators, Sensors, Hooks, TaskFlow API @task)

Author DAGs with classic operators/sensors/hooks or the TaskFlow @task API.

IntermediateOperators · Sensors · TaskFlow3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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 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.

Visual diagram

Loading interactive 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.

Writing Airflow DAGs in one concrete design

@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.

Key takeaway: 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

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.

Key takeaway: 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.

How it works, step by step

Step 1 of 4
Operators

1. 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.

schedule, catchup, owner, and defaults
Key numbers at this step
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 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.
  • 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/end or ds, make tasks idempotent, and prefer one observable external command/action per operator with timeout.

Operators / Sensors / TaskFlow - roles, limits, and operating cost

Operators
Best for: Task templates that perform an action, from Bash/Python to provider-specific warehouse, Spark, or transfer APIs.

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.

Sensors
Best for: Tasks that wait on an external condition, ideally in reschedule or deferrable mode to avoid holding a worker.

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.

TaskFlow
Best for: Airflow's Python-decorated task API that infers XCom task dependencies from function return values.

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 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.
  • 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; a PythonSensor(mode='reschedule') or deferrable sensor checks source readiness, and BashOperator invokes 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 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.
  • When is Operators insufficient for Sensor mode, and what does adding Sensors cost in Parse time?
Interview tip: Lead with the data path and failure boundary: 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. Then make the answer operational: 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.

Knowledge check

Loading quiz

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.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Apache Airflow Architecture (Webserver, Scheduler, Workers, Metadata DB, Executors)
Next topic
Dynamic Task Mapping, XComs, Variables & Connections