🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Deferrable and Async Operators in Airflow

đŸŸĸ Free Lesson

Advertisement

Deferrable and Async Operators in Airflow

Deferrable Operator FlowWorkerCelery / K8s PodDeferrable OpYields triggerTriggerJSON serializableTriggererAsync event loopCallbackRequeues taskBlocking vs Deferrabletime.sleep() wastes slots; triggers free worker resourcesUse CasesLong API polls, file waits, DB checks, event streamsDeferrable ops: 10x more efficient for long-running external waits

Architecture Diagram

Formal Definitions

Detailed Explanation

The Problem with Blocking Operators

When a traditional operator waits for an external event (like an API response or file availability), it holds a worker slot the entire time. This wastes resources because the worker is doing nothing but polling.

Key Insight: A blocking sensor polling every 30 seconds for 1 hour uses only 0.1% of its time productively — the other 99.9% is wasted polling.

How Deferrable Operators Solve This

AspectBlocking OperatorDeferrable Operator
Worker SlotHeld during entire waitReleased immediately
Memory Usage~50-200MB per task~1KB per trigger
ConcurrencyLimited by executor slotsLimited by triggerer capacity
Resource EfficiencyLowHigh

Why Deferrable Operators?

Traditional blocking operators hold a worker slot while waiting for external conditions. For long-running operations like API callbacks, database queries, or cloud job completions, this wastes valuable executor resources. Deferrable operators release the worker slot immediately and delegate the wait to the lightweight triggerer process.

For a blocking sensor polling every 30 seconds for 1 hour: (nearly all time is wasted polling). A deferrable equivalent achieves since the worker slot is released immediately.

Creating a Deferrable Operator

HTTP Polling Trigger

Triggerer Configuration

Key Concepts Table

ComponentPurposeResource ImpactScaling
Deferrable OperatorReleases worker slot on deferMinimal worker usageIndependent of worker count
Trigger ObjectAsync event definitionNear-zero in memorySerialized to DB
Triggerer ProcessExecutes async triggersSingle process, many coroutinesScale by capacity config
Trigger CallbackRequeues completed taskMetadata DB writeScales with DB connections
BaseTriggerBase class for triggersN/AN/A

Comparison: Blocking vs Deferrable

MetricBlocking OperatorDeferrable Operator
Worker slot heldEntire durationOnly during execute()
Memory per wait~50-200MB (worker process)~1KB (trigger object)
ConcurrencyLimited by executor slotsLimited by triggerer capacity
Latency on completionPoll intervalSeconds (triggerer heartbeat)
Implementation complexitySimpleModerate
Best forShort waits (<5 min)Long waits (>5 min)

Code Examples

Custom Trigger with Multiple Conditions

Deferrable Sensor Pattern

Performance Metrics

MetricBlockingDeferrableImprovement
Worker slot time (1hr wait)3600s~0.1s36000x
Memory per waiting task~100MB~1KB100000x
Max concurrent waits (16 slots)161000+62x+
Task startup overheadWorker processTrigger coroutineNegligible

Best Practices

Implementation Guidelines

  1. Use for long waits: Deferrable operators shine when tasks wait >5 minutes for external events.
  2. Implement serialize(): Always implement serialize() on triggers for persistence across triggerer restarts.
  3. Handle timeouts: Set execution_timeout on defer calls to prevent infinite waits.
  4. Use async libraries: Leverage aiohttp, httpx, or aiobotocore for non-blocking I/O in triggers.

Operational Guidelines

  1. Monitor triggerer capacity: Track triggers.running and triggers.queued metrics.
  2. Fail gracefully: Handle trigger errors by yielding TriggerEvent({'status': 'error'}).
  3. Avoid blocking in triggers: Never use time.sleep() or synchronous I/O in trigger run() methods.
  4. Test triggers independently: Mock the async loop and verify trigger serialization.

When to Use Deferrable Operators

ScenarioUse Deferrable?Reason
API polling (>5 min)YesSaves worker resources
Database queries (<1 min)NoOverhead not justified
File existence checksYesCan wait indefinitely
Cloud job monitoringYesJobs can run for hours
Quick HTTP calls (<30s)NoSimpler to use blocking

See Also

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Airflow Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement