Sharing data between tasks effectively in Apache Airflow - Time & Space Complexity
When tasks share data in Airflow, the way data is passed affects how long the workflow takes to run.
We want to know how the time to share data grows as the amount of data or number of tasks increases.
Analyze the time complexity of the following code snippet.
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
def push_data(ti):
data = list(range(1000))
ti.xcom_push(key='my_data', value=data)
def pull_data(ti):
data = ti.xcom_pull(key='my_data', task_ids='push_task')
print(len(data))
dag = DAG('example_dag', start_date=datetime(2024, 1, 1))
push_task = PythonOperator(task_id='push_task', python_callable=push_data, dag=dag)
pull_task = PythonOperator(task_id='pull_task', python_callable=pull_data, dag=dag)
push_task >> pull_task
This code pushes a list of 1000 numbers from one task and pulls it in another using XComs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and transferring a list of data items between tasks.
- How many times: The list size determines how many items are handled; here 1000 items.
As the data size grows, the time to push and pull data grows roughly in proportion to the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Handles 10 data items |
| 100 | Handles 100 data items |
| 1000 | Handles 1000 data items |
Pattern observation: Doubling the data roughly doubles the work to share it.
Time Complexity: O(n)
This means the time to share data grows linearly with the amount of data passed between tasks.
[X] Wrong: "Sharing data between tasks is always instant and does not depend on data size."
[OK] Correct: Actually, larger data means more time to serialize, transfer, and deserialize, so it takes longer.
Understanding how data sharing scales helps you design workflows that run smoothly and predictably as they grow.
"What if we changed from passing data via XCom to using an external database? How would the time complexity change?"
Practice
Solution
Step 1: Understand Airflow Variables role
Airflow Variables are designed to hold small data values that can be shared between tasks.Step 2: Identify the correct use case
Sharing data between tasks requires a storage mechanism accessible by all tasks, which Airflow Variables provide.Final Answer:
To store small pieces of data accessible across different tasks -> Option DQuick Check:
Airflow Variables = data sharing [OK]
- Confusing Variables with task scheduling
- Thinking Variables execute tasks
- Using Variables for logging
my_var with value 123 inside a PythonOperator task?Solution
Step 1: Recall Variable.set syntax
The correct method to set a variable is Variable.set(key, value) where key is a string and value is the data.Step 2: Check option correctness
Variable.set('my_var', 123) uses Variable.set with key 'my_var' and value 123 correctly as a string key and integer value.Final Answer:
Variable.set('my_var', 123) -> Option AQuick Check:
Set variable = Variable.set(key, value) [OK]
- Using Variable.get instead of set to store data
- Not quoting the variable name key
- Using a non-existent method like Variable.create
task_b?
from airflow.models import Variable
def task_a():
Variable.set('shared_key', 'hello')
def task_b():
value = Variable.get('shared_key')
print(value)Solution
Step 1: Analyze task_a behavior
task_a sets the Variable 'shared_key' with value 'hello'.Step 2: Analyze task_b behavior
task_b retrieves the Variable 'shared_key' and prints its value, which is 'hello'.Final Answer:
hello -> Option BQuick Check:
Set then get Variable = 'hello' [OK]
- Expecting the key name instead of value
- Assuming Variable.get returns None if set
- Forgetting to set the Variable before getting
task_b to get a Variable set by task_a:
value = Variable.get('my_var')
But you get an error: KeyError: 'Variable my_var does not exist'. What is the best fix?Solution
Step 1: Understand the error cause
The error means 'my_var' was not set before getting it, so Variable.get fails.Step 2: Use default value to avoid error
Variable.get supports a default_var parameter to return if key missing, preventing KeyError.Final Answer:
Set a default value in Variable.get like Variable.get('my_var', default_var='default') -> Option AQuick Check:
Use default_var to avoid missing key error [OK]
- Using Variable.set instead of get to read data
- Ignoring missing Variable errors
- Restarting scheduler without fixing code
Solution
Step 1: Understand data sharing options
Airflow Variables are for small strings; passing Python variables directly is not possible between tasks.Step 2: Identify best practice for complex data
XComs are designed to share data like lists between tasks efficiently and safely.Final Answer:
Use XComs to push and pull the list between tasks -> Option CQuick Check:
XComs = best for task-to-task data sharing [OK]
- Trying to pass Python variables directly
- Using Variables for large or complex data
- Using local files which break portability
Ready to prove it?
Pass the test and this pattern moves unknown
