Skip to content

fix(client): empty the client-side cache when the selected database changes - #3641

Merged
nkaradzhov merged 2 commits into
redis:masterfrom
Cdineshreddy12:fix/csc-select-db
Nov 19, 2026
Merged

nkaradzhov merged 2 commits into
redis:masterfrom
Cdineshreddy12:fix/csc-select-db

Conversation

@Cdineshreddy12

@Cdineshreddy12 Cdineshreddy12 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3631

Implements what @nkaradzhov laid out in the issue: empty the cache when the selected database changes, instead of putting the database in the cache key or rejecting SELECT.

  • new #setSelectedDB helper that records the db and, only when the value actually changed, calls this.#clientSideCache?.invalidate(null, this._clientId) — so selecting the already-selected database keeps the cache
  • used in the three places that record a successful SELECT: select(), the pipeline path and the multi() path
  • reset() always empties the cache, even when the database doesn't change, since RESET turns client tracking off server-side and entries cached before it would never get invalidated
  • raw sendCommand(['SELECT', ...]) left out of scope as discussed

Tests: the repro from the issue as a spec plus the pipeline, multi() and reset() variants @nkaradzhov asked for, and one check that selecting the current database doesn't drop the cache. One heads-up: I couldn't run the spec suite locally — the test harness spawns containers with --network host, which doesn't expose ports to the host on Docker for Mac, and times out before any test runs (pre-existing specs do the same on my machine). I verified the change against a real Redis 8 instead by running the issue's repro script plus the pipeline/multi/reset variants against the patched source: before the fix the second get() returns value in db 0 with a cache hit, after it returns value in db 1, and the cache size drops to 0 on pipeline-SELECT, multi-SELECT and reset(). Happy to adjust if CI turns anything up.


Note

Medium Risk
Changes core client-side caching behavior on SELECT and RESET paths; incorrect invalidation could cause stale reads or extra cache misses, but scope is limited to CSC users on RESP3.

Overview
Fixes #3631 by clearing the client-side cache when the active Redis database changes, since cache keys omit the DB and server invalidations only name keys.

A new #setSelectedDB path records the selected DB and calls full cache invalidation only when the DB actually changes—wired into select(), successful SELECT in pipelines, and SELECT inside multi(). Re-selecting the same database leaves the cache intact. reset() always empties the cache because RESET disables server tracking, so pre-reset entries would never invalidate.

BasicClientSideCache.clear() now invalidates every entry before dropping map state, so an in-flight load cannot pass validate() after a clear and write a stale value back.

New specs cover clear() vs in-flight promises and select / pipeline / multi / reset` database-change behavior.

Reviewed by Cursor Bugbot for commit b3641a7. Bugbot is set up for automated code reviews on this repo. Configure here.

…hanges

Cache keys are built from command arguments only and invalidation messages
carry just the key name, so after select() a cached read kept serving the
previous database's value until something wrote that key name.

A new #setSelectedDB helper records the database and empties the cache
when the value changed; it is used by select(), SELECT in a pipeline and
SELECT in multi(). reset() always empties the cache, even when the
database does not change, because RESET turns client tracking off on the
server, so entries cached before it would never get invalidated.

Raw sendCommand(['SELECT', ...]) is out of scope, as discussed in redis#3631.

Fixes redis#3631
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T07:00:31.275172Z b3641a7 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e4598a4c15

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

// RESET turns client tracking off on the server, so entries cached before it
// would never get invalidated: always empty the cache, even when the
// selected database does not change.
this._self.#clientSideCache?.invalidate(null, this._self._clientId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Invalidate pending loads before clearing the shared cache

When two RESP3 clients share BasicPooledClientSideCache, calling reset() on A while B has a GET in flight can now poison the cache even though both remain on DB 0. invalidate(null) calls clear(false), which drops the maps without invalidating the pending entry object. If B's delayed read reply and a concurrent write's invalidation are decoded together afterwards, the invalidation cannot find the removed index; handleCache() then re-inserts the obsolete reply because cacheEntry.validate() still succeeds. A local RESP3 transport repro returned fresh data on the parent commit versus stale cache hits here, potentially indefinitely with ttl: 0. Invalidate outstanding entries or use a cache-generation guard before clearing.

Useful? React with 👍 / 👎.

A load in flight while clear() runs holds its promise entry, and
handleCache() re-validates that entry before saving the loaded value, so
a reply from before the clear could re-insert its value into the emptied
cache. Invalidating the entries before dropping the maps makes the
revalidation fail, the same way per-key invalidation already does.
@Cdineshreddy12

Copy link
Copy Markdown
Contributor Author

Good catch from the bot — clear() dropped the maps but left in-flight promise entries valid, so a load racing the clear could re-insert its stale value afterwards (handleCache re-validates the entry before saving). Pushed a commit that invalidates the entries before dropping the maps, with a unit test that fails without it. That window predates this PR (any server-initiated null invalidation cleared the same way), but this PR adds callers, so fixing it here.

@nkaradzhov nkaradzhov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@nkaradzhov
nkaradzhov merged commit 60befdf into redis:master Nov 19, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-side cache returns the previous database's value after select()

2 participants