Skip to content

fix(ttl-cache): concurrent calls during the first fetch get undefined (@boergeson) - #8375

Open
boergeson wants to merge 2 commits into
monkeytypegame:masterfrom
boergeson:fix/ttl-cache-fetch-failure
Open

fix(ttl-cache): concurrent calls during the first fetch get undefined (@boergeson)#8375
boergeson wants to merge 2 commits into
monkeytypegame:masterfrom
boergeson:fix/ttl-cache-fetch-failure

Conversation

@boergeson

@boergeson boergeson commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Description

Concurrent calls to a cacheWithTTL function while the first fetch is still running each got undefined back, since the cache was marked fresh before the data arrived. Now the first call starts the fetch and later calls wait on the same promise, so there is only one fetch and everyone gets the value.

A failed fetch still marks the cache fresh, so the stale value keeps being served until the ttl passes. That part is unchanged on purpose, the cache also throttles the database.

Closes #8363

@monkeytypegeorge monkeytypegeorge added the backend Server stuff label Aug 29, 2026
@fehmer

fehmer commented Aug 29, 2026

Copy link
Copy Markdown
Member

hi @boergeson ,

thank you for your contribution.

Fetching only once if expired and waiting on the fetch on concurrent calls is fine.

I don't think the behavior in case of a fetch error is correct. The cache also acts like a throttel and if there is a problem with the database now each request will re-try and add additional load on the database.

Currently the cache is only used for PSA and the plan was to use it for leaderboard as well, both are non-critical and I think it is better to serve stale data in case of an error instead of an error or no data. Therefor I would keep the current behavior.

@Miodec what is your take on this?

@boergeson
boergeson force-pushed the fix/ttl-cache-fetch-failure branch from 8d3e6f1 to cfe8c5a Compare August 29, 2026 14:29
@boergeson boergeson changed the title fix(ttl-cache): dont serve stale data after a failed fetch (@boergeson) fix(ttl-cache): concurrent calls during the first fetch get undefined (@boergeson) Aug 29, 2026
@boergeson

Copy link
Copy Markdown
Contributor Author

Makes sense, didnt think of it as a throttle. Reworked it: a failed fetch still marks the cache fresh and the stale value is served until the ttl passes, only the in-flight dedup is left. Updated the test to check the stale value is kept after a failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Server stuff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug (backend): cacheWithTTL marks cache fresh before fetch resolves — failed fetch serves stale data for full TTL, no in-flight dedup

3 participants