Fix API server memory leak: bound DBDagBag version cache with LRU eviction#64326
Open
dheerajturaga wants to merge 7 commits intoapache:mainfrom
Open
Fix API server memory leak: bound DBDagBag version cache with LRU eviction#64326dheerajturaga wants to merge 7 commits intoapache:mainfrom
dheerajturaga wants to merge 7 commits intoapache:mainfrom
Conversation
eladkal
reviewed
Mar 27, 2026
Contributor
|
Nice. Seems like a real production bug. A few thoughts:
|
Member
Author
Done! scheduler is now not bound by the cache. Its only the API server that can have the cache size configurable. Also added metrics to track. |
Contributor
…ction Replace the plain dict in DBDagBag._dags with a bounded OrderedDict-based LRU cache. In long-running API server processes, every unique dag_version_id accessed is inserted and never evicted, causing unbounded RSS growth (observed: 9.4 GiB after 7 days with ~70k DAG versions in DB). The cache is now capped at 512 entries by default (configurable via core.max_dag_version_cache_size). Cache hits promote the entry to MRU so frequently-accessed versions are retained over stale historical ones.
Co-authored-by: Elad Kalif <45845474+eladkal@users.noreply.github.com>
Replace the unbounded DBDagBag._dags dict with an OrderedDict-based LRU
cache. In long-running API server processes every unique dag_version_id was
inserted and never evicted, causing unbounded RSS growth (observed: 9.4 GiB
after 7 days with ~70k DAG versions in DB).
- API server: bounded LRU cache, size controlled by api.dag_version_cache_size
(default 4096). Config lives in [api] because only the API server accumulates
historical versions.
- Scheduler: explicitly unbounded (max_cache_size=None). Its working set is
naturally capped at one version per active DAG, so a size limit would add
eviction overhead with no benefit.
- Add Stats metrics: dag_bag.cache.hits, dag_bag.cache.misses,
dag_bag.cache.evictions, dag_bag.cache.size (sampled at 10%) — emitted to
the configured StatsD/OTEL backend, no-op if metrics are not configured.
Co-authored-by: Jens Scheffler <95105677+jscheffl@users.noreply.github.com>
9712e2b to
6942523
Compare
Member
pierrejeambrun
left a comment
There was a problem hiding this comment.
Compared to Kaxil version, I believe this implementation isn't thread safe, which is a problem. Can we fill that gap?
Otherwise looking good to me.
Member
|
cc: @kaxil in case you want to take a look :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DBDagBag._dags is an unbounded in-memory cache causing steady memory
growth in the API server.
DBDagBag was designed for the scheduler, which works with a bounded set
of currently-active DAG versions. As an API server singleton, it is exposed to
the full history of DAG versions in the database with no bound on how
many it will cache.
Replace the plain dict in DBDagBag._dags with an OrderedDict-based LRU
cache. In long-running API server processes, every unique dag_version_id
accessed is inserted and never evicted, causing unbounded RSS growth (observed:
9.4 GiB after 7 days with ~70k DAG versions in DB).
The scheduler and API server have different access patterns, so the cache
policy is now split:
(configurable via api.dag_version_cache_size). Cache hits promote the
entry to MRU so frequently-accessed versions are retained over stale
historical ones.
set is naturally capped at one version per active DAG, so a size limit
would add eviction overhead with no benefit.
Add Stats metrics to make the cache observable:
dag_bag.cache.hits, dag_bag.cache.misses, dag_bag.cache.evictions,
dag_bag.cache.size — emitted to the configured StatsD/OTEL backend,
no-op if metrics are not configured.
Was generative AI tooling used to co-author this PR?
ClaudeCode