Skip to main content

How to Set Task Dependencies with Bitshift Operators (>> and <<) in Apache Airflow®

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

In Apache Airflow®, a Dag's tasks run in the order you set between them. a >> b says a runs before b, and b << a says the same thing from the other side. Put tasks in a list and they all get the same place in the order, so they can run side by side. This guide sets the order of a four-task Dag on Airflow 3.4.0 and checks it in a real run.

Syntax​

Syntax
<upstream task> >> <downstream task>
<downstream task> << <upstream task>
<task> >> [<task>, <task>] >> <task>
PartWhat it means
<upstream task>The task that runs first
<downstream task>The task that waits for it
>>The task on the left runs before the task on the right
<<The task on the right runs before the task on the left
[<task>, <task>]A list: each task in it gets the same dependency, and they don't wait for each other

Airflow's Tasks documentation names the two operators: "There are two ways of declaring dependencies - using the >> and << (bitshift) operators", or the set_upstream and set_downstream methods. It recommends the operators, "as they are easier to read in most cases."

The Example​

A Dag called make_breakfast has four tasks. boil_water comes first. brew_tea and toast_bread both wait for it, and neither needs the other. serve waits for both.

Prerequisites​

  • Apache Airflow® running, for example with airflow standalone, and its dags folder in your AIRFLOW_HOME.

Step 1: Write the Four Tasks​

Create dags/make_breakfast.py. Each task only prints what it does:

dags/make_breakfast.py
from airflow.sdk import dag, task


@dag(schedule=None, tags=["demo"])
def make_breakfast():
@task
def boil_water():
print("Boiling water")

@task
def brew_tea():
print("Brewing tea")

@task
def toast_bread():
print("Toasting bread")

@task
def serve():
print("Serving breakfast")

Without dependencies, the four tasks have no order: none of them waits for another, so Airflow could start all four at once.

Step 2: Set the Order with >>​

Still inside the Dag function, call each task function once and keep what it returns:

dags/make_breakfast.py
boil = boil_water()
tea = brew_tea()
toast = toast_bread()
breakfast = serve()

Calling a task function inside a Dag doesn't run it. It adds the task to the Dag and gives you an object that stands for it, which >> can connect. Now set the order:

dags/make_breakfast.py
boil >> [tea, toast] >> breakfast

Read it left to right: boil first, then both tea and toast, then breakfast. The complete file, with the call to the Dag function at the end:

dags/make_breakfast.py
from airflow.sdk import dag, task


@dag(schedule=None, tags=["demo"])
def make_breakfast():
@task
def boil_water():
print("Boiling water")

@task
def brew_tea():
print("Brewing tea")

@task
def toast_bread():
print("Toasting bread")

@task
def serve():
print("Serving breakfast")

boil = boil_water()
tea = brew_tea()
toast = toast_bread()
breakfast = serve()

boil >> [tea, toast] >> breakfast


make_breakfast()

The make_breakfast.py Dag file in an editor window: four @task functions, the four calls kept as boil, tea, toast and breakfast, and the line boil >> [tea, toast] >> breakfast.

Step 3: Check the Order in the Graph View​

Trigger the Dag (How to Trigger a Dag from the Apache Airflow® UI walks through the dialog), open it and choose the Graph view:

The Graph view of make_breakfast: boil_water on the left, brew_tea and toast_bread stacked in the middle, and serve on the right, all four with a green success badge.

The graph reads left to right in the same order as the line of code. brew_tea and toast_bread sit in one column because they're in one list: both depend on boil_water, and serve depends on both.

Step 4: Check the Start Times​

Open the run's Task Instances tab:

The Task Instances of the make_breakfast run: boil_water started at 10:28:52, brew_tea and toast_bread both at 10:28:53, and serve at 10:28:55, all four Success.

boil_water started first, at 10:28:52. brew_tea and toast_bread started in the same second once it had finished, side by side, and serve started at 10:28:55, after both. That's the order boil >> [tea, toast] >> breakfast asked for.

Step 5: Write the Same Order with <<​

<< points the other way: the task on the right runs first. A second Dag, make_breakfast_backwards, has the same four tasks and ends with:

dags/make_breakfast_backwards.py
breakfast << [tea, toast] << boil

Its graph after a run:

The Graph view of make_breakfast_backwards: the same layout as before, boil_water, then brew_tea and toast_bread, then serve, all with a green success badge.

The graph is the same as the first Dag's: << only changes how the line reads, not the order. Pick one direction per Dag so the code reads the same way throughout.

Common Mistakes​

MistakeWhat happensDo this instead
Putting a list on both sides: [a, b] >> [c, d]Airflow's documentation says that for a list of tasks depending on another list, "you can't use either of the approaches above"Use cross_downstream, or chain() for pairs
Expecting the tasks in a list to run in their listed orderThey don't wait for each other: brew_tea and toast_bread started in the same secondPut >> between them if one must finish first
Reading << as "run in reverse"It sets the same kind of dependency, pointing the other wayRead the arrow: the task it points to runs second

Troubleshooting​

SymptomLikely causeFix
A task runs before the one you expectedThe arrow points the wrong wayCheck the Graph view, and swap the two sides or use the other operator
Two tasks in a list ran at the same timeThat's what a list meansChain them with >> if one must wait for the other
A task runs as soon as the Dag startsIt has no upstream taskAdd it to the dependency line

FAQ​

What do upstream and downstream mean?​

The upstream task runs first and the downstream task waits for it. The Tasks documentation says "We call the upstream task the one that is directly preceding the other task." In boil >> tea, boil is upstream of tea.

Can I use methods instead of operators?​

Yes. boil.set_downstream([tea, toast]) means the same as boil >> [tea, toast], and tea.set_upstream(boil) the same as tea << boil. The documentation says they "both do exactly the same thing".

Conclusion​

boil >> [tea, toast] >> breakfast made boil_water run first, brew_tea and toast_bread side by side, and serve last, and breakfast << [tea, toast] << boil gave the same graph. >> runs the left side first and << the right side first; a list puts several tasks in the same place in the order.

Additional Resources​

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.