Skip to main content

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:

ComponentHow It Uses the Metadata DB
SchedulerDecides what task to run next
WebserverDisplays DAGs, runs, logs, states
WorkersUpdate task status and retries
TriggererManages deferrable tasks
CLIReads 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:

DatabaseUsage 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.

ColumnMeaning
dag_idUnique DAG name
is_pausedWhether DAG is paused
last_parsed_timeWhen 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.

ColumnMeaning
dag_idDAG name
run_idUnique run identifier
staterunning, success, failed
execution_dateLogical date

Example Query​

SELECT dag_id, run_id, state
FROM dag_run
ORDER BY execution_date DESC
LIMIT 5;

Example Output​

dag_idrun_idstate
sales_etlscheduled__2024-01-10success
sales_etlscheduled__2024-01-09failed

3️⃣ task_instance – The Most Important Table​

This table records every task execution.

ColumnMeaning
dag_idDAG name
task_idTask name
statesuccess, failed, retry
try_numberRetry count
start_dateTask start time
end_dateTask end time

Example Query​

SELECT task_id, state, try_number
FROM task_instance
WHERE dag_id = 'sales_etl';

Example Output​

task_idstatetry_number
extract_datasuccess1
transform_datafailed2

πŸ“Œ 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_idtask_idkeyvalue
example_dagpush_valuereturn_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_instance
  • dag_run
  • xcom

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​

TableRows per Year
dag_run365
task_instance~18,000
log~10,000
xcomDepends 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_instance is 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)