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:
- 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.
- 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?
Other Common Redis Questions (and Answers)
- What is the default port used by Redis?
- How to start Redis server?
- Is Redis persistent?
- How fast is Redis?
- How to install Redis on Mac?
- How to check if Redis is running?
- How to restart Redis?
- Does Redis persist data?
- How to install Redis on Ubuntu?
- How to stop Redis server?
- How to see Redis version?
- Does Redis have tables?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost