1

A redis master can be discovered via sentinel using:

from redis.sentinel import Sentinel
sentinel = Sentinel([('127.0.0.1', 26379)])
master_server = sentinel.master_for('mymaster')

Now to write data to the master node :

master_server.setex(key, 120, value)
# do something
master_server.setex(key, 120, new_value)

Now, while # do something, if the master_server crashes, sentinel will promote a slave to master by voting. Hence, master_server.setex(key, 120, new_value) will throw a MasterNotFoundError.

A workaround is to try-catch this block; Is there a way such that master discovery is automatically handled by redis? Since placing try-catch everywhere in the code would make the code redundant and messy.

ndim
  • 35,870
  • 12
  • 47
  • 57
greenlantern
  • 374
  • 1
  • 3
  • 15

1 Answers1

0

Redis sentinel actually does have automatic rediscovery of master, as it uses the SentinelConnectionPool. While it does have some issues—see my recent question—it sounds like the issue here is that your redis setup isn't successfully electing a new master.

Do you have an odd number of sentinels with an appropriately set quorum?

https://redis.io/topics/sentinel

Check out the redis sentinel page to make sure you aren't running into some of the common pitfalls.

dizzyf
  • 3,263
  • 19
  • 29
  • 1
    I understand that a new master will be automatically discovered. My questions is, suppose if master_server is set to node_a...and node_a crashes midway...then do I always have to again set `master_server = sentinel.master_for('mymaster')` in a try-catch block – greenlantern Mar 09 '18 at 06:49
  • @greenlantern Did you find any better solution than try-catch-refresh-master? – CodeFu Sep 25 '20 at 18:22