How to tell a Client where the new Redis master is using Sentinel
Asked Answered
F

1

10

Ok, I feel like I'm missing some crucial piece of information.

Locally I have 1 master and 1 slave redis server running on different ports http://redis.io/topics/sentinel

I also have 3 sentinels and they all appear to be aware of each other and working as expected.

Now I have a big of java code pointing to 127.0.0.1:6379 where my master redis server is.

If i take down the master, sentinel does everthing as expected promoting the slave to master so now the new master is on

127.0.0.1:6380

My question is how does my code know this and auto switch?

Flogging answered 15/3, 2013 at 16:21 Comment(0)
O
7

You have to subscribe to sentinel messages on one of their pubsub channels. You can see at the link that you posted that the sentinel will publish out messages like

+odown <instance details> -- The specified instance is now in Objectively Down state.
-odown <instance details> -- The specified instance is no longer in Objectively Down state.
+failover-takedown <instance details> -- 25% of the configured failover timeout has elapsed, but this sentinel can't see any progress, and is the new leader. It starts to act as the new leader reconfiguring the remaining slaves to replicate with the new master.
+failover-triggered <instance details> -- We are starting a new failover as a the leader sentinel.

So when you see a sentinel publish on one of those channels, you need to parse the message and have your client respond accordingly. Redis is not smart - you have to handle these things using a client library.

Specifically, the most useful channels are

+odown
+failover-detected
+switch-master
Oren answered 15/3, 2013 at 16:26 Comment(3)
What about using a load balancer on the redis master/slave.. only issue I have with that is I can't write to the slave correct?Flogging
You wouldn't want to load balance using the slave. If you want load balancing, you should have multiple nodes running and come up with logic in your client to handle which keys will go into which nodes.Oren
#29210539Shakespeare

© 2022 - 2024 — McMap. All rights reserved.