1

To subscribe to Sentinel failover, what is the name of the Channel, and how do I detect that I need to refresh the master in the subscribed function?

I have a multi node Redis setup using Redis Sentinel for High Availability and failover.

I need to setup a Pub/Sub to Redis to detect when the Redis Master has failed and the system has elected a new Master.

_sentinel = redis.sentinel.Sentinel([(app.config["REDIS_HOSTNAME"],app.config["REDIS_SENTINEL_PORT"])])
_master = _sentinel.master_for(app.config["REDIS_SERVICE_NAME"])

def _sentinel_message_handler(message):
    #TODO how do I detect that there is a new Redis Master?

_pubsub = _master.pubsub()
_pubsub.subscribe(**{app.config["TODO"]:_sentinel_message_handler})
pettinato
  • 1,472
  • 2
  • 19
  • 39
  • 1
    A list of channels can be found here: https://redis.io/topics/sentinel#pubsub-messages – davissp14 Dec 02 '16 at 23:29
  • I see one issue here is that the REDIS_HOSTNAME should be running through a list of Sentinel Hostnames trying each one successively until a connection is achieved. This prevents crashing on startup just because one sentinel node is down. – pettinato Dec 03 '16 at 01:43

1 Answers1

5

To achieve this, you will want to subscribe to the sentinel, not the master node. The channel you are looking for is "+switch-master". The Sentinel documentation indicates the channel as "switch-master" "without the +", but as of 3.2.4 the + is included.

Redis Sentinel Pub/Sub Message Docs

You should be able to reference the redis-py docs for the rest. Redis-py Docs


UPDATE

You could also consider using the client-reconfig-script setting.

When the master changed because of a failover a script can be called in order to perform application-specific tasks to notify the clients that the configuration has changed and the master is at a different address.

http://download.redis.io/redis-stable/sentinel.conf

Hope that helps

davissp14
  • 765
  • 4
  • 13
  • The sentinel object doesn't have a pubsub method. Only the master. I agree that this seems odd as I want the failover to work even if the master is unplugged. Do I need to find all the slaves and pubsub to every slave as well as the master? – pettinato Dec 03 '16 at 01:42
  • Well the primary issue is that the failover events are not published to the Redis nodes, only the Sentinels. The only thing you will see in the Redis pub/sub are "__sentinel__:hello" events. – davissp14 Dec 03 '16 at 02:37
  • 1
    I am guessing you are wanting to listen for failovers so you can quickly reconfigure your client? If sentinel pub/sub is out of the question, one idea would be to use: https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L414 as your source of truth. Not ideal though. – davissp14 Dec 03 '16 at 02:50
  • I think what I want to do is the following, 1) Use Sentinel to find the Master node 2) Use the Master node to find all the slave nodes 3) Subscribe pub/sub for the master as well as the slave nodes. Then when a new master is assigned, hopefully the message handler will be called. – pettinato Dec 03 '16 at 19:54
  • Maybe this would work for you? `http://download.redis.io/redis-stable/sentinel.conf`` Search for: "CLIENTS RECONFIGURATION SCRIPT" – davissp14 Dec 05 '16 at 04:52