Bird
Raised Fist0
Apache Airflowdevops~20 mins

Why XCom enables task communication in Apache Airflow - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XCom Communication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does XCom facilitate communication between Airflow tasks?

In Apache Airflow, tasks often need to share data. Which of the following best explains how XCom enables this communication?

AXCom creates temporary files on the worker machine that tasks read and write to exchange data.
BXCom stores small messages or data in Airflow's metadata database, allowing tasks to push and pull information asynchronously.
CXCom sends data directly over network sockets between running tasks in real-time.
DXCom uses environment variables set globally on the Airflow server to share data between tasks.
Attempts:
2 left
💡 Hint

Think about where Airflow keeps task metadata and how tasks can access shared information.

💻 Command Output
intermediate
2:00remaining
What is the output of this XCom push and pull example?

Consider two Airflow tasks where task A pushes a value to XCom and task B pulls it. What will be printed by task B?

Apache Airflow
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime

def push_function(ti):
    ti.xcom_push(key='sample_key', value='hello world')

def pull_function(ti):
    value = ti.xcom_pull(key='sample_key', task_ids='push_task')
    print(value)

dag = DAG('xcom_example', start_date=datetime(2023, 1, 1))

push_task = PythonOperator(task_id='push_task', python_callable=push_function, dag=dag)
pull_task = PythonOperator(task_id='pull_task', python_callable=pull_function, dag=dag)
push_task >> pull_task
Ahello world
BKeyError: 'sample_key'
CNone
DSyntaxError
Attempts:
2 left
💡 Hint

Remember that task B pulls the value pushed by task A using the same key and task ID.

❓ Troubleshoot
advanced
2:00remaining
Why does this XCom pull return None?

A developer notices that their task pulling from XCom always gets None. The push task runs before the pull task. What is the most likely cause?

Apache Airflow
def pull_function(ti):
    value = ti.xcom_pull(key='data_key', task_ids='push_task')
    print(value)

# The push task uses ti.xcom_push(key='data_key', value='data') correctly.
AThe pull task runs before the push task, so no data is available yet.
BThe key used in xcom_pull does not match the key used in xcom_push.
CThe push task did not run successfully or did not push any value.
DXCom only works with JSON-serializable data, and the pushed data was not serializable.
Attempts:
2 left
💡 Hint

Check if the push task actually completed and pushed data.

🔀 Workflow
advanced
2:00remaining
Which Airflow feature allows tasks to share data asynchronously?

In a complex DAG, tasks need to exchange small pieces of data without direct connections. Which Airflow feature is designed for this purpose?

AXCom, which stores data in the metadata database for tasks to push and pull asynchronously.
BVariables, which store global configuration values accessible by all tasks.
CPools, which limit concurrent task execution but do not share data.
DConnections, which store credentials for external systems.
Attempts:
2 left
💡 Hint

Think about a feature that allows tasks to exchange data during DAG execution.

✅ Best Practice
expert
3:00remaining
What is the best practice when using XCom for task communication in Airflow?

When using XCom to share data between tasks, which practice helps avoid common issues?

AAvoid specifying keys when pushing data to XCom to reduce complexity.
BPush large files directly into XCom to simplify data sharing.
CUse environment variables to store sensitive data instead of XCom.
DKeep the data small and JSON-serializable to prevent database bloat and serialization errors.
Attempts:
2 left
💡 Hint

Consider how XCom stores data and what limitations it has.

Practice

(1/5)
1. What is the main purpose of XCom in Airflow?
easy
A. To store large files generated by tasks
B. To schedule tasks at specific times
C. To monitor the health of Airflow workers
D. To enable tasks to share small pieces of data between each other

Solution

  1. Step 1: Understand XCom's role in Airflow

    XCom is designed to let tasks exchange small data easily during a workflow.
  2. Step 2: Compare options with XCom's purpose

    Scheduling tasks, monitoring workers, or storing large files are not XCom's functions.
  3. Final Answer:

    To enable tasks to share small pieces of data between each other -> Option D
  4. Quick Check:

    XCom = task data sharing [OK]
Hint: XCom is for small data sharing between tasks [OK]
Common Mistakes:
  • Confusing XCom with scheduling features
  • Thinking XCom stores large files
  • Mixing XCom with monitoring tools
2. Which Airflow method is used to send data from one task to another using XCom?
easy
A. xcom_pull()
B. xcom_push()
C. send_data()
D. push_xcom()

Solution

  1. Step 1: Identify the method to send data in XCom

    The method to send data is called xcom_push().
  2. Step 2: Verify other options

    xcom_pull() is for receiving data, and the others are not valid Airflow methods.
  3. Final Answer:

    xcom_push() -> Option B
  4. Quick Check:

    Send data = xcom_push() [OK]
Hint: Push data with xcom_push(), pull with xcom_pull() [OK]
Common Mistakes:
  • Confusing xcom_push() with xcom_pull()
  • Using non-existent methods like send_data()
  • Mixing method names
3. Given the following code snippet in Airflow tasks:
def task_a(ti):
    ti.xcom_push(key='result', value=42)

def task_b(ti):
    value = ti.xcom_pull(key='result', task_ids='task_a')
    print(value)

What will task_b print when executed?
medium
A. 42
B. None
C. task_a
D. Error

Solution

  1. Step 1: Understand xcom_push and xcom_pull usage

    task_a pushes the value 42 with key 'result'. task_b pulls this value using the same key and task_id.
  2. Step 2: Determine the printed output

    Since task_b pulls the value 42 correctly, it will print 42.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    Push 42, pull 42 = print 42 [OK]
Hint: Pull what was pushed with matching key and task_id [OK]
Common Mistakes:
  • Expecting None if keys don't match
  • Confusing task_ids parameter
  • Assuming print outputs task name
4. In the code below, why does task_b print None instead of the expected value?
def task_a(ti):
    ti.xcom_push(key='data', value='hello')

def task_b(ti):
    value = ti.xcom_pull(key='result', task_ids='task_a')
    print(value)
medium
A. xcom_pull requires a different method signature
B. The task_ids parameter is incorrect
C. The key used in xcom_pull does not match the key used in xcom_push
D. xcom_push cannot send strings

Solution

  1. Step 1: Compare keys used in push and pull

    task_a pushes with key 'data', but task_b pulls with key 'result'. This mismatch causes None to be returned.
  2. Step 2: Verify other parameters

    task_ids is correct, xcom_push supports strings, and method signature is valid.
  3. Final Answer:

    The key used in xcom_pull does not match the key used in xcom_push -> Option C
  4. Quick Check:

    Matching keys needed for XCom data [OK]
Hint: Keys must match exactly in push and pull [OK]
Common Mistakes:
  • Ignoring key name mismatch
  • Assuming xcom_push can't send strings
  • Changing task_ids unnecessarily
5. You want to pass a list of numbers from task_a to task_b using XCom. Which approach correctly enables this communication?
hard
A. In task_a: ti.xcom_push(key='numbers', value=[1, 2, 3])
In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_a')
B. In task_a: ti.xcom_push(key='numbers', value='1,2,3')
In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_b')
C. In task_a: ti.xcom_push(key='data', value=[1, 2, 3])
In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_a')
D. In task_a: ti.xcom_push(value=[1, 2, 3])
In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_a')

Solution

  1. Step 1: Check key and value consistency

    In task_a: ti.xcom_push(key='numbers', value=[1, 2, 3])
    In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_a') uses the same key 'numbers' in push and pull, and passes a list correctly.
  2. Step 2: Verify task_ids and value types

    Other options fail: one pushes a string '1,2,3' instead of list and pulls from wrong task_id 'task_b'; another mismatches keys 'data' vs 'numbers'; another omits key in push (defaults to 'return_value') but uses key='numbers' in pull.
  3. Final Answer:

    In task_a: ti.xcom_push(key='numbers', value=[1, 2, 3])
    In task_b: nums = ti.xcom_pull(key='numbers', task_ids='task_a')
    -> Option A
  4. Quick Check:

    Matching keys and task_ids with correct value type [OK]
Hint: Use same key and task_id, push list directly [OK]
Common Mistakes:
  • Mismatching keys between push and pull
  • Pulling from wrong task_id
  • Omitting key parameter in push or pull
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard