Skip to content

feat(client): add stale cache fallback on disconnect - #3495

Draft
PavelPashov wants to merge 1 commit into
redis:masterfrom
PavelPashov:feat/csc-serve-stale-on-disconnect
Draft

PavelPashov wants to merge 1 commit into
redis:masterfrom
PavelPashov:feat/csc-serve-stale-on-disconnect

Conversation

@PavelPashov

Copy link
Copy Markdown
Contributor

Description

Add an opt-in serve-stale disconnect policy with a required maximum stale age. Return cached values when reads fail with connection errors and clear stale entries on reconnect.


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

Add an opt-in serve-stale disconnect policy with a required maximum
stale age. Return cached values when reads fail with connection errors
and clear stale entries on reconnect.

@Cdineshreddy12 Cdineshreddy12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice feature — the test matrix (connection-error vs error-reply vs abort, joined readers, maxStaleAge) is thorough. I spent time in cache.ts recently (#3641) so I focused the review there; one correctness point and a couple of smaller notes.

#onDisconnect drops in-flight promise entries without invalidating them, which reintroduces the race #3641 just fixed in clear(). The serve-stale branch moves value entries to #staleEntries and then does:

this.#cacheKeyToEntryMap.clear();

That drops any in-flight promise entries (loads that haven't resolved yet) without calling entry.invalidate() on them. handleCache() re-validates the promise entry before storing (step 3d), so a load that resolves in the narrow window where its reply and onError() interleave will pass validate() and re-insert its value into the now-cleared live map — a live entry appearing while the client is disconnected. That's exactly the case #3641 added the invalidation loop to clear() for:

// current master, clear():
for (const entry of this.#cacheKeyToEntryMap.values()) entry.invalidate();
this.#cacheKeyToEntryMap.clear();

This PR predates #3641, so after a rebase the comment in #onDisconnect ("Promise entries are dropped exactly as clear() drops them") no longer holds — clear() now invalidates them first and #onDisconnect doesn't. Suggest invalidating the promise entries you drop (iterate and invalidate() the ones you're not moving to stale, or anything still in #cacheKeyToEntryMap after the value-entry move). A test: start a load, fire onError() before it resolves, then resolve it successfully, and assert size() stays 0.

Smaller:

  • isConnectionError treats any Error with a 'syscall' in err as a connection error. That's the right intent for raw net.Socket errors (ECONNRESET/EPIPE), but it's a structural check — worth a comment that it's deliberately matching Node system errors, since a future error type that happens to carry syscall would be served stale.
  • Stale entries past maxStaleAge are only removed when a read touches them (per the test). During a long outage, keys that aren't read again sit in #staleEntries past their max age until reconnect. Probably fine given maxStaleAge bounds correctness, but worth noting it's a retained-memory tradeoff rather than a time-based sweep.

Haven't run it against a server (the cluster/standalone docker harness uses --network host, which doesn't map ports on Docker for Mac), but the unit tests read correctly and cover the branches well.

This branch has not been deployed

No deployments
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.

2 participants