Skip to content

fix(cluster): route the empty-string key by slot instead of at random - #3660

Merged
nkaradzhov merged 1 commit into
redis:masterfrom
Cdineshreddy12:fix/cluster-empty-string-key
Nov 23, 2026
Merged

nkaradzhov merged 1 commit into
redis:masterfrom
Cdineshreddy12:fix/cluster-empty-string-key

Conversation

@Cdineshreddy12

@Cdineshreddy12 Cdineshreddy12 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

getClientAndSlotNumber classified the routing key as keyless with a falsy check (!firstKey), but the empty string is a valid Redis key (slot 0). So cluster.set('', v) / cluster.get('') were sent to a random node instead of the slot owner — a MOVED loop, and a MaxCommandRedirectionsError once the cluster has more than maxCommandRedirections nodes.

The pinned-client path (index.ts) and the parser already gate on firstKey === undefined; this aligns the normal routing path with them. An empty Buffer was already routed correctly (a Buffer is truthy) — this only affects the empty string.

Added a unit test (in-process fake node, no docker) asserting "" resolves its slot rather than taking the keyless random-node branch.


Note

Medium Risk
Touches core cluster command routing; the change is small but incorrect routing could still affect any caller using empty-string keys until deployed.

Overview
Fixes cluster routing so "" is treated as a keyed command (hash slot 0) instead of being sent to a random node like true keyless commands.

In getClientAndSlotNumber, the keyless branch now checks firstKey === undefined instead of !firstKey, matching the pinned-client path and parser. That stops operations like cluster.set('', v) / cluster.get('') from missing the slot owner and hitting MOVED loops or MaxCommandRedirectionsError on larger clusters.

Adds an in-process unit test with a fake RESP2 node asserting "" returns slot 0 and the slot owner client, while undefined still routes keylessly with no slot.

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

getClientAndSlotNumber treated the first key as keyless with a falsy
check (!firstKey), but the empty string is a valid Redis key (slot 0).
So commands like cluster.set('', v) or cluster.get('') were sent to a
random node instead of the slot owner, producing a MOVED loop — and a
MaxCommandRedirectionsError once the cluster has more than
maxCommandRedirections nodes.

Match the pinned-client path and the parser's own checks by routing as
keyless only when firstKey === undefined. An empty Buffer was already
routed correctly (a Buffer is truthy); this fixes the empty string.
@chatgpt-codex-connector

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-21T13:53:46.151834Z 1277b60 PR opened
ℹ️ 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.

@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 c42a640 into redis:master Nov 23, 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.

2 participants