Question: How can you force a Redis slave to become a master?

Answer

In Redis, you may sometimes need to promote a slave to a master for various reasons, such as implementing a failover mechanism in case the existing master node fails. Here's how you would do it:

  1. Stop replication: You should first stop the slave from continuing with the replication process. This is done by executing the SLAVEOF NO ONE command. In the Redis CLI, this looks like:
redis-cli SLAVEOF NO ONE

This command effectively makes the current slave server a master.

  1. Data consistency: It's important to ensure that all the data on the new master server (the former slave) is up-to-date before turning off replication. If any write operations were performed on the old master after the last sync, they won't be present in the new master.

Please note that forcing a slave to act as a master should generally only be done when the master is permanently unavailable or during maintenance tasks. Doing so in other cases might lead to inconsistent states across your Redis servers.

Also understand that if the original master comes back online, it will not automatically start acting as a slave of the new master. If you want to set up a new master-slave relationship, you'll have to manually configure that using the SLAVEOF command again.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.