StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.9k stars 1.51k forks source link

Issues with Sentinel #1427

Closed ctlajoie closed 3 years ago

ctlajoie commented 4 years ago

I implemented support for Redis Sentinel in an application yesterday and I had a rough time getting it to work. I had to actually dig into the library code to figure out what I was doing wrong. If you make a few minor tweaks to the API you could entirely avoid the problems I ran into. Allow me to explain. This is not the exact code I had; I modified it to illustrate the point.

IConnectionMultiplexer sentinel = ConnectionMultiplexer.Connect(new ConfigurationOptions {
    ServiceName = _redisSentinelServiceName,
    EndPoints = { { _redisSentinelHost, 26379 } },
    CommandMap = CommandMap.Sentinel
});

IConnectionMultiplexer master = sentinel.GetSentinelMasterConnection(new ConfigurationOptions {
    ServiceName = _redisSentinelServiceName
});

Looks like it should work... but there are 2 problems with this code.

  1. IConnectionMultiplexer does not have a GetSentinelMasterConnection function.
  2. The sentinel ConfigurationOptions MUST have TieBreaker = "" to work.

I have a few suggestions:

  1. Add GetSentinelMasterConnection to IConnectionMultiplexer
  2. Have a separate ConnectionMultiplexer.ConnectSentinel(...) function, which sets CommandMap and TieBreaker to the required values, and make endpoints default to port 26379.
  3. Just use the ServiceName provided in the Sentinel configuration, so I don't have to provide it twice

I realize there may be any number of reasons why these suggestions might not make sense (this isn't my code), but please consider if there's some improvements to be made with regard to the library's Sentinel functionality.