Airflow Deferrable Operators
A deferrable operator hands its waiting time to a lightweight service called the triggerer. The worker slot frees up at once, so a task that waits for hours costs almost no resources. Deferrable operators help when many tasks wait for slow outside events.
The Restaurant Pager Picture
A diner waits for a table. One restaurant asks the diner to stand at the door and block the entrance. Another restaurant hands over a pager and lets the diner walk away. The pager buzzes when the table is ready. A normal waiting task blocks the door. A deferrable task carries a pager.
Normal waiting task: [worker slot busy ████████████████████] done
Deferrable task: [slot][ free: triggerer watches ][slot]
│ │
start resume
The Problem It Solves
A worker has a fixed number of slots. Fifty sensors in poke mode hold fifty slots while they do nothing but wait. Real tasks queue behind them. Deferrable operators release those slots and let real work run.
The Three Steps of Deferral
- The task starts on a worker and decides it must wait.
- The task defers. It gives a trigger to the triggerer and leaves the worker.
- The trigger fires when the event happens. Airflow schedules the task again, and it finishes on a worker.
The Triggerer Service
The triggerer is a separate Airflow process. It uses asynchronous code to watch thousands of triggers at the same time with very little memory. Start it next to the scheduler:
airflow triggerer
Deferrable tasks stay in the deferred state until a triggerer is running.
Use a Built-In Deferrable Option
Many operators and sensors accept a simple flag.
from airflow.providers.amazon.aws.sensors.s3 import S3KeySensor
wait_for_file = S3KeySensor(
task_id="wait_for_file",
bucket_name="company-data",
bucket_key="incoming/sales.csv",
deferrable=True,
)
Some operators offer a separate "Async" version, such as DateTimeSensorAsync. Check the provider documentation for the exact option.
Write Your Own Deferrable Operator
from datetime import timedelta
from airflow.models import BaseOperator
from airflow.triggers.temporal import TimeDeltaTrigger
class WaitThenGreet(BaseOperator):
def execute(self, context):
self.defer(
trigger=TimeDeltaTrigger(timedelta(minutes=10)),
method_name="resume",
)
def resume(self, context, event=None):
self.log.info("Wait finished. Hello!")
The execute method calls defer and stops. When the trigger fires, Airflow calls the resume method named in method_name.
Task States During Deferral
scheduled ► queued ► running ► deferred ► scheduled ► queued ► running ► success
▲
waiting on the triggerer
Comparing Waiting Methods
| Method | Worker Slot Use | Best For |
|---|---|---|
| Sensor, poke mode | Held the whole time | Waits of a few minutes |
| Sensor, reschedule mode | Freed between checks | Waits of many minutes |
| Deferrable operator | Freed during the wait | Long waits and many parallel waits |
When Not to Use Deferral
- The wait lasts a few seconds. The switch costs more than it saves.
- No triggerer runs in your environment.
- The provider offers no deferrable version of the operator.
Good Habits
- Run at least one triggerer, and two for high availability.
- Set a timeout, so a missing event cannot leave a task deferred forever.
- Keep trigger code light and asynchronous. Blocking code slows every other trigger.
Key Takeaways
- Deferral releases the worker slot during long waits.
- The triggerer watches events for thousands of tasks at once.
- Many built-in operators support
deferrable=True.
