Complex Multi-DAG Orchestration
Architecture Diagram
Formal Definitions
Detailed Explanation
Multi-DAG Orchestration Patterns
| Pattern | Mechanism | Wait Type | Complexity | Best For |
|---|---|---|---|---|
| TriggerDagRunOperator | Creates child dag_run | Blocking or non-blocking | Low | Simple parent-child |
| ExternalTaskSensor | Polls child task state | Reschedule mode recommended | Medium | Implicit dependencies |
| Callback-based | DAG failure/success hooks | Asynchronous notification | Medium | Alerting and logging |
| Dynamic DAGs | Code-generated dependencies | Variable | High | Multi-team coordination |
| Fan-Out/Fan-In | Parallel triggers + consolidated wait | Parallel with barrier | Medium | Multi-region/multi-table |
When to Use Each Pattern
- TriggerDagRunOperator: When you need to trigger a child DAG and optionally wait for completion
- ExternalTaskSensor: When you need to wait for a specific task in another DAG to complete
- Callbacks: When you need to be notified of child DAG success/failure without polling
- Fan-Out/Fan-In: When you need to process multiple datasets in parallel and consolidate results
Pattern 1: TriggerDagRunOperator
The simplest way to invoke a child DAG from a parent. The parent triggers the child and can optionally wait for completion.
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.trigger_dagrun import TriggerDagRunOperator
from airflow.operators.python import PythonOperator
with DAG(
dag_id='parent_orchestrator',
start_date=datetime(2024, 1, 1),
schedule_interval='@daily',
catchup=False,
tags=['orchestration', 'parent'],
) as parent_dag:
def preprocess(**context):
"""Prepare data before child DAG execution."""
context['ti'].xcom_push(key='record_count', value=15000)
context['ti'].xcom_push(key='source_date', value=context['ds'])
trigger_etl = TriggerDagRunOperator(
task_id='trigger_etl_pipeline',
trigger_dag_id='child_etl_pipeline',
conf={
'source_date': '{{ ds }}',
'record_count': '{{ ti.xcom_pull(task_ids="preprocess", key="record_count") }}',
},
wait_for_completion=True,
execution_timeout=timedelta(hours=2),
)
trigger_quality = TriggerDagRunOperator(
task_id='trigger_data_quality',
trigger_dag_id='child_data_quality',
conf={'source_date': '{{ ds }}'},
wait_for_completion=True,
)
def post_process(**context):
"""Run after all children complete."""
import logging
logger = logging.getLogger(__name__)
logger.info('All child DAGs completed successfully')
process = PythonOperator(task_id='preprocess', python_callable=preprocess)
post = PythonOperator(task_id='post_process', python_callable=post_process)
process >> trigger_etl >> trigger_quality >> post
Pattern 2: ExternalTaskSensor
Wait for a specific task in another DAG to reach a terminal state. This creates implicit inter-DAG dependencies.
from datetime import datetime, timedelta
from airflow import DAG
from airflow.sensors.external_task import ExternalTaskSensor
from airflow.operators.python import PythonOperator
with DAG(
dag_id='reporting_pipeline',
start_date=datetime(2024, 1, 1),
schedule_interval='0 6 * * *',
catchup=False,
tags=['reporting'],
) as report_dag:
wait_for_etl = ExternalTaskSensor(
task_id='wait_for_etl_complete',
external_dag_id='etl_pipeline',
external_task_id='load_to_warehouse',
execution_delta=timedelta(hours=1),
timeout=timedelta(hours=4).total_seconds(),
mode='reschedule',
poke_interval=300,
)
wait_for_quality = ExternalTaskSensor(
task_id='wait_for_quality_checks',
external_dag_id='data_quality_checks',
external_task_id='validate_tables',
execution_delta=timedelta(hours=1),
timeout=timedelta(hours=4).total_seconds(),
mode='reschedule',
)
generate_reports = PythonOperator(
task_id='generate_reports',
python_callable=lambda: print('Generating reports...'),
)
[wait_for_etl, wait_for_quality] >> generate_reports
Pattern 3: Cross-DAG XCom with Dynamic DAGs
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator
def check_downstream_status(**context):
"""Query metadata DB for downstream DAG status."""
from airflow.models import DagRun, TaskInstance
from airflow import settings
from airflow.utils.state import State
session = settings.Session()
downstream_dags = ['etl_pipeline', 'quality_checks', 'reporting']
statuses = {}
for dag_id in downstream_dags:
runs = session.query(DagRun).filter(
DagRun.dag_id == dag_id,
DagRun.execution_date >= context['execution_date'] - timedelta(hours=1),
).all()
statuses[dag_id] = {
'run_count': len(runs),
'latest_state': runs[-1].state if runs else 'no_runs',
}
context['ti'].xcom_push(key='downstream_statuses', value=statuses)
return statuses
with DAG(
dag_id='multi_dag_monitor',
start_date=datetime(2024, 1, 1),
schedule_interval='*/15 * * * *',
catchup=False,
tags=['monitoring'],
) as monitor_dag:
check = PythonOperator(
task_id='check_downstream_status',
python_callable=check_downstream_status,
)
alert = SlackWebhookOperator(
task_id='alert_on_failure',
slack_webhook_conn_id='slack_webhook',
message=(
'Downstream DAGs status:\n'
'{{ ti.xcom_pull(task_ids="check_downstream_status", key="downstream_statuses") }}'
),
)
check >> alert
Pattern 4: Callback-Driven Coordination
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python import PythonOperator
import json
import logging
logger = logging.getLogger(__name__)
def on_child_failure(context):
"""Callback triggered when child DAG fails."""
dag_run = context.get('dag_run')
task_instance = context.get('task_instance')
logger.error(
f'Child DAG failed: {dag_run.dag_id}, '
f'run_id: {dag_run.run_id}'
)
# Send alert
from airflow.providers.slack.hooks.slack_webhook import SlackWebhookHook
hook = SlackWebhookHook(slack_webhook_conn_id='slack_webhook')
hook.send(
f'Child DAG Failure Alert\n'
f'DAG: {dag_run.dag_id}\n'
f'Run: {dag_run.run_id}\n'
f'Task: {task_instance.task_id}'
)
def on_child_success(context):
"""Callback triggered when child DAG succeeds."""
dag_run = context.get('dag_run')
logger.info(f'Child DAG completed: {dag_run.dag_id}')
with DAG(
dag_id='callback_orchestrator',
start_date=datetime(2024, 1, 1),
schedule_interval='@daily',
catchup=False,
tags=['callbacks'],
) as callback_dag:
from airflow.operators.trigger_dagrun import TriggerDagRunOperator
trigger_pipeline = TriggerDagRunOperator(
task_id='trigger_pipeline',
trigger_dag_id='data_pipeline',
on_success_callback=on_child_success,
on_failure_callback=on_child_failure,
wait_for_completion=True,
)
trigger_pipeline
Pattern 5: Fan-Out / Fan-In with Multiple DAGs
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.trigger_dagrun import TriggerDagRunOperator
from airflow.sensors.external_task import ExternalTaskSensor
from airflow.operators.python import PythonOperator
with DAG(
dag_id='fanout_fanin_orchestrator',
start_date=datetime(2024, 1, 1),
schedule_interval='@daily',
catchup=False,
tags=['fanout', 'fanin'],
) as orchestrator:
# Fan-out: trigger multiple child DAGs in parallel
regions = ['us-east-1', 'eu-west-1', 'ap-southeast-1']
triggers = []
for region in regions:
trigger = TriggerDagRunOperator(
task_id=f'trigger_{region.replace("-", "_")}',
trigger_dag_id='region_etl',
conf={'region': region},
wait_for_completion=False,
)
triggers.append(trigger)
# Fan-in: wait for all children to complete
sensors = []
for region in regions:
sensor = ExternalTaskSensor(
task_id=f'wait_{region.replace("-", "_")}',
external_dag_id='region_etl',
external_task_id='complete',
mode='reschedule',
poke_interval=60,
execution_delta=timedelta(minutes=5),
)
sensors.append(sensor)
consolidate = PythonOperator(
task_id='consolidate_results',
python_callable=lambda: print('All regions processed'),
)
triggers >> sensors >> consolidate
Where Cross-DAG Wiring Breaks
Two DAGs are parsed, scheduled and retried independently, so failures surface in the parent even when the fault sits in the child. The commonest break is an execution-date mismatch: ExternalTaskSensor looks up the child task instance at its own logical date, so a reporting DAG that runs at 06:00 waits for an ETL task stamped 06:00 while the ETL only ever runs at 02:00. execution_delta=timedelta(hours=4) shifts that lookup backwards, and for unrelated timetables a callback passed as execution_date_fn computes the expected date on each poke.
The second break is a child run that never exists. TriggerDagRunOperator writes a dag_run row that the scheduler picks up on its next pass, so a paused child, a mistyped trigger_dag_id, or an undeployed DAG file leaves the parent waiting until execution_timeout expires. The third break is asymmetric retries: a retried trigger starts a second child run while the first is still writing to the same tables, so give each run an idempotency key through conf.
| Symptom | Root cause | Fix |
|---|---|---|
| Sensor pokes for hours with no child log lines | logical dates do not line up | set execution_delta or supply execution_date_fn |
| Triggered run never appears in the Grid view | paused child, typo in trigger_dag_id | verify pause state and spelling before the trigger fires |
| Parent fails while the child is still healthy | parent execution_timeout shorter than child runtime | raise the timeout or use wait_for_completion=False plus a sensor |
| Child reprocesses the same date after a parent retry | no idempotency guard on the triggered run | pass a key through conf and skip work already done |
Timeout and Slot Budgets
A sensor in poke mode holds one worker slot for the whole wait. A four-hour ExternalTaskSensor at a 300-second poke_interval issues 48 pokes while occupying that slot, so ten parallel sensors can drain a small Celery deployment. In reschedule mode the slot is released after every poke and re-acquired at the next interval, costing one extra metadata query per cycle.
Budget a parent DAG around three numbers. timeout is the sensor's hard stop and should be at least twice the longest child runtime observed. execution_timeout on the trigger must exceed timeout plus the parent's remaining work, so the sensor fails first with a message that names the missing dependency instead of killing the parent mid-flight. retry_delay then decides how long the chain waits after that failure.
| Setting | Example value | What it controls |
|---|---|---|
poke_interval | 300 seconds | how often the sensor queries the metadata database |
timeout | 14400 seconds | when a waiting sensor declares the child lost |
execution_timeout | 16200 seconds | when the parent gives up on its own task |
Performance Metrics
| Metric | TriggerDagRun | ExternalTaskSensor | Callbacks |
|---|---|---|---|
| Latency to trigger | <1s | N/A (polling) | Immediate |
| Worker slot during wait | Released (if wait=False) | Held (unless reschedule) | N/A |
| Scalability | High | Medium (sensor overhead) | High |
| Error handling | Task failure | Sensor timeout | Event-driven |
Best Practices
Pattern Selection
- Use
mode='reschedule'on ExternalTaskSensor to avoid holding worker slots during waits. - Set
execution_deltaorexecution_date_fnto match the child DAG's execution schedule. - Implement
wait_for_completion=Trueon TriggerDagRunOperator when downstream tasks depend on child completion.
Communication and Monitoring
- Use callbacks for alerting and logging â they fire independently of task state transitions.
- Avoid deep inter-DAG chains â they create fragile, hard-to-debug dependencies.
- Use XCom sparingly across DAGs â prefer external state stores for large data.
Operational Guidelines
- Tag orchestration DAGs distinctly from child DAGs for monitoring clarity.
- Set
execution_timeouton sensors and triggers to prevent indefinite hangs.
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
| Deep dependency chains | Hard to debug, fragile | Keep to 2-3 levels max |
| Missing timeouts | Sensors hang indefinitely | Always set execution_timeout |
| Blocking sensors | Wastes worker slots | Use mode='reschedule' |
| XCom for large data | Database bloat | Use external storage (S3, GCS) |
See Also
- DAG Design Patterns â DAG composition and dependency patterns
- Scheduling and Triggers â Timetables and scheduling patterns
- Branching Logic â BranchPythonOperator and conditional workflows
- XCom Communications â Task communication and data passing