Airflow Best Practices
Best practices are habits that keep pipelines reliable, fast, and easy to maintain. Teams that follow them spend less time fixing night-time failures. Each habit below solves a problem that many beginners meet in the first months.
Make Every Task Idempotent
An idempotent task gives the same result no matter how many times it runs for the same date. Think of a light switch labeled "turn on". Pressing it twice leaves the light on. A task that appends rows each run breaks this rule, and a rerun creates duplicates.
Bad: INSERT rows for today → rerun doubles the rows Good: DELETE rows for the date, then INSERT → rerun gives the same rows Good: MERGE / UPSERT on a unique key → rerun gives the same rows
Keep Tasks Small and Focused
One task should do one job. A task that downloads, cleans, and loads data hides the failing step. Three small tasks show exactly where the problem sits, and a retry repeats only the broken step.
Keep the DAG File Light
The scheduler parses every DAG file again and again. Heavy work at the top level of the file slows everything down.
# Bad: runs on every parse
data = requests.get("https://api.example.com/list").json()
# Good: runs only when the task runs
def fetch():
return requests.get("https://api.example.com/list").json()
Use Time Values from Airflow
Never call datetime.now() inside a task for business logic. Use {{ ds }} or the data interval. A rerun of last week's date then processes last week's data and not today's.
Store Secrets Safely
- Place passwords and tokens in Connections or a secrets backend.
- Keep credentials out of DAG files and version control.
- Give each connection only the permissions it needs.
Pass Small Data Through XComs
XComs live in the metadata database. Pass file paths and ids, and keep large data in object storage or a warehouse.
Set Sensible Defaults
default_args = {
"owner": "data_team",
"retries": 2,
"retry_delay": timedelta(minutes=5),
"email_on_failure": True,
}
with DAG(
dag_id="orders_daily",
default_args=default_args,
start_date=datetime(2024, 1, 1),
schedule="@daily",
catchup=False,
max_active_runs=1,
tags=["orders", "daily"],
doc_md="Loads daily orders into the warehouse.",
) as dag:
...
| Setting | Benefit |
|---|---|
| owner | Shows who to contact |
| retries | Handles short outages automatically |
| catchup=False | Prevents a flood of old runs |
| max_active_runs | Limits overlapping runs |
| tags | Helps filtering in the UI |
| doc_md | Adds documentation inside the UI |
Name Things Clearly
A DAG named dag1 tells nobody anything. Names like orders_daily_load and extract_orders_api explain the purpose at a glance. Use lowercase letters and underscores everywhere.
Control Resources with Pools
A pool limits how many tasks hit one resource at the same time. Create a pool named warehouse with five slots and assign it to every warehouse task. Airflow then never opens more than five connections together.
Test Before You Deploy
Run DAG import checks and task tests before every release. A later topic covers testing in depth.
Use Version Control
Store DAG files in Git. Review changes with a teammate before they reach production. History shows who changed what and lets you roll back a bad release.
Best Practices Checklist
[ ] Tasks are idempotent [ ] Tasks are small and single-purpose [ ] No heavy code at the top of DAG files [ ] No datetime.now() in business logic [ ] Secrets live in Connections [ ] XComs carry small values only [ ] Retries, owner, and tags are set [ ] Code sits in Git and passes tests
Key Takeaways
- Idempotent, small tasks make reruns safe.
- Light DAG files keep the scheduler fast.
- Defaults, naming, and documentation help the whole team.
