Database backend optimization in Apache Airflow - Commands & Configuration
[core] executor = LocalExecutor sql_alchemy_conn = postgresql+psycopg2://airflow:airflow@localhost:5432/airflow [database] sql_alchemy_pool_size = 10 sql_alchemy_max_overflow = 20 sql_alchemy_pool_recycle = 1800 [scheduler] max_threads = 4 [logging] logging_level = INFO [metrics] enable = True
The sql_alchemy_conn sets the connection string to the PostgreSQL database used by Airflow.
The sql_alchemy_pool_size and sql_alchemy_max_overflow control how many database connections Airflow keeps open to improve speed.
The sql_alchemy_pool_recycle helps avoid stale connections by recycling them every 1800 seconds.
Adjusting max_threads in the scheduler controls how many tasks Airflow can schedule in parallel, affecting database load.
Enabling metrics helps monitor database performance over time.
airflow db upgrade
airflow db check
airflow db cleanup --clean-before-timestamp 2023-01-01T00:00:00
--clean-before-timestamp - Specify the cutoff date for cleaning old recordsairflow scheduler --num_runs 1--num_runs - Limit the scheduler to run a fixed number of iterations for testingIf you remember nothing else from database backend optimization, remember: tuning connection pooling and cleaning old data keeps Airflow fast and stable.
Practice
Solution
Step 1: Understand Airflow database role
The Airflow database stores metadata about tasks and DAGs, which affects scheduling and UI responsiveness.Step 2: Connect optimization to performance
Optimizing the database backend reduces delays in task scheduling and speeds up the UI loading times.Final Answer:
Improves task scheduling and UI speed -> Option BQuick Check:
Database optimization = better scheduling and UI [OK]
- Confusing database optimization with adding more DAGs
- Assuming it fixes DAG code errors automatically
- Thinking it removes the need for a scheduler
Solution
Step 1: Recall Airflow database commands
The official command to upgrade the Airflow database schema isairflow db upgrade.Step 2: Verify command correctness
Other options like migrate, update, or refresh are not valid Airflow CLI commands for database upgrade.Final Answer:
airflow db upgrade -> Option DQuick Check:
Upgrade command = airflow db upgrade [OK]
- Using 'migrate' instead of 'upgrade'
- Confusing with generic database commands
- Typing 'update' or 'refresh' which are invalid
sql_alchemy_pool_size = 10?
[database] sql_alchemy_pool_size = 10
Solution
Step 1: Understand sql_alchemy_pool_size meaning
This setting controls the maximum number of open connections in the SQLAlchemy connection pool.Step 2: Interpret the value 10
Setting it to 10 means Airflow can have up to 10 simultaneous database connections open.Final Answer:
Limits the number of simultaneous DB connections to 10 -> Option AQuick Check:
sql_alchemy_pool_size = max DB connections [OK]
- Confusing pool size with retry count
- Thinking it sets query timeout
- Assuming it controls DB replicas
airflow db check and get a connection timeout error. What is the most likely cause?Solution
Step 1: Analyze the error type
A connection timeout error means Airflow cannot reach the database server.Step 2: Rule out other options
Missing config or DAG syntax errors do not cause connection timeouts. Scheduler status is unrelated to DB connection.Final Answer:
Database server is unreachable or down -> Option AQuick Check:
Connection timeout = DB unreachable [OK]
- Blaming missing config for connection timeout
- Thinking DAG errors cause DB connection issues
- Assuming scheduler status affects DB connection
Solution
Step 1: Understand pool size and max overflow
sql_alchemy_pool_size sets base DB connections; max_overflow allows extra temporary connections beyond pool size.Step 2: Tune for heavy load
Decreasing pool size but increasing max_overflow lets Airflow handle bursts by opening extra connections temporarily.Step 3: Evaluate other options
Setting both to zero disables pooling (bad), increasing pool size but reducing overflow limits burst handling, disabling pooling hurts performance.Final Answer:
Decrease sql_alchemy_pool_size and increase sql_alchemy_max_overflow -> Option CQuick Check:
Pool size low + max overflow high = better burst handling [OK]
- Setting both pool size and overflow to zero disables pooling
- Increasing pool size but limiting overflow reduces flexibility
- Disabling pooling hurts DB performance
Ready to prove it?
Pass the test and this pattern moves unknown
