What if you never had to worry about missing a deadline again because your system tells you first?
Why SLA misses and notifications in Apache Airflow? - Purpose & Use Cases
Imagine you run a busy bakery where cakes must be ready by 9 AM every day. You try to check each cake manually to see if it's done on time. Sometimes you forget, or you find out too late, upsetting customers.
Manually tracking if tasks finish on time is slow and easy to miss. You might not notice delays until it's too late, causing unhappy customers and rushed fixes. It's stressful and error-prone.
With SLA misses and notifications in Airflow, you get automatic alerts when tasks don't finish on time. This means you can fix problems quickly before they affect your users, without constantly watching the clock.
Check task status every hour and send email if late
dag = DAG(..., sla_miss_callback=notify_team)
task = PythonOperator(..., sla=timedelta(minutes=30))You can trust the system to watch deadlines and alert you instantly, freeing you to focus on other important work.
A data team uses SLA notifications to know immediately if a daily report job is late, so they can fix it before executives see missing data.
Manual deadline checks are slow and risky.
SLA misses and notifications automate alerts for late tasks.
This helps teams respond faster and keep systems reliable.
Practice
sla parameter on an Airflow task do?Solution
Step 1: Understand the purpose of
Theslain Airflow tasksslaparameter sets a time limit for task execution.Step 2: Identify what happens when the limit is exceeded
If the task runs longer than theslatime, Airflow marks it as an SLA miss.Final Answer:
It defines a time limit; if the task runs longer, it triggers an SLA miss. -> Option AQuick Check:
slasets time limit = C [OK]
- Confusing SLA with retry count
- Thinking SLA schedules task start time
- Assuming SLA disables tasks
Solution
Step 1: Recall the correct data type for
Theslaslaparameter expects adatetime.timedeltaobject.Step 2: Identify the correct syntax for 30 minutes timedelta
timedelta(minutes=30)correctly creates a 30-minute duration.Final Answer:
sla=timedelta(minutes=30) -> Option CQuick Check:
slauses timedelta object = B [OK]
- Using string instead of timedelta
- Passing integer without timedelta
- Using incorrect string format
task1 runs longer than 10 minutes?from datetime import timedelta
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from airflow.utils.dates import days_ago
def sla_miss_callback(dag, task_list, blocking_task_list, slas, session=None):
print('SLA missed!')
dag = DAG('sla_example', start_date=days_ago(1))
task1 = DummyOperator(
task_id='task1',
dag=dag,
sla=timedelta(minutes=10)
)
dag.sla_miss_callback = sla_miss_callbackSolution
Step 1: Understand SLA miss callback usage
The DAG-levelsla_miss_callbackis called when any task misses its SLA.Step 2: Identify what the callback does
The callback prints 'SLA missed!' when triggered by a task exceeding its SLA.Final Answer:
Thesla_miss_callbackfunction will print 'SLA missed!' when the SLA is missed. -> Option AQuick Check:
SLA miss triggers callback print = A [OK]
- Thinking SLA causes retries automatically
- Assuming DummyOperator ignores SLA
- Believing DAG fails immediately on SLA miss
def notify_sla_miss(dag, task_list, blocking_task_list, slas, session=None):
send_email('admin@example.com', 'SLA Missed')
dag = DAG('sla_notify', start_date=days_ago(1), sla_miss_callback=notify_sla_miss)
task = DummyOperator(task_id='task', dag=dag, sla=timedelta(minutes=5))What is the likely reason notifications are not sent?
Solution
Step 1: Check the callback function implementation
The callback callssend_emailbut no import or definition is shown.Step 2: Understand Airflow's email sending
Airflow requires proper email function or operator; undefinedsend_emailcauses failure silently.Final Answer:
Thesend_emailfunction is not defined or imported. -> Option DQuick Check:
Undefined email function blocks notification = D [OK]
- Setting callback on DAG instead of task (both valid but DAG preferred)
- Wrong SLA time units
- Assuming DummyOperator ignores SLA
Solution
Step 1: Understand how SLA miss notifications work
SLA misses trigger thesla_miss_callbackfunction assigned to the DAG or task.Step 2: Identify correct way to send Slack message on SLA miss
Implement a callback function that calls Slack API and assign it to DAG'ssla_miss_callback.Final Answer:
Define asla_miss_callbackfunction that uses Slack API and assign it to the DAG'ssla_miss_callbackparameter. -> Option BQuick Check:
SLA miss callback with Slack API = A [OK]
- Using on_failure_callback instead of sla_miss_callback
- Trying to notify via downstream tasks
- Misconfiguring sla parameter as Slack channel
Ready to prove it?
Pass the test and this pattern moves unknown
