Alex
Alex

Reputation: 1535

Stackexchange Redis Sentinel Client

i m working with Redis distributed cache with failover pattern merged with hashing data distribution. My configuration is :

          Sentinel1                   Sentinel4
Master1                     Master2
          Sentinel2                   Sentinel5
Slave1                      Slave2
          Sentinel3                   Sentinel6

In my code i need to access master cache using sentinel.

Which one have i to point?

I suppose i have to register all the sentinel enpoints inside the configuration of the ConnectionMultiplexer.

I m able to connect my client to sentinel using this code:

  var options = new ConfigurationOptions()
    {
        CommandMap = CommandMap.Sentinel,
        EndPoints = { { IP, Port } },
        AllowAdmin = true,
        TieBreaker = "",
        ServiceName = ServiceName,
        SyncTimeout = 5000,
        AbortOnConnectFail = true,
        Ssl = false
    };
    var connection = ConnectionMultiplexer.Connect(options, Console.Out);
    return connection;

Once got the connection i need to access the cache database using the standard redis methods like SetString and getString... so

db = conn.getDatabase();
db.getString(key);
db.setString(key, value);

at this point i get an error stating "This operation has been disabled in the command-map and cannot be used: SETEX" or GET.

I suppose there should be a way to ask the sentinel the connection to the current master, but i m not finding many useful code example around. Can anyone help me please?

Upvotes: 2

Views: 4920

Answers (2)

d_f
d_f

Reputation: 4859

Things change and today it's enough to specify a sentinel endpoint and serviceName in the redis connection string or Configuration. All the rest has been encapsulated as a part of this commit.

var conn = ConnectionMultiplexer.Connect("sentinel:26379,serviceName=mymaster");
var db = conn.GetDatabase();
db.StringSet("key", "value");

Upvotes: 6

Lucius
Lucius

Reputation: 439

I believe if you connect to the Sentinel you can only issue Sentinel-specific commands. Try connecting to Redis instances instead, as explained here.

As for having the Redis client know, via Sentinel, that there was a change in Redis config (something I'm researching now and that's how I found your question) I think that part isn't implemented in Stackexchange.Redis yet - have a look at this.

Upvotes: 0

Related Questions