Skip to content

Enhance handling of loading replicas when replying LOADING ... - #3382

Open
lamoboos223 wants to merge 7 commits into
seredis:mainfrom
lamoboos223:main
Open

lamoboos223 wants to merge 7 commits into
seredis:mainfrom
lamoboos223:main

Conversation

@lamoboos223

@lamoboos223 lamoboos223 commented Nov 27, 2026 •

Copy link
Copy Markdown
  • Introduced logic to suppress loading replicas from selection until they are confirmed to be ready to serve traffic again.
  • Added methods to manage loading suppression and clear it when a command proves the server is no longer loading.
  • Implemented a mechanism to reissue commands away from loading replicas to ensure commands are routed to available servers.
  • Added unit tests to validate behavior when all replicas are loading and ensure proper rerouting to the primary server.

This change improves the reliability of command execution in scenarios where replicas are temporarily unable to serve requests due to loading states.

closes #1913

Checklist

  • I fully and freely contribute this code in accordance with the project license (and am legally able to do so)
  • I take responsibility for this contribution's quality and correctness, including any portions produced with AI assistance (see CONTRIBUTING.md).

- Introduced logic to suppress loading replicas from selection until they are confirmed to be ready to serve traffic again.
- Added methods to manage loading suppression and clear it when a command proves the server is no longer loading.
- Implemented a mechanism to reissue commands away from loading replicas to ensure commands are routed to available servers.
- Added unit tests to validate behavior when all replicas are loading and ensure proper rerouting to the primary server.

This change improves the reliability of command execution in scenarios where replicas are temporarily unable to serve requests due to loading states.

@mgravell mgravell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept looks good; will look in more depth after weekend - a few nits, but: heading in the right direction

…ng states, allowing certain commands to be processed while replicas are loading
…Exchange for setting the loading suppression state, ensuring that the latest observed value is respected
@lamoboos223
lamoboos223 requested a review from mgravell November 28, 2026 16:54
@lamoboos223 lamoboos223 changed the title Enhance handling of loading replicas in Redis commands Enhance handling of loading replicas when replying LOADING ... Nov 28, 2026
@lamoboos223

Copy link
Copy Markdown
Author

Concept looks good; will look in more depth after weekend - a few nits, but: heading in the right direction

Thank you @mgravell for the valuable comments and code review!

IServer messages are now marked CommandServerSpecific ("tied to a specific
endpoint, never retry elsewhere"), and the LOADING reissue skips any message
carrying that flag. So e.g. IServer.DatabaseSize() against a loading replica
surfaces the LOADING error instead of quietly returning another node's answer;
resumed SCAN cursors, which already carry the flag, are covered too.

This is deliberately not NoRedirect: a keyed command should still follow MOVED.
The flag's other reader, RetryPolicy, is not reachable from IServer.

@mgravell mgravell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the updates — the earlier nits are all sorted (the CEX is exactly what I was after). I've done a deeper pass:

  1. Server-targeted commands could be answered by a different node. IServer messages don't carry NoRedirect (and FixFlags turns them into PreferReplica when the target is a replica), so conn.GetServer(replica).DatabaseSize() against a loading replica was re-selected and returned the primary's (or the other replica's) count as the replica's. The underlying gap — nothing marked those messages as tied to their endpoint — predates this PR, so I've taken it on and pushed 32d3756 to your branch: IServer messages now carry CommandServerSpecific, and the LOADING reissue skips anything with that flag (which also covers resumed SCAN cursors). There's a regression test with it.

  2. The "accepted while loading" list is a lot shorter than Redis's. Going by the LOADING flag in upstream src/commands/*.json, ECHO, SELECT, SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE/UNSUBSCRIBE, PUBLISH/SPUBLISH, CLIENT SETNAME/SETINFO, ROLE, CLUSTER NODES, TIME, READONLY, MULTI/EXEC/DISCARD/WATCH/UNWATCH, RESET, QUIT, LATENCY and SLOWLOG are all served while loading (PING isn't). Several of those reach a replica routinely — the handshake tracer is ECHO, the subscription connection SUBSCRIBEs, topology refresh is CLUSTER NODES — so suppression gets cleared early. The cost is bounded (one wasted round trip, then LOADING suppresses it again), but a hand-maintained list mirrored in two places will drift. I'd prefer to drop the early clear and let the 2s expiry do the work, which also takes the switch off the success path. If you'd rather keep it, it needs to key off something positive (a successful non-internal data command) rather than an exclusion list. Either way the toy's copy has the same gaps, so it currently rejects SUBSCRIBE/ECHO/SELECT while loading, which real Redis doesn't.

  3. DemandReplica when every replica is loading. The first call surfaces LOADING, but for the next 2s selection finds nothing and callers get "no connection available" even though the connections are healthy — less informative than today's LOADING error. Making that error say why servers were skipped is a broader, pre-existing issue and doesn't belong here, but I'd like a test that pins the behaviour down so it's a deliberate choice.

Smaller:

  • There's no test for "one replica loading, the other healthy → reads go to the healthy one", which the description claims.
  • IsLoadingSuppressed() sits ahead of everything in IsSelectable, including primaries; a replica promoted while suppressed stays skipped for up to 2s. Fine, but worth a comment.
  • The toy now lets the handshake through while loading, which makes the comment in MultiGroupTests/LoadingRerouteTests.cs ("every reconnect fails during the handshake") stale.
  • The Windows CI failure (ConfigTests.GetClients, net481) looks unrelated.

…hat certain commands can be answered while the server is still loading and adjusted the logic in ResultProcessor to prevent premature clearing of loading suppression based on successful replies to commands that are allowed during loading

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.

Give chance of reconnecting to another server on failures like LOADING

2 participants