fix(cache): await db query before cache.onMutate to close stale-repopulation race#5951
Open
sanjibani wants to merge 1 commit into
Open
fix(cache): await db query before cache.onMutate to close stale-repopulation race#5951sanjibani wants to merge 1 commit into
sanjibani wants to merge 1 commit into
Conversation
…ulation race The cache invalidation in PgPreparedQuery.queryWithCache was firing concurrently with the DB write via Promise.all, contradicting the in-code comment that said the DB write should resolve first. When onMutate (a single Redis Lua script call) completed before the DB write committed, a concurrent SELECT could read the pre-mutation row and repopulate the cache via cache.put(), serving stale data until TTL expiry. Repro: write-heavy workload with cache ex > DB RTT. Change Promise.all([query(), onMutate]) to sequential await. The DB write now commits before any cache keys are cleared, closing the race. Same fix applied to mysql-core, sqlite-core, gel-core, singlestore-core which shared the identical pattern. Regression test added in pg-common-cache.ts that fails on the old ordering (query:end > onMutate) and passes on the new (query:end < onMutate). Closes drizzle-team#5828
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.
Closes #5828.
The cache invalidation in
PgPreparedQuery.queryWithCachewas firing concurrently with the DB write viaPromise.all, contradicting the in-code comment that said the DB write should resolve first.When
onMutate(a single Redis Lua script call) completed before the DB write committed, a concurrent SELECT could read the pre-mutation row and repopulate the cache viacache.put(), serving stale data until TTL expiry. Repro: write-heavy workload withcache.exlarger than the DB RTT.Change
Promise.all([query(), onMutate])to sequentialawait. The DB write now commits before any cache keys are cleared, closing the race. Same fix applied to mysql-core, sqlite-core, gel-core, singlestore-core which shared the identical pattern.Regression test added in pg-common-cache.ts that fails on the old ordering and passes on the new.