Skip to content

Expose HashSlot(RedisChannel) on IConnectionMultiplexer #3365

Done

Description

@aradalvand

IConnectionMultiplexer.HashSlot(RedisKey) tells you which hash slot a key is in, but there's no public way to ask the same for a RedisChannel. With sharded pub/sub, that's a natural thing to want. For example, to tell whether a HashSlotMoved event concerns a channel you're subscribed to, you compare e.HashSlot against the channel's slot.

The workaround, and where it goes wrong

The obvious workaround is to hash the channel's name as if it were a key:

var slot = multiplexer.HashSlot(new RedisKey(channel)); // or (RedisKey)(byte[])channel!

The repo's own ClusterShardedTests does this too. But it isn't what the library actually routes by, which is ServerSelectionStrategy.HashSlot(in RedisChannel). That method is internal. It differs from the workaround in two ways:

  • ConfigurationOptions.ChannelPrefix. For a channel that doesn't ignore the prefix (e.g. RedisChannel.Sharded(...)), the slot is computed from the prefixed name, since that's the name sent to the server. The workaround hashes the bare name, so with a prefix configured it gets a different slot. It does so silently.
  • Key-routed keyspace channels. A channel from RedisChannel.KeySpaceSingleKey(key, db) is routed by the key part (RoutingSpan strips the __keyspace@{db}__: prefix). The workaround hashes the whole channel name instead.

Proposal

Expose the existing logic, alongside the key overload:

public interface IConnectionMultiplexer
{
    int HashSlot(RedisKey key);
    int HashSlot(RedisChannel channel); // new
}

// ConnectionMultiplexer
public int HashSlot(RedisChannel channel) => ServerSelectionStrategy.HashSlot(channel);

Like HashSlot(RedisKey), it would return -1 when not connected to a cluster.

This adds a member to a public interface. I believe that's been acceptable in minor releases before, since the interface isn't meant to be implemented outside the library. It's flagged here in case you'd prefer a different shape, e.g. an extension method or a member on ISubscriber.

I have a PR ready that does this, with unit tests against the in-process cluster server.

Activity

  1. added a commit that references this issue on Nov 24, 2026
    3eca39b
  2. added 2 commits that reference this issue on Dec 2, 2026
    f80bdd4
    ed66398
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions