Course contentsShow
← Data Engineering
Lesson 394 of 8256. Orchestration and Workflow ManagementFree lesson

Passing Data Between Tasks Using XComs

Enable communication between independent tasks by pushing and pulling small metadata payloads through the Airflow internal database.

In this lesson, you will learn how to orchestrate data movement between independent tasks by utilizing XComs as a lightweight bridge for metadata.

The Role of XComs in Airflow

In a distributed workflow, tasks are isolated by design; they run on different workers and do not share memory. However, data engineering pipelines often require the output of one task (like a file path or a status flag) to influence the behavior of the next. XComs (short for "cross-communications") solve this by allowing tasks to push small pieces of metadata into the Airflow metadata database, where a downstream task can then pull and utilize that information.

How It Works

Think of an XCom as a shared "digital sticky note." When a task finishes, it can write a value to the metadata database. The subsequent task reads that specific entry during its own execution. Because this data is stored in the database, keep it small—use XComs for metadata, IDs, or configuration parameters, not for transferring large datasets (like Parquet files or DataFrames), which should reside in external storage like S3 or GCS.

Practical Implementation

When using the PythonOperator, you can return a value from your function; Airflow automatically pushes the return value to XComs.

def extract_metadata():
    # Return value is automatically pushed to XCom
    return {"file_path": "/data/landing/extract_01.csv", "row_count": 450}

def process_data(**context):
    # Pulling the metadata from the previous task
    task_instance = context['ti']
    metadata = task_instance.xcom_pull(task_ids='extract_task')
    print(f"Processing file at: {metadata['file_path']}")

# In the DAG definition:
# extract_task = PythonOperator(task_id='extract_task', python_callable=extract_metadata)
# process_task = PythonOperator(task_id='process_task', python_callable=process_data)

By referencing the task_id of the producer, the consumer can reliably retrieve the payload. This mechanism turns your DAG from a series of disconnected steps into a collaborative pipeline where metadata flows seamlessly to inform downstream decisions.

Key Takeaway: XComs act as the internal messaging system for Airflow, enabling you to pass small, critical metadata payloads between independent tasks without sacrificing the isolation required for robust orchestration.