30 Mins

5. Airflow Architecture & Executors

The Airflow Control Plane Components

Airflow's architecture is a decoupled, distributed system where the control plane consists of the Webserver, Scheduler, Metadata Database, and the Triggerer. The Metadata Database serves as the source of truth, storing DAG states, task instances, and configuration, typically managed via SQLAlchemy. When you scale, the database latency becomes a critical bottleneck defined by $T_{total} = T_{query} + T_{network}$, where $T_{query}$ is dominated by row-level locking on the task_instance table.

The Scheduler is the heart of the system, responsible for parsing DAG files and transitioning task states from 'scheduled' to 'queued'. In high-concurrency environments, you must monitor 'scheduler_heartbeat' metrics and consider running multiple schedulers in High Availability (HA) mode. The Triggerer was introduced to handle deferred tasks, allowing the system to handle thousands of concurrent tasks without occupying worker slots while waiting for external events.

Operator Note: Always index your metadata database specifically on task_instance(state, dag_id) to prevent performance degradation as your history grows.
SQL
-- Inspecting active task instances to monitor database load
SELECT state, count(*) as task_count 
FROM task_instance 
WHERE state IN ('running', 'queued', 'scheduled') 
GROUP BY state;

Choosing the Right Executor

The executor determines where and how your tasks are run. The LocalExecutor is excellent for single-node deployments but lacks horizontal scalability. The CeleryExecutor provides a robust, distributed architecture utilizing Redis or RabbitMQ as the message broker, making it the industry standard for production environments requiring high throughput. Tasks are distributed across worker nodes that pull from specific queues.

The KubernetesExecutor represents the modern cloud-native approach, spinning up a discrete pod for every individual task instance. This provides complete isolation, as $Memory_{task} = Memory_{pod}$, ensuring no resource contention between tasks. While this eliminates the overhead of managing a worker cluster, it introduces pod startup latency, which must be accounted for in time-sensitive ETL pipelines.

Operator Note: Use KubernetesExecutor if your tasks have wildly different dependency requirements that would otherwise lead to 'dependency hell' on static Celery workers.
Python 3
# Configuration snippet to prioritize specific task queues in Celery
from airflow import DAG
from airflow.operators.bash import BashOperator

with DAG('priority_dag', schedule_interval=None) as dag:
    # Using a specific queue to isolate high-resource tasks
    task = BashOperator(
        task_id='heavy_compute_job',
        bash_command='echo "Process complete"',
        queue='high_memory_workers'
    )

Scaling, Queues, and Troubleshooting

Scaling Airflow requires a careful balance between resource pools and queue bindings. Pools allow you to limit parallelism for specific tasks, preventing your cluster from overwhelming downstream databases or APIs. By utilizing pools, you can ensure that your database concurrency remains within the limits of the connection pool $Conn_{limit} > rac{Tasks_{running}}{Workers}$.

Troubleshooting often involves checking the 'scheduler_loop_duration' metric. If this value consistently exceeds the 'min_file_process_interval', your scheduler is starved of resources. When scaling, focus on the Metadata Database first; if the database cannot handle the write IOPS, increasing your worker count will only exacerbate the contention and lock wait times.

Operator Note: If your task latency increases after scaling up workers, check for 'zombie tasks' or database connection exhaustion using your monitoring dashboard.
Bash
# Command to check the number of task instances currently running in a specific pool
airflow pools list | grep 'default_pool'

Knowledge Check

Ready to test your understanding of 5. Airflow Architecture & Executors?

Back