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.