ML Pipeline Dependencies & Orchestration
“Managing complex ML workflows with thousands of interdependent tasks.”
TL;DR
ML pipeline orchestration manages complex DAGs of interdependent tasks with scheduling, retries, monitoring, and backfilling. Airflow is the industry standard, defining pipelines as Python code with cron-based scheduling and automatic retries. Key dependency patterns include linear, fan-out (parallel training), fan-in (merging), and diamond structures. Every task must be idempotent for safe retries. Feature stores like Feast ensure consistency between offline training and online serving features. For the data processing layer underneath orchestration, see batch processing pipelines, and for monitoring the deployed models, see model monitoring systems.

1. Why ML Pipeline Orchestration Matters
Machine Learning workflows are complex DAGs (Directed Acyclic Graphs) with dependencies:
Data Ingestion → Feature Engineering → Training → Evaluation → Deployment
↓ ↓ ↓
Validation Feature Store Model Registry
Challenges:
- Dependencies: Training depends on feature engineering completing successfully.
- Scheduling: Run daily at 2 AM, or trigger when new data arrives.
- Retry Logic: If a task fails due to transient error, retry 3 times.
- Monitoring: Alert if any step takes > 2 hours.
- Backfilling: Re-run pipeline for historical dates.
2. Apache Airflow - The Industry Standard
Airflow models workflows as DAGs (Directed Acyclic Graphs) of tasks.
The examples use Airflow 2 import paths. Airflow 3 (released April 2025) keeps the same concepts but moves DAG authoring imports to airflow.sdk, moves core operators such as PythonOperator into provider packages, and removes schedule_interval and days_ago (both replaced below).
Example: Training Pipeline DAG
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime, timedelta
default_args = {
'owner': 'ml-team',
'retries': 3,
'retry_delay': timedelta(minutes=5),
'email_on_failure': True,
'email': ['alerts@company.com']
}
dag = DAG(
'ml_training_pipeline',
default_args=default_args,
description='Daily model training',
schedule='0 2 * * *', # 2 AM daily
start_date=datetime(2024, 1, 1),
catchup=False
)
def extract_data(**context):
# Pull data from warehouse
data = query_warehouse(context['ds']) # ds = execution date
save_to_gcs(data, f"raw_data_{context['ds']}.parquet")
def feature_engineering(**context):
data = load_from_gcs(f"raw_data_{context['ds']}.parquet")
features = compute_features(data)
save_to_gcs(features, f"features_{context['ds']}.parquet")
def train_model(**context):
features = load_from_gcs(f"features_{context['ds']}.parquet")
model = train(features)
save_model(model, f"model_{context['ds']}.pkl")
def evaluate_model(**context):
model = load_model(f"model_{context['ds']}.pkl")
metrics = evaluate(model, test_data)
if metrics['auc'] < 0.75:
raise ValueError("Model quality below threshold!")
log_metrics(metrics)
def deploy_model(**context):
model_path = f"model_{context['ds']}.pkl"
deploy_to_production(model_path)
# Define tasks (the *_task names avoid shadowing train() and evaluate() above)
extract_task = PythonOperator(task_id='extract_data', python_callable=extract_data, dag=dag)
feature_task = PythonOperator(task_id='feature_engineering', python_callable=feature_engineering, dag=dag)
train_task = PythonOperator(task_id='train_model', python_callable=train_model, dag=dag)
evaluate_task = PythonOperator(task_id='evaluate_model', python_callable=evaluate_model, dag=dag)
deploy_task = PythonOperator(task_id='deploy_model', python_callable=deploy_model, dag=dag)
# Dependencies (topological order)
extract_task >> feature_task >> train_task >> evaluate_task >> deploy_task
Key Features:
- Scheduling: Cron-like syntax (
schedule). - Backfilling: Re-run for past dates (
airflow dags backfillin Airflow 2,airflow backfill createin Airflow 3). - Retries: Automatic retries, with exponential backoff as an opt-in setting.
- Monitoring: Web UI shows task status, logs, duration.
3. Kubeflow Pipelines (Kubernetes-Native)
Kubeflow runs ML pipelines on Kubernetes.
Benefits:
- Scalability: Auto-scale workers on K8s.
- Reproducibility: Each task runs in a Docker container.
- GPU Support: Easily request GPU resources.
Example: Training Pipeline
Each step runs in its own container, so steps exchange data as artifacts (files in object storage that KFP tracks) rather than local paths. This uses the KFP v2 SDK:
import kfp
from kfp import compiler, dsl
from kfp.dsl import Dataset, Input, Model, Output
@dsl.component(base_image='python:3.11', packages_to_install=['pandas', 'pyarrow', 'sqlalchemy'])
def data_ingestion(db_url: str, raw: Output[Dataset]):
import pandas as pd
df = pd.read_sql("SELECT * FROM users", db_url)
df.to_parquet(raw.path)
@dsl.component(base_image='python:3.11', packages_to_install=['pandas', 'pyarrow'])
def feature_engineering(raw: Input[Dataset], features: Output[Dataset]):
import pandas as pd
df = pd.read_parquet(raw.path)
df['age_squared'] = df['age'] ** 2 # Engineer features
df.to_parquet(features.path)
@dsl.component(base_image='tensorflow/tensorflow:latest-gpu')
def train_model(features: Input[Dataset], model: Output[Model]):
import tensorflow as tf
# ... load features.path, build and fit a Keras model ...
keras_model.save(model.path + '.keras')
@dsl.pipeline(name='ml-training-pipeline')
def ml_pipeline(db_url: str):
data_task = data_ingestion(db_url=db_url)
feature_task = feature_engineering(raw=data_task.outputs['raw'])
train_task = train_model(features=feature_task.outputs['features'])
train_task.set_accelerator_type('nvidia.com/gpu').set_accelerator_limit(1) # Request 1 GPU
# Compile and run
compiler.Compiler().compile(ml_pipeline, 'pipeline.yaml')
client = kfp.Client()
client.create_run_from_pipeline_func(ml_pipeline, arguments={'db_url': 'postgresql://...'})
Advantages:
- Containerization: Each step is isolated.
- Resource Management: Request specific CPU/GPU/memory.
- Artifact Tracking: Automatic versioning of data, models.
4. Dependency Patterns in ML Pipelines
Pattern 1: Linear Pipeline
A → B → C → D
Example: Data Ingestion → Preprocessing → Training → Deployment.
Pattern 2: Fan-Out (Parallel Tasks)
→ B1 →
A → → B2 → → D
→ B3 →
Example: Train 3 models in parallel, then ensemble them.
# Airflow
feature_eng >> [train_model_1, train_model_2, train_model_3] >> ensemble >> deploy
Pattern 3: Fan-In (Join)
A1 →
A2 → → C
A3 →
Example: Process data from 3 sources, then merge.
Pattern 4: Diamond (Complex Dependencies)
→ B →
A → → D
→ C →
Example: Extract features from images (B) and text (C), then combine for training (D).
5. Handling Failures and Retries
Transient vs. Persistent Failures
Transient: Network timeout, database busy.
- Solution: Retry with exponential backoff.
Persistent: Bug in code, missing data.
- Solution: Alert humans, don’t retry forever.
Retry Strategy
from datetime import timedelta
from prefect import flow
# Airflow
default_args = {
'retries': 3,
'retry_delay': timedelta(minutes=5),
'retry_exponential_backoff': True,
'max_retry_delay': timedelta(hours=1)
}
# Prefect
@flow(retries=3, retry_delay_seconds=300)
def my_flow():
pass
Idempotency
Critical: Tasks must be idempotent (running twice = running once).
Bad (not idempotent):
def bad_task():
db.execute("INSERT INTO results VALUES (...)") # Duplicate inserts!
Good (idempotent): replace the whole partition for the run’s date, in one transaction, with bound parameters rather than string formatting.
def good_task(**context):
date = context['ds']
with db.begin(): # no window where the partition is half-written
db.execute("DELETE FROM results WHERE date = %s", (date,))
db.execute("INSERT INTO results SELECT ... FROM staging WHERE date = %s", (date,))
6. Dynamic DAG Generation
Problem: You have 100 models to train daily. Don’t want to write 100 tasks manually.
Solution: Programmatically generate DAGs.
# Airflow
from airflow import DAG
from airflow.operators.python import PythonOperator
models = ['model_A', 'model_B', 'model_C', ...]
dag = DAG('dynamic_training', ...)
for model_name in models:
task = PythonOperator(
task_id=f'train_{model_name}',
python_callable=train_model,
op_kwargs={'model_name': model_name},
dag=dag
)
Deep Dive: A Netflix-Style Stack with Metaflow
Netflix open-sourced Metaflow, its framework for data science workflows, and runs it on its own container platform, Titus. A stack in that style looks like this:
Architecture:
- Metaflow (Netflix’s open-source tool):
- Simplifies Airflow/Kubernetes complexity.
- Auto-versioning of data/model artifacts.
- S3 for Data Lake.
- Spark for Feature Engineering.
- TensorFlow/PyTorch for Training.
- Titus (Netflix’s container platform) for Execution.
Example Metaflow Pipeline:
from metaflow import FlowSpec, step, batch, resources
class RecommendationFlow(FlowSpec):
@step
def start(self):
self.data = load_from_s3('s3://my-bucket/user_views.parquet')
self.next(self.feature_engineering)
@batch(cpu=16, memory=64000) # 16 CPUs, 64GB RAM
@step
def feature_engineering(self):
self.features = compute_features(self.data)
self.next(self.train)
@batch(gpu=4, memory=128000) # 4 GPUs
@step
def train(self):
self.model = train_model(self.features)
self.next(self.evaluate)
@step
def evaluate(self):
self.metrics = evaluate(self.model)
self.next(self.deploy)
@step
def deploy(self):
# Metaflow graphs are static, so the quality gate lives inside the step
if self.metrics['precision@10'] > 0.8:
deploy_to_production(self.model) # Don't deploy bad models
self.next(self.end)
@step
def end(self):
print("Pipeline complete!")
if __name__ == '__main__':
RecommendationFlow()
Deep Dive: Uber’s Michelangelo Platform
Michelangelo is Uber’s end-to-end ML platform, described publicly on Uber’s engineering blog.
Pipeline Components:
- Data Sources: Ride data, driver data, geospatial data.
- Feature Store: Pre-computed features (e.g., “average rides per hour in this area”).
- Training: Distributed training on Spark/Horovod.
- Model Serving: Deploy to Uber’s serving infrastructure.
- Monitoring: Track prediction accuracy, latency.
Scheduling:
- Batch: Models retrained on a schedule, with features computed by batch jobs.
- Streaming: Features computed from event streams for near-real-time predictions.
Dependency Management:
- A workflow orchestrator schedules the feature pipelines and retraining jobs.
- Feature engineering depends on multiple data sources joining correctly.
Deep Dive: Feature Store Integration
Problem: Feature computation is expensive. Don’t recompute for every model.
Solution: Feature Store (Feast, Tecton)
Architecture:
Batch Pipeline (Airflow) → Compute Features → Feature Store (Online + Offline)
↓
Training & Serving
Integration with Airflow (current Feast FeatureStore API):
from airflow.decorators import task
from feast import FeatureStore
from feast.data_source import PushMode
store = FeatureStore(repo_path='feature_repo')
@task
def compute_features(**context):
raw_data = load_data(context['ds'])
features = transform(raw_data) # DataFrame: user_id, event_timestamp, feature columns
# Write to the online and offline stores through a push source
store.push('user_profile_push_source', features, to=PushMode.ONLINE_AND_OFFLINE)
@task
def train_model(**context):
entity_df = load_labels(context['ds']) # user_id, event_timestamp, label
# Point-in-time correct join: each row gets feature values as of its timestamp
training_df = store.get_historical_features(
entity_df=entity_df,
features=['user_profile:age', 'user_profile:country'],
).to_df()
model = train(training_df)
Deep Dive: Pipeline Testing and Validation
Unit Tests: Test individual tasks.
def test_feature_engineering():
input_data = pd.DataFrame({'age': [25, 30, 35]})
output = feature_engineering(input_data)
assert 'age_squared' in output.columns
assert output['age_squared'].tolist() == [625, 900, 1225]
Integration Tests: Test full pipeline on sample data.
def test_pipeline_end_to_end():
# Run pipeline with small test dataset
result = run_pipeline(test_data)
assert result['model_quality'] > 0.7
Data Validation: Check data quality before training.
import great_expectations as gx
from airflow.decorators import task
@task
def validate_data(**context):
data = load_data(context['ds'])
# Cheap inline checks (raise, not assert: asserts vanish under python -O)
if not data['age'].between(0, 120).all():
raise ValueError("Invalid ages!")
if data['revenue'].isnull().any():
raise ValueError("Missing revenue!")
# Great Expectations (1.x API) for richer suites
gx_context = gx.get_context()
checkpoint = gx_context.checkpoints.get('data_quality_checkpoint')
results = checkpoint.run(batch_parameters={'dataframe': data})
if not results.success:
raise ValueError("Data quality check failed!")
Deep Dive: Backfilling Historical Data
Problem: You fixed a bug in feature engineering. Need to re-run for the past 90 days.
Airflow Backfill:
# Airflow 2
airflow dags backfill \
--start-date 2024-01-01 \
--end-date 2024-03-31 \
ml_training_pipeline
# Airflow 3 (backfills are run by the scheduler)
airflow backfill create --dag-id ml_training_pipeline \
--from-date 2024-01-01 --to-date 2024-03-31
Challenges:
- Resource Limits: Running 90 days worth of jobs simultaneously can overwhelm infrastructure.
- Solution: Use
max_active_runsparameter to limit parallelism.
dag = DAG(
'ml_pipeline',
schedule='@daily',
start_date=datetime(2024, 1, 1),
max_active_runs=5, # Run max 5 dates in parallel
)
Deep Dive: Monitoring and Alerting
Metrics to Track:
- Task Duration: Alert if task takes > 2x usual time.
- Task Failure Rate: Alert if > 5% failure rate.
- Data Volume: Alert if input data is 50% smaller than yesterday (possible upstream issue).
- Model Quality: Alert if AUC drops below threshold.
Airflow Integration with Prometheus/Grafana (batch jobs push to a Pushgateway that Prometheus scrapes):
from airflow.decorators import task
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
@task
def push_metrics(model_auc: float, training_seconds: float, data_rows: int):
registry = CollectorRegistry()
values = {
'model_auc': model_auc,
'training_duration_seconds': training_seconds,
'data_rows': data_rows,
}
for name, value in values.items():
Gauge(name, f'ML pipeline {name}', registry=registry).set(value)
push_to_gateway('pushgateway:9091', job='ml_training_pipeline', registry=registry)
Deep Dive: Cost Optimization
Problem: Training 1000 models on GPUs is expensive.
Strategies:
- Spot Instances: Use AWS Spot / GCP Spot VMs (AWS advertises discounts of up to 90% versus on-demand, at the cost of possible interruption).
- Auto-Scaling: Scale down workers when idle.
- Resource Right-Sizing: Don’t request more CPU/RAM than needed.
- Caching: Cache intermediate results (features).
Airflow on Spot Instances:
from airflow.providers.amazon.aws.operators.ecs import EcsRunTaskOperator
train_task = EcsRunTaskOperator(
task_id='train_model',
task_definition='ml-training',
cluster='ml-cluster',
overrides={
'containerOverrides': [{
'name': 'training-container',
'environment': [...],
}],
'cpu': '4096', # 4 vCPUs
'memory': '16384', # 16GB
'taskRoleArn': 'arn:aws:iam::...',
},
# Fargate Spot is selected through a capacity provider, not launch_type
capacity_provider_strategy=[{'capacityProvider': 'FARGATE_SPOT', 'weight': 1}],
dag=dag
)
Implementation: Full ML Pipeline Orchestration
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.email import EmailOperator
from datetime import datetime, timedelta
import pandas as pd
default_args = {
'owner': 'ml-team',
'depends_on_past': False,
'email': ['ml-alerts@company.com'],
'email_on_failure': True,
'email_on_retry': False,
'retries': 2,
'retry_delay': timedelta(minutes=5),
}
dag = DAG(
'complete_ml_pipeline',
default_args=default_args,
description='End-to-end ML pipeline',
schedule='0 2 * * *', # 2 AM daily
start_date=datetime(2025, 1, 1),
catchup=False,
max_active_runs=1,
)
def data_quality_check(**context):
"""Validate data quality before processing"""
date = context['ds']
data = load_data(date)
# Checks (raise rather than assert, which python -O strips)
if len(data) <= 10000:
raise ValueError("Insufficient data!")
if data['label'].isnull().any():
raise ValueError("Missing labels!")
# Log stats
context['ti'].xcom_push(key='data_size', value=len(data))
def extract_features(**context):
"""Feature engineering"""
date = context['ds']
data = load_data(date)
features = engineer_features(data)
features.to_parquet(f'/data/features_{date}.parquet')
def train_models(**context):
"""Train several candidate models"""
date = context['ds']
features = pd.read_parquet(f'/data/features_{date}.parquet')
models = {}
for model_type in ['xgboost', 'random_forest', 'logistic_regression']:
model = train_model(features, model_type)
models[model_type] = model
save_model(model, f'/models/{model_type}_{date}.pkl')
# XCom values must be JSON-serializable, so push a list, not dict_keys
context['ti'].xcom_push(key='model_types', value=list(models))
def evaluate_and_select_best(**context):
"""Evaluate models and select the best"""
date = context['ds']
model_types = context['ti'].xcom_pull(task_ids='train_models', key='model_types')
best_model = None
best_auc = 0
for model_type in model_types:
model = load_model(f'/models/{model_type}_{date}.pkl')
auc = evaluate(model, test_data)
if auc > best_auc:
best_auc = auc
best_model = model_type
# Quality gate
if best_auc < 0.75:
raise ValueError(f"Best model AUC ({best_auc}) below threshold!")
context['ti'].xcom_push(key='best_model', value=best_model)
context['ti'].xcom_push(key='best_auc', value=best_auc)
def deploy_best_model(**context):
"""Deploy the best model to production"""
best_model = context['ti'].xcom_pull(task_ids='evaluate_and_select_best', key='best_model')
date = context['ds']
model_path = f'/models/{best_model}_{date}.pkl'
deploy_to_production(model_path)
print(f"Deployed {best_model} to production!")
# Define tasks
quality_task = PythonOperator(task_id='data_quality_check', python_callable=data_quality_check, dag=dag)
feature_task = PythonOperator(task_id='extract_features', python_callable=extract_features, dag=dag)
train_task = PythonOperator(task_id='train_models', python_callable=train_models, dag=dag)
evaluate_task = PythonOperator(task_id='evaluate_and_select_best', python_callable=evaluate_and_select_best, dag=dag)
deploy_task = PythonOperator(task_id='deploy_best_model', python_callable=deploy_best_model, dag=dag)
success_email = EmailOperator(
task_id='success_email',
to='ml-team@company.com',
subject='ML Pipeline Success - {{ ds }}',
html_content='Pipeline completed successfully. Best AUC: {{ ti.xcom_pull(task_ids=\'evaluate_and_select_best\', key=\'best_auc\') }}',
dag=dag
)
# Dependencies
quality_task >> feature_task >> train_task >> evaluate_task >> deploy_task >> success_email
Top Interview Questions
Q1: How do you handle a task that depends on multiple upstream tasks?
Answer:
Use list syntax in Airflow: [task1, task2, task3] >> downstream_task. All must complete successfully before downstream runs.
Q2: What’s the difference between schedule and start_date?
Answer:
start_date is the earliest date the scheduler considers. schedule (called schedule_interval before Airflow 2.4) sets how often the DAG runs. With Airflow 2’s default data-interval timetables, the run for an interval starts when that interval ends, so the first run fires one interval after start_date. Check the timetable your version uses, because this behavior is configurable.
Q3: How do you pass data between tasks? Answer:
- Small data: XCom (Airflow’s inter-task communication).
- Large data: Write to shared storage (S3, GCS) and pass the path via XCom.
Q4: How do you handle long-running tasks (> 24 hours)?
Answer:
Use sensors or split into smaller tasks. Or use execution_timeout parameter to fail gracefully if task hangs.
Key Takeaways
- DAG Structure: ML pipelines are DAGs with dependencies modeled via topological sort.
- Airflow is a Common Default: Apache Airflow is widely used for batch orchestration; Kubeflow, Metaflow, Prefect and Dagster cover other niches.
- Idempotency is Critical: Tasks must be safe to re-run.
- Monitoring: Track task duration, failure rate, data quality.
- Cost Optimization: Use spot instances, auto-scaling, caching.
Summary
| Aspect | Insight |
|---|---|
| Core Problem | Orchestrate complex ML workflows with dependencies |
| Best Tools | Airflow (general), Kubeflow (K8s), Metaflow (Netflix) |
| Key Patterns | Linear, Fan-Out, Fan-In, Diamond |
| Critical Features | Scheduling, retries, monitoring, backfilling |
FAQ
What is the difference between Airflow, Kubeflow, and Metaflow?
Airflow is the general-purpose industry standard for workflow orchestration with Python-defined DAGs and cron scheduling. Kubeflow runs ML pipelines natively on Kubernetes with containerized tasks, GPU support, and artifact tracking. Metaflow (Netflix) simplifies the complexity of Airflow and Kubernetes with automatic data versioning and decorator-based resource allocation. Choose Airflow for general pipelines, Kubeflow for Kubernetes-native ML, and Metaflow for data science workflows.
How do you handle task failures in ML pipelines?
Distinguish transient failures (network timeouts, database busy) from persistent failures (code bugs, missing data). For transient failures, configure automatic retries with exponential backoff. For persistent failures, alert humans and do not retry indefinitely. All tasks must be idempotent so that retries produce the same result as a single execution. Use INSERT OVERWRITE instead of INSERT INTO to prevent data duplication.
How do you pass data between pipeline tasks?
For small data like metrics or file paths, use XCom (Airflow’s inter-task communication). For large data like datasets or model artifacts, write to shared storage (S3, GCS) and pass only the path via XCom. Never pass large DataFrames through XCom as it stores data in the metadata database and causes performance issues.
How do you backfill historical data after fixing a pipeline bug?
Use Airflow’s backfill command to re-run the DAG for a range of historical dates. Set max_active_runs to limit parallelism (e.g., 5 concurrent runs) to avoid overwhelming infrastructure. Each task must be idempotent so that backfilling overwrites old results cleanly. For large backfills spanning months, process in batches to manage resource consumption.