Skip to main content

18. What are XComs in Apache Airflow?

Imagine your workflow has two tasks:

Download Customer Data
โ†“
Generate Customer Report

The second task needs to know where the downloaded file is stored.

Instead of saving this information in a database or file, Airflow provides XCom (Cross Communication).

XCom allows tasks to exchange small pieces of data.

Examples include:

  • File paths
  • Record counts
  • IDs
  • Status messages
  • API responses (small)

Exampleโ€‹

def extract():
return "customers.csv"

The returned value is automatically pushed into XCom.

Another task can retrieve it.

filename = ti.xcom_pull(task_ids="extract_task")

Interview Tipโ€‹

XCom is designed for small metadata, not large datasets.

Avoid storing large DataFrames or files in XCom.


19. What is the difference between XCom Push and XCom Pull?

Think of two coworkers.

Employee A finishes a report.

Employee B needs that report.

Employee A Pushes the report.

Employee B Pulls the report.

Exactly the same concept exists in Airflow.

XCom Pushโ€‹

Stores information.

ti.xcom_push(
key="total_sales",
value=2500
)

XCom Pullโ€‹

Retrieves information.

sales = ti.xcom_pull(
task_ids="calculate_sales",
key="total_sales"
)

Real-World Exampleโ€‹

Download File
โ†“
Push File Name
โ†“
Read File Name
โ†“
Load Database

20. What is BranchPythonOperator?

Sometimes your workflow should follow different execution paths depending on a condition.

Example:

Receive Sales Data
โ†“
Sales > 0 ?
/ \
Yes No
โ†“ โ†“
Process Send Alert

BranchPythonOperator allows you to implement this logic.

Exampleโ€‹

from airflow.operators.python import BranchPythonOperator

def choose_path():
return "process_task"

branch = BranchPythonOperator(
task_id="branch_task",
python_callable=choose_path
)

The function returns the task ID that should execute next.


21. What is ShortCircuitOperator?

ShortCircuitOperator is used when you want to stop the remaining workflow if a condition is not satisfied.

Example:

Check File Exists
โ†“
True?
|
Yes
โ†“
Process File

False
โ†“
Stop Workflow

Exampleโ€‹

from airflow.operators.python import ShortCircuitOperator

def check_file():
return True

task = ShortCircuitOperator(
task_id="check_file",
python_callable=check_file
)

If the function returns False, downstream tasks are skipped.


22. What are Trigger Rules in Apache Airflow?

By default, a task runs only when all upstream tasks succeed.

However, production workflows often require more flexibility.

Airflow provides Trigger Rules.

Some common trigger rules are:

Trigger RuleDescription
all_successRun only if all upstream tasks succeed
all_failedRun only if all upstream tasks fail
one_successRun if at least one task succeeds
one_failedRun if any upstream task fails
all_doneRun regardless of success or failure

Real-World Exampleโ€‹

Suppose three data sources are loaded.

Sales
Inventory
Customers
โ†“
Generate Report

Even if one source fails, you might still want to generate a partial report.

Using one_success or all_done makes this possible.


23. What is the TaskFlow API?

Before Airflow 2.x, DAGs required a lot of boilerplate code.

TaskFlow API simplifies DAG development using Python decorators.

Traditional approach:

task = PythonOperator(
task_id="hello",
python_callable=my_function
)

TaskFlow approach:

from airflow.decorators import task

@task
def hello():
print("Hello Airflow")

This style is cleaner and easier to maintain.


24. What is the @task decorator?

The @task decorator converts a Python function into an Airflow task.

Example:

from airflow.decorators import task

@task
def calculate_total():
return 100

Benefits include:

  • Less boilerplate code
  • Automatic XCom support
  • Better readability
  • Easier testing
  • Improved maintainability

25. What is Dynamic Task Mapping?

Imagine your company receives reports from 200 stores every day.

Instead of writing 200 separate tasks:

Store1
Store2
Store3
...
Store200

Airflow can automatically create tasks dynamically.

Example:

@task
def process_store(store):
print(store)

process_store.expand(
store=["Store1", "Store2", "Store3"]
)

Airflow automatically creates one task for each store.

This greatly reduces duplicate code.


26. What is Dynamic DAG Generation?

Sometimes organizations have hundreds of similar workflows.

Instead of manually creating hundreds of DAG files, you can generate them dynamically.

Example:

Suppose you have:

USA
Canada
India
Germany
Australia

Instead of writing five DAGs manually, a single Python script can generate all five.

Advantagesโ€‹

  • Less duplicate code
  • Easier maintenance
  • Consistent workflow structure
  • Faster onboarding of new pipelines

Summaryโ€‹

QuestionTopic
18XCom
19XCom Push & Pull
20BranchPythonOperator
21ShortCircuitOperator
22Trigger Rules
23TaskFlow API
24@task Decorator
25Dynamic Task Mapping
26Dynamic DAG Generation

Interview Tipโ€‹

A common interview question is:

"When should you use XCom instead of a database?"

A simple answer:

  • Use XCom for small pieces of metadata, such as file names, IDs, timestamps, or status values.
  • Use a database, cloud storage, or data lake for large datasets, files, or DataFrames.

Keeping XCom lightweight improves Airflow's performance and keeps the metadata database efficient.