Skip to content

Expose HashSlot(RedisChannel) on IConnectionMultiplexer - #3366

Merged
mgravell merged 2 commits into
seredis:mainfrom
aradalvand:feat/hash-slot-of-channel
Nov 27, 2026
Merged

mgravell merged 2 commits into
seredis:mainfrom
aradalvand:feat/hash-slot-of-channel

Conversation

@aradalvand

@aradalvand aradalvand commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3365.

There was no public way to ask which hash slot a RedisChannel is routed by. The usual workaround is HashSlot(new RedisKey(channel)), which hashes the channel's name as a key. It gets two cases wrong: a configured ChannelPrefix, and single-key keyspace channels, which are routed by their key. This exposes the existing ServerSelectionStrategy.HashSlot(in RedisChannel), which is what routing actually uses and handles both.

Changes

  • New IConnectionMultiplexer.HashSlot(RedisChannel), implemented by ConnectionMultiplexer (delegating to ServerSelectionStrategy.HashSlot(in RedisChannel)). Also forwarded by MultiGroupMultiplexer and the test fixture's NonDisposingConnection.
  • ServerSelectionStrategy.HashSlot(in RedisChannel) now returns NoSlot (-1) for multi-node channels (IsMultiNode, e.g. KeySpacePattern/KeySpacePrefix), which are subscribed on every node rather than routed by slot. Internally, slot-based routing is unaffected: the maintenance migration check only considers sharded channels, and multi-node subscriptions never select a server by slot.
  • [OverloadResolutionPriority(1)] on the existing HashSlot(RedisKey), on both the interface and ConnectionMultiplexer. string and byte[] convert implicitly to both RedisKey and RedisChannel; the channel conversions are [Obsolete], but that doesn't remove them from overload resolution. Without the attribute, HashSlot("key") becomes ambiguous (CS0121). I checked this: removing it breaks existing calls in ClusterTests and HotKeysTests. This follows the precedent of RedisChannel.KeySpacePrefix, including the framework shim for older TFMs.
  • PublicAPI.Unshipped.txt entries.

Things worth a reviewer's eye

  • A new interface member. This adds a member to IConnectionMultiplexer, which is a break for anyone implementing it outside the library. Happy to change the shape (e.g. an extension method) if you'd rather avoid that.
  • The priority attribute on an existing member. Adding it doesn't change any signature, so it isn't a binary break. Compilers that don't understand the attribute would still see the ambiguity for string/byte[] arguments.
  • Plain literal and pattern channels. These return the slot of the (prefixed) name: that's the slot this client routes them by, which decides the node it subscribes on, rather than a server-side slot constraint. Multi-node channels return -1. As with HashSlot(RedisKey), it returns -1 when not connected to a cluster.

Testing

  • New ChannelHashSlotUnitTests, against the in-process server in cluster mode:
    • a sharded channel has the same slot as the same-named key;
    • with a ChannelPrefix, its slot is the prefixed name's;
    • a KeySpaceSingleKey channel has its key's slot;
    • a plain literal channel on a cluster has its name's slot;
    • multi-node channels (KeySpacePattern, KeySpacePrefix) return -1;
    • standalone returns -1;
    • string and byte[] arguments still bind to the key overload, both through ConnectionMultiplexer and through the interface.
  • Builds for all of StackExchange.Redis's TFMs with no warnings.
  • Existing tests: all *UnitTests classes pass on net10.0, and CI is green on Ubuntu and Windows.

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).

@mgravell

Copy link
Copy Markdown
Member

Thank you for this — it's a genuinely well-made PR. The diagnosis of the two ways the HashSlot(new RedisKey(channel)) workaround lies (ChannelPrefix, and key-routed keyspace channels) is exactly right, the tests assert the behavior from the correct angle, and flagging your own soft spots up front made the review easy. We verified rather than trusted: all five tests pass locally, the library builds clean across all TFMs on the branch, and the [OverloadResolutionPriority] is both necessary (removing it really does make HashSlot("x")/HashSlot(byte[]) ambiguous via the obsolete RedisChannel conversions) and correctly shimmed for down-level TFMs.

On the "non-sharded, non-key-routed channels" question you raised, we traced the routing and it splits three ways:

  • Literal / Pattern channels: returning the name-hash matches what the client actually routes by — RedisSubscriber sends every subscribe through SelectServer(SUBSCRIBE, …, channel) → ServerSelectionStrategy.HashSlot(channel) — so even though the server broadcasts regular pub/sub cluster-wide, the slot does determine which node this library subscribes on. We're happy with the current behavior here; it fits the method's documented contract.
  • Multi-node channels (RedisChannel.KeySpacePattern(...), KeySpacePrefix(...), etc.): here the "same as internal routing" rationale breaks down. Routing branches to MultiNodeSubscription before any slot is consulted — these are subscribed on every node — yet the new API returns a concrete slot (e.g. 6732 for KeySpacePrefix("hash") in our probe) that nothing ever uses. Given the motivating use case in Expose HashSlot(RedisChannel) on IConnectionMultiplexer #3365 is comparing HashSlotMoved.HashSlot against a channel's slot, a caller with a keyspace-pattern subscription would draw a genuinely wrong conclusion from that number.

The one change we'd ask for: return ServerSelectionStrategy.NoSlot when channel.IsMultiNode, with a test pinning it — and while there, a test pinning the plain-Literal-on-cluster behavior too (it was the first question this PR prompted, so it deserves an executable answer). A short note in the XML docs distinguishing "the slot this client routes by" from "a server-side constraint" would round it out.

You've left maintainer edits enabled, so if you'd rather not do that piece, just say so and we'll happily push it to your branch — your call entirely.

One caveat we considered and accept, for the record: consumers compiling with a Roslyn older than 17.12 don't honor [OverloadResolutionPriority], so their HashSlot("x") calls would hit CS0121. On the v3 line we're fine with that trade.

@aradalvand

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review! Addressed in c872e74:

  • ServerSelectionStrategy.HashSlot(in RedisChannel) now returns ServerSelectionStrategy.NoSlot when channel.IsMultiNode.
  • Added <remarks> to IConnectionMultiplexer.HashSlot(RedisChannel) and ConnectionMultiplexer.HashSlot(RedisChannel) distinguishing the slot this client routes by from a server-side constraint, and noting -1 for non-cluster and multi-node channels.
  • Added MultiNodeChannelsHaveNoSlot (KeySpacePattern and KeySpacePrefix) and LiteralChannelOnClusterHasSlotOfItsName to ChannelHashSlotUnitTests (all 7 tests pass; library builds clean across all TFMs).

aradalvand and others added 2 commits November 25, 2026 01:13
There was no public way to ask which hash slot a channel is routed by. The
usual workaround, hashing the channel's name as a key, ignores
ConfigurationOptions.ChannelPrefix, and the key-based routing of single-key
keyspace channels. This exposes the existing internal
ServerSelectionStrategy.HashSlot(in RedisChannel), which accounts for both.

Strings and byte arrays convert implicitly to both RedisKey and RedisChannel,
so the existing HashSlot(RedisKey) gets [OverloadResolutionPriority(1)] to
keep calls like HashSlot("key") compiling, and binding to it.

Fixes seredis#3365.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XUNs4qcAQaGCRpkwmr8LnY
- Return ServerSelectionStrategy.NoSlot in ServerSelectionStrategy.HashSlot when channel.IsMultiNode
- Clarify XML docs on IConnectionMultiplexer.HashSlot and ConnectionMultiplexer.HashSlot that the slot reflects client routing rather than a server-side constraint, and note -1 for non-cluster / multi-node channels
- Add unit tests pinning multi-node channels (KeySpacePattern, KeySpacePrefix) and plain Literal channels on cluster
@aradalvand
aradalvand force-pushed the feat/hash-slot-of-channel branch from c872e74 to d351e2f Compare November 24, 2026 21:43
@mgravell
mgravell merged commit f80bdd4 into seredis:main Nov 27, 2026
5 of 6 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.

Expose HashSlot(RedisChannel) on IConnectionMultiplexer

2 participants