Airflow Metadata Database Explained
The Brain Behind Every DAG π§ πβ
The Story: The Invisible Engine Powering Airflowβ
Imagine Apache Airflow as a busy airport βοΈ.
DAGs are flight schedules, tasks are airplanes, and workers are pilots.
But who keeps track of everything?
- Which flights already departed?
- Which ones failed?
- Which are delayed or retried?
- Which are scheduled next?
That responsibility belongs to Airflowβs Metadata Database β the single source of truth for everything happening inside Airflow.
Without it:
- The Scheduler wouldnβt know what to run
- The Web UI would show nothing
- Task retries, dependencies, and states would collapse
This article explains what the Airflow Metadata Database is, how it works, what tables matter, and how to manage it professionally in production.
What Is the Airflow Metadata Database?β
The Airflow Metadata Database (also called the Metastore) is a relational database used by Airflow to store:
- DAG definitions and versions
- Task execution states
- DAG runs and schedules
- Task retries and failures
- XCom messages
- Users, roles, and permissions
- Logs metadata (not always log content)
π Important: This database does NOT store your business data β only Airflow operational metadata.
Why the Metadata Database Is Criticalβ
Every Airflow component depends on it:
| Component | How It Uses the Metadata DB |
|---|---|
| Scheduler | Decides what task to run next |
| Webserver | Displays DAGs, runs, logs, states |
| Workers | Update task status and retries |
| Triggerer | Manages deferrable tasks |
| CLI | Reads and updates DAG state |
π If the metadata database is slow or corrupted, Airflow becomes slow or unusable.
Supported Databases (Production vs Local)β
Airflow uses SQLAlchemy, so it supports multiple databases:
| Database | Usage Recommendation |
|---|---|
| SQLite | β Local testing only |
| PostgreSQL | β Best for production |
| MySQL / MariaDB | β Production (with tuning) |
π Best Practice:
Always use PostgreSQL for production-grade Airflow.
Core Metadata Tables You Must Knowβ
Letβs explore the most important tables β the heartbeat of Airflow.
1οΈβ£ dag β DAG Definitionsβ
Stores basic DAG-level information.
| Column | Meaning |
|---|---|
dag_id | Unique DAG name |
is_paused | Whether DAG is paused |
last_parsed_time | When DAG was last parsed |
π If a DAG doesnβt appear in UI β check this table first.
2οΈβ£ dag_run β Every DAG Executionβ
Each scheduled or manual run creates a row here.
| Column | Meaning |
|---|---|
dag_id | DAG name |
run_id | Unique run identifier |
state | running, success, failed |
execution_date | Logical date |
Example Queryβ
SELECT dag_id, run_id, state
FROM dag_run
ORDER BY execution_date DESC
LIMIT 5;
Example Outputβ
| dag_id | run_id | state |
|---|---|---|
| sales_etl | scheduled__2024-01-10 | success |
| sales_etl | scheduled__2024-01-09 | failed |
3οΈβ£ task_instance β The Most Important Tableβ
This table records every task execution.
| Column | Meaning |
|---|---|
dag_id | DAG name |
task_id | Task name |
state | success, failed, retry |
try_number | Retry count |
start_date | Task start time |
end_date | Task end time |
Example Queryβ
SELECT task_id, state, try_number
FROM task_instance
WHERE dag_id = 'sales_etl';
Example Outputβ
| task_id | state | try_number |
|---|---|---|
| extract_data | success | 1 |
| transform_data | failed | 2 |
π 90% of Airflow debugging starts here.
4οΈβ£ xcom β Cross-Task Communicationβ
Stores small messages exchanged between tasks.
β οΈ Warning: XCom is stored in the metadata DB β large payloads can kill performance.
Example Python Codeβ
from airflow.decorators import task
@task
def push_value():
return {"records": 120}
Stored in DB As:β
| dag_id | task_id | key | value |
|---|---|---|---|
| example_dag | push_value | return_value | {"records":120} |
π Best Practice: Store only small JSON / strings, not files or DataFrames.
5οΈβ£ log β Metadata About Logsβ
Tracks:
- Who triggered a DAG
- When a task was cleared or retried
- Manual actions from UI
π Actual logs may live on local disk, S3, GCS, or Azure (covered in next article).
How the Metadata Database Works (Flow)β
DAG File β Parsed by Scheduler
β Metadata stored in DB
β Scheduler queries DB
β Task sent to Worker
β Worker updates DB
β Webserver reads DB
π The database is constantly read and written to.
Performance Best Practices (Very Important for Production)β
β Use a Strong Database Backendβ
- PostgreSQL with SSD storage
- Proper connection pooling
β Enable Database Cleanupβ
Old records slow everything down.
airflow db cleanup \
--clean-before-timestamp 2024-01-01
β Limit XCom Usageβ
β Donβt store:
- DataFrames
- Files
- Large JSON blobs
β Monitor Table Growthβ
Especially:
task_instancedag_runxcom
Common Mistakes with Metadata Databaseβ
β Using SQLite in production
β Storing large objects in XCom
β Never cleaning old DAG runs
β Running Airflow with underpowered DB
β Ignoring DB connection limits
Input & Output Example (Realistic Scenario)β
Inputβ
A DAG runs daily with:
- 50 tasks
- 3 retries
- 1 year retention
Output in Metadata DBβ
| Table | Rows per Year |
|---|---|
| dag_run | 365 |
| task_instance | ~18,000 |
| log | ~10,000 |
| xcom | Depends on usage |
π This is why cleanup matters.
Summary π§ β
- The Airflow Metadata Database is the brain of Airflow
- It stores DAGs, task states, schedules, retries, and UI data
- Every Airflow component depends on it
- Poor DB design = poor Airflow performance
- PostgreSQL + cleanup = stable production Airflow
Key Takeawaysβ
- β Metadata DB stores operations, not business data
- β
task_instanceis the most critical table - β XCom misuse is a common performance killer
- β Database cleanup is mandatory at scale
- β Healthy metadata DB = healthy Airflow
Whatβs Next? πβ
β‘οΈ Airflow Logs & Log Retention Policies
(How logs work, where they live, and how to manage them professionally)