apache / apache/airflow

Improve the performance of the DAG Processor

Open
#56,446 7 comments 0 reactions 0 assignees View on GitHub
area:core kind:feature needs-triage
Dominant language
Python
Stars
46.9k
Forks
17.8k
Avg merge
2d 10h
Merged PRs (30d)
483

Description

### Description

We run Airflow 3 in an AWS ECS cluster. We noticed that the DAG processor is always taking all the CPU in the container, and is continuously reloading our DAGs. In our setup, we have about 50 python files, which contain the definitions for about 43 DAGs, and the processing time for the DAGs is about one second per file on average, and the longest processing time is 3 seconds.

Looking closer at the logs, we noticed that the DAGs are continuously being parsed and loaded to the DB. We wanted Airflow to quickly detect and reflect changes to our DAGs, and because of 60 seconds for min_file_process_interval. However, giving that we don't change the files that often, we wanted an optimization that would skip the process of DAGs when we can tell for sure that they have not changed. We noticed the introduction of DAG bundles, and the versioning of the bundles which is leveraged by the GIT DAG bundles, but not the local ones. We decided to take this approach further:
1. Calculate a checksum for the folder of the local DAG bundle, and use that as a version (similar to the GIT commit ID).
2. Track the bundle version for each file as it is parsed. When it is time to populate the file queue, we can compare the current bundle version with that of the file from the last time it was parsed. If the version is the same, then that means that the DAG python file (and any locally imported common python code) has not changed, and therefore we can skip processing it.
3. Add a maximum age, after which we will force the processing of the DAG files (in case there are important side effects of loading them).

After doing these changes, the CPU dropped down significantly. While item 1 above is specific to local DAG bundles, I think items 2 and 3 can be beneficial even for GIT bundles.

### Use case/motivation

_No response_

### Related issues

_No response_

### Are you willing to submit a PR?

- [ ] Yes I am willing to submit a PR!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)

Contributor guide

Open the contributing guide

Research direction

Start with the DAG processor's file-queue population and the local and GIT DAG bundle versioning described in the issue. Review how parsed files retain bundle versions, then define and measure completion using skipped unchanged DAG processing, forced processing after a maximum age, and reduced CPU usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, git, python
Domain
backend, data-engineering, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.