Skip to content

The Subscribe method subscribes to one node only when having Redis cluster #2750

Closed

Description

@orlinhristov

I'm trying to write a straightforward .Net app that will subscribe to a cluster of Redis nodes, to receive change notifications.

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var redis = ConnectionMultiplexer.Connect("10.0.255.86:6379, 10.0.255.87:6379, 10.0.255.88:6379");

            var subscriber = redis.GetSubscriber();

            RedisChannel topic = "__key*__:*";

            subscriber.Subscribe(topic, (channel, value) =>
            {
                System.Console.WriteLine("Key change notification received. Channel: {0}, Value: {1}", channel, value);
            });

            var endpoint = subscriber.SubscribedEndpoint(topic);

            System.Console.WriteLine("Subscribed to Redis endpoint {0}", endpoint);

            System.Console.ReadLine();
        }
    }
}

I see that the subscriber always subscribes to one of the nodes (in my case "10.0.255.86:6379") and I guess it's because the Redis will broadcast each published message to all other nodes:

"The clients can send SUBSCRIBE to any node and can also send PUBLISH to any node. It will simply broadcast each published message to all other nodes."

It works as described if the message is indeed published by a client, but won't work the same way when it comes to internal messages published by the node itself, when a change occurs. For some reason only the node that owns the key/value entity will receive it. I don't know if that's a bug in Redis or it works this way by design but I tried to workaround it. I created a separate ConnectionMultiplexer connecting it to a single node. However, all the multiplexers always subscribe to "10.0.255.86:6379" including those that are connected to "10.0.255.87:6379" or "10.0.255.88:6379" and receive change notifications that happen on that node. I guess the ConnectionMultiplexer gets the whole list of nodes after connects and always subscribes for the first one.

So, if I'm not doing anything wrong, then I assume there is no way to control to which node to subscribe and respectively there is no way to receive Redis keyspace notifications from all the nodes using StackExchange.Redis.

Activity

  1. changed the title [-]The Subsscribe subscribes to one node only when having Redis cluster[/-] [+]The Subscribe method subscribes to one node only when having Redis cluster[/+] on Jun 27, 2024
  2. mgravell commented on Jul 22, 2026

    @mgravell
    Collaborator

    Implemented in StackExchange.Redis 2.11.0 (#2995): cluster-wide keyspace/keyevent notifications via RedisChannelOptions.MultiNode and the RedisChannel.KeySpace/KeyEvent factory APIs (see docs/KeyspaceNotifications.md). Closing as completed.

    Note: this needs the new multi-node channel APIs — a plain string-channel subscribe still targets a single node.

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