Task dependencies (>> and << operators) in Apache Airflow - Time & Space Complexity
When we set task dependencies in Airflow using the >> and << operators, we create relationships between tasks.
We want to understand how the time to set these dependencies grows as we add more tasks.
Analyze the time complexity of setting dependencies between tasks using >> operator.
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from datetime import datetime
dag = DAG('example_dag', start_date=datetime(2023, 1, 1))
task1 = DummyOperator(task_id='task1', dag=dag)
task2 = DummyOperator(task_id='task2', dag=dag)
task3 = DummyOperator(task_id='task3', dag=dag)
# Set dependencies
task1 >> task2 >> task3
This code creates three tasks and sets dependencies so task1 runs before task2, which runs before task3.
Look for repeated steps in setting dependencies.
- Primary operation: Linking one task to the next using the >> operator.
- How many times: Once per pair of tasks connected in the chain.
As the number of tasks grows, the number of dependency links grows too.
| Input Size (n tasks) | Approx. Operations (links) |
|---|---|
| 10 | 9 |
| 100 | 99 |
| 1000 | 999 |
Pattern observation: The number of operations grows roughly one less than the number of tasks, increasing linearly.
Time Complexity: O(n)
This means the time to set dependencies grows in a straight line as you add more tasks.
[X] Wrong: "Setting dependencies between tasks takes the same time no matter how many tasks there are."
[OK] Correct: Each dependency link is a separate step, so more tasks mean more links and more time.
Understanding how task dependencies scale helps you design workflows that run efficiently and avoid bottlenecks.
"What if we set dependencies between every task and every other task (all-to-all)? How would the time complexity change?"
Practice
task1 >> task2 mean?Solution
Step 1: Understand the meaning of
The>>operator>>operator in Airflow means "runs before" or "upstream to downstream" dependency.Step 2: Apply to given tasks
Giventask1 >> task2, task1 must finish before task2 starts.Final Answer:
task1 runs before task2 -> Option AQuick Check:
>>means runs before [OK]
- Confusing >> with <<
- Thinking >> means runs after
- Assuming tasks run in parallel
Solution
Step 1: Identify the operator meaning
>>means "runs before"; sotask1 >> task2means task1 runs before task2.Step 2: Match the requirement
To make task2 run after task1, task1 must run before task2, sotask1 >> task2is correct.Final Answer:
task1 >> task2 -> Option AQuick Check:
task2 after task1 = task1 >> task2 [OK]
- Using << backwards
- Swapping task order
- Confusing which task is upstream
task1 >> task2 << task3
Which statement is true about the task execution order?
Solution
Step 1: Break down the dependencies
task1 >> task2means task1 runs before task2;task3 << task2means task3 runs before task2 (because << means runs after, so task2 runs after task3).Step 2: Combine the relations
Both task1 and task3 must finish before task2 starts.Final Answer:
task1 runs before task2, and task3 runs before task2 -> Option CQuick Check:
task1 and task3 before task2 = task1 runs before task2, and task3 runs before task2 [OK]
- Misreading << as runs before
- Assuming task2 runs first
- Thinking tasks run in parallel
task1 >> >> task2
Solution
Step 1: Analyze the operator usage
The code has two consecutive >> operators without a task in between, which is invalid syntax in Python and Airflow.Step 2: Understand Airflow dependency syntax
Only one >> operator is allowed between two tasks to set dependency; double >> causes syntax error.Final Answer:
Syntax error due to double >> operators -> Option BQuick Check:
Double >> is invalid syntax [OK]
- Writing multiple >> operators in a row
- Ignoring syntax errors
- Assuming double >> means stronger dependency
task1 and task3 run first in parallel, then task2 runs after both finish. Which code correctly sets this dependency?Solution
Step 1: Understand the requirement
task1 and task3 run in parallel first, then task2 runs after both finish.Step 2: Translate to Airflow syntax
Using list syntax[task1, task3] >> task2means both task1 and task3 run before task2.Step 3: Check other options
task1 >> task2 >> task3 runs tasks sequentially; B and D have wrong directions.Final Answer:
[task1, task3] >> task2 -> Option DQuick Check:
List >> task means all list tasks before next [OK]
- Using >> in wrong order
- Not using list for parallel tasks
- Confusing << with >>
Ready to prove it?
Pass the test and this pattern moves unknown
