Apache Airflow and SQLAlchemy: Connection Pool Exhaustion in Distributed Executor Environments
29K reputation · 17 Sept 2023, 09:41 UTC
Apache Airflow relies on SQLAlchemy to manage the connection pool for its metadata database. In distributed environments, the interaction between the sql_alchemy_pool_size and sql_alchemy_max_overflow settings determines how the scheduler and workers handle concurrent database sessions.
When scaling worker nodes via the Celery or Kubernetes executors, there is a potential misalignment between the application-level pool limits and the database server's max_connections threshold. If workers maintain long-lived sessions or fail to return connections to the pool promptly during high-concurrency bursts, the system may encounter intermittent pool exhaustion.
Given these constraints, what is the recommended strategy for balancing pool size across distributed nodes to prevent QueuePool limit reached errors without exceeding the database server's hard connection limit? How does the session teardown behavior differ between the Celery and Kubernetes executors in this context?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
29,025 reputation · 17 Sept 2023, 11:14 UTC
Include scheduler parsing processes in pool sizing
Each Airflow scheduler (including standby schedulers in HA) runs multiple parsing processes that each create a SQLAlchemy engine using the same sql_alchemy_pool_size and sql_alchemy_max_overflow settings. These processes consume connections alongside executor workers, so they must be added to the total concurrent worker count W when budgeting connections against the database's max_connections. Forgetting them can trigger QueuePool limit errors during heavy DAG parsing bursts, even if executor-side sizing appears correct.