StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Working with sentinels: the master changes to another server

Open emi662002 opened this issue 6 years ago • 9 comments

Hi,

I'm using the ConnectionMultiplexer.SentinelConnect and this works as expected (given any sentinel server order in the connection string, I can retrieve a sentinel connection and then a master connection with GetSentinelMasterConnection.

I am storing this last connection multiplexer as documented (the multiplexer is designed to be reused). The problem I'm currently experimenting is as follows: I have 3 sentinels (host1, host2, host3), host1 is the master. Everything works fine now (any combination of hosts in my connection string will work as the multiplexer always retrieves the right master) When the master changes (for example, in a Kubernetes environment using redis-ha a pod is deleted, the another pod is chosen as master) Then my multiplexer is not valid anymore.

Is there a way to check if the connection multiplexer for the master is still the master?

emi662002 avatar Oct 13 '20 09:10 emi662002

Hi,

I'm using the ConnectionMultiplexer.SentinelConnect and this works as expected (given any sentinel server order in the connection string, I can retrieve a sentinel connection and then a master connection with GetSentinelMasterConnection.

I am storing this last connection multiplexer as documented (the multiplexer is designed to be reused). The problem I'm currently experimenting is as follows: I have 3 sentinels (host1, host2, host3), host1 is the master. Everything works fine now (any combination of hosts in my connection string will work as the multiplexer always retrieves the right master) When the master changes (for example, in a Kubernetes environment using redis-ha a pod is deleted, the another pod is chosen as master) Then my multiplexer is not valid anymore.

Is there a way to check if the connection multiplexer for the master is still the master?

Couldn't find a nice way to do this but we can subscribe to an event instead of polling to check the connection. see https://github.com/StackExchange/StackExchange.Redis/issues/1582#issuecomment-707614401

emi662002 avatar Oct 13 '20 12:10 emi662002

this approach still doesn't work.

emi662002 avatar Oct 15 '20 08:10 emi662002

@emi662002: Did you have any success? I am in a similiar situation and the "configure" through "ConnectionFailed" does not help. He still works with an old IP.

ThomasTosik avatar Oct 20 '21 13:10 ThomasTosik

@emi662002: Did you have any success? I am in a similiar situation and the "configure" through "ConnectionFailed" does not help. He still works with an old IP.

Well, i don't know if it works. I have the code there just in case. What I do to force it is on the healthcheck fail on K8s, just initialise the Redis connection again: basically:

Healthcheck: try { var redisConnection = await cacheFactory.GetRedisConnection(); await redisConnection.GetDatabase().PingAsync(); return new HealthCheckResult(HealthStatus.Healthy); } catch (Exception ex) { await TryRestore(cacheFactory); return Unhealthy(ex); }

Kill connection on healthcheck failed (TryRestore calls this on another class but for simplification): await this.connection.CloseAsync(false); this.connection.Dispose(); this.connection = null;

emi662002 avatar Oct 21 '21 07:10 emi662002

We listen for the Sentinel pub/sub messages to proactively reconnect to any changes in the usual case here, so I'd like to understand better. Can you share how you're connecting? e.g. "Sentinel A and B, servers F, G, H" and let us know how you're connecting, e.g. connecting string especially?

NickCraver avatar Oct 21 '21 14:10 NickCraver

We listen for the Sentinel pub/sub messages to proactively reconnect to any changes in the usual case here, so I'd like to understand better. Can you share how you're connecting? e.g. "Sentinel A and B, servers F, G, H" and let us know how you're connecting, e.g. connecting string especially?

please see: https://github.com/StackExchange/StackExchange.Redis/issues/1582#issuecomment-866628163

emi662002 avatar Oct 22 '21 19:10 emi662002

@emi662002 Several Sentinel fixes went in since that issue was filed and comment you linked to - which version of the library are you on? Are you still having trouble in the latest version?

NickCraver avatar Jan 05 '22 12:01 NickCraver

If anyone's still hitting issues here, please try and latest release - it has a great number of Sentinel fixes along the way. Closing this issue out to tidy up.

NickCraver avatar Feb 20 '22 23:02 NickCraver

Hi @NickCraver I can reproduce this exactly behavior using version 2.5.43. Do you prefer that I open other issue?

JorTurFer avatar Mar 10 '22 08:03 JorTurFer