ioredis icon indicating copy to clipboard operation
ioredis copied to clipboard

Failover on master when there aren't slaves

Open antoniomuso opened this issue 4 years ago • 3 comments

I think can be useful (in a sentinel environment where role: 'slave') to have an option to failover on master when there aren't slaves alive.

antoniomuso avatar Nov 11 '21 11:11 antoniomuso

I think this could be done subscribing to sdown|odown channel on sentinel instance and mapping the state of the slaves as they and fallback to using the master. Also, but resource wasteful, by long polling SENTINEL REPLICAS <master name>

simonecorsi avatar Nov 11 '21 11:11 simonecorsi

The reverse (failover to a slave when using master role) would be useful too. My initial idea about this is in https://github.com/luin/ioredis/issues/840.

I think a unavailable master can be detected via error MASTERDOWN, not sure if a similar error code exist for the reverse situation. Main challenge about this is probably the recovery.

silverwind avatar Apr 12 '22 16:04 silverwind

Yeah new roles like master-slave and slave-master sgtm.

Don't feel we need to subscribe to any events as ioredis will reconnect when the connected server is down so the only thing we need to do is to add the fallback logic in https://github.com/luin/ioredis/blob/main/lib/connectors/SentinelConnector/index.ts#L328

The only case it won't work is when the master is down but the connection to the server is still live. But it should be a separate job that has already been addressed by https://github.com/luin/ioredis/blob/main/lib/connectors/SentinelConnector/FailoverDetector.ts.

luin avatar Apr 13 '22 04:04 luin