user9124444
user9124444

Reputation:

C# operations in Redis sentinel

I've setup a local docker environment for an HA redis cluster (2 replicas, 1 master, 3 sentinels). Only the sentinels are exposing ports (10021, 10022, 10023).

I'm using the stackexchange.redis C# client v.2.1.58, and try to do some basic operations.

This is my sample code.

  ConfigurationOptions configuration = new ConfigurationOptions
  {
      /// sentinels 
      EndPoints =
      {
          { "localhost", 10021 },
          { "localhost", 10022 },
          { "localhost", 10023 }
      },
      CommandMap = CommandMap.Sentinel,
      ServiceName = "redismaster",
      Ssl = false,
   };

   ConnectionMultiplexer connection = ConnectionMultiplexer.SentinelConnect(configuration, Console.Out);
   IDatabase database = connection.GetDatabase();

When trying a set operation

   database.StringSetAsync("key", "value");

I'm getting

This operation has been disabled in the command-map and cannot be used: SET

I'm thinking that the operations are done against the sentinel nodes, but I'm not sure.

Some help with this ?

Repo here https://github.com/rms1234567890/redis-sentinel

Upvotes: 2

Views: 3506

Answers (1)

sonus21
sonus21

Reputation: 5388

Due to the latest change in StackExchange Redis, this will not work, you need to directly connect with Sentiels using service name like

 using(var connection2= ConnectionMultiplexer.Connect("localhost:10021,localhost:10022,localhost:10023,serviceName=redismaster,AllowAdmin=true"))
 {
      IDatabase database2 = connection.GetDatabase();
      database2.StringSet("test-key", "value"); 
      database.StringGet("test-key");
 }

Even this will fail when you run your application on your local machine but if you run this in a docker instance in the same network as your sentinel/Redis this should work. In short, you need to expose the port of the master Redis instance as well, given another slave can become master as well, so you need to allow inbound traffic rule(s) for all Redis instances(master, slave, and sentinels)

Upvotes: 3

Related Questions