Question: How can you promote a Redis replica to master?

Answer

In order to promote a Redis replica to master, you'll need to follow certain steps. This process is typically required when the current master is not available or in other scenarios where you want to switch over to a different node.

Here are the steps:

  1. Disconnect the replica from the master: You can do this using the replicaof no one command. This will stop the replication and therefore allow the current replica to accept write commands.

    redis-cli -h <replica_host> -p <replica_port> replicaof no one
  2. Check if the promotion was successful: Use the info replication command. If the role is 'master', then your replica has been promoted successfully.

    redis-cli -h <new_master_host> -p <new_master_port> info replication

The output should begin with something like this:

```
# Replication
role:master
connected_slaves:0
...
```

3. Connect other replicas (if any) to the new master: For every other replica, use the replicaof <new_master_host> <new_master_port> command.

```sh
redis-cli -h <slave_host> -p <slave_port> replicaof <new_master_host> <new_master_port>
```

Please note that replica promotion is a manual process and during this process there can be data inconsistencies due to asynchronous nature of Redis replication.

It's also important to mention sentinel mode, which is a more robust way to handle failover, as it automates this process. However, setting up and maintaining a sentinel system can be more complex than managing a simple master-replica setup.

Was this content helpful?

Start building today

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