Error Handling and Retry Strategies
Architecture Diagram
Formal Definitions
Detailed Explanation
Retry Configuration
Airflow provides built-in retry mechanisms for handling transient failures.
Retry Parameters:
| Parameter | Description | Example |
|---|---|---|
retries | Number of retry attempts | 3 |
retry_delay | Time between retries | timedelta(minutes=5) |
retry_exponential_backoff | Double delay each retry | True |
max_retry_delay | Maximum retry delay cap | timedelta(minutes=30) |
execution_timeout | Max task execution time | timedelta(hours=1) |
@task(
retries=3,
retry_delay=timedelta(minutes=5),
retry_exponential_backoff=True,
max_retry_delay=timedelta(minutes=30),
execution_timeout=timedelta(hours=1),
)
def flaky_api_call():
# Retries: 5min â 10min â 20min (capped at 30min)
pass
Callback Functions
Callbacks are invoked on specific task state transitions.
Callback Types:
| Callback | Trigger | Use Case |
|---|---|---|
on_failure_callback | Task fails | Send alerts, log errors |
on_success_callback | Task succeeds | Notify, update external systems |
on_retry_callback | Task retries | Log retry attempts |
on_execute_callback | Task starts | Initialize resources |
def failure_callback(context):
task_instance = context['task_instance']
exception = context['exception']
send_email(
to=['team@example.com'],
subject=f"Task Failed: {task_instance.task_id}",
html_content=f"Exception: {exception}",
)
SLA Configuration
SLAs define expected completion times for tasks.
SLA Parameters:
| Level | Setting | Effect |
|---|---|---|
| DAG-level | sla_miss_callback | Invoked when any task misses SLA |
| Task-level | sla=timedelta(hours=2) | Per-task SLA |
SLA Violation Condition: T_completion > T_sla + T_execution_date
@dag(
sla_miss_callback=sla_miss_callback,
default_args={'sla': timedelta(hours=2)},
)
def sla_example_dag():
@task(sla=timedelta(hours=1))
def critical_task():
pass # Must complete within 1 hour