In this Valkey blog post, I will demonstrate how we can set up a simple replication process among the Valkey nodes and then finally use some auto-failover mechanism with the help of the Valkey-sentinel tool.

Replication (Master-Slave)

Basically, we have two setups with the information below.

Now let’s prepare some basic configurations in file [/etc/valkey/valkey.conf] for both servers.

  • requirepass – To add authentication in valkey
  • Masterauth – To connect the replication securely.

Now restart the Valkey service on each node.

Next, we will set up a basic replication setup. To establish the replication, we need to execute the command below from the slave node.

Syntax:

Command:

The above setting can be persisted using the [config rewrite] command.

That’s all. The replication setup is successful now!

We need to make sure the firewall/IP whitelisting is done for both servers.

Now, let’s do some data writing on the master[ 172.31.21.83] to confirm if the replication works fine or not.

On slave[172.31.58.227], the changes were very well reflected.

The only caveat with this setup is if the master[172.31.21.83] goes down, then we have no auto-failover to sustain the incoming connections on other nodes unless we do some manual intervention.

However, there is one way of setting up an auto-failover mechanism in Valkey by using valkey-sentinel tool. In this way, the application can either use the direct sentinel port [26379] to connect to Valkey server or use some middleware/haproxy tool to distribute read/write. Well, we do not go into those details and are confined to the sentinel setup only here.

Auto-Failover using valkey-sentinel

First, we can edit the sentinel configuration file [/etc/valkey/sentinel.conf] on all nodes as below.

 

  • sentinel monitor – It tells sentinel to start monitoring a new master with the specified name, ip, port, and quorum.
  • sentinel down-after-milliseconds – Sentinel verify based on this if the instance is unreachable/down or not before doing any failover.
  • sentinel failover-timeout  – In case a sentinel voted another sentinel for the failover of a given master it will wait this many milliseconds to attempt to failover the same master again.
  • sentinel parallel-syncs – The number of replicas that can be synced with the new master after a failover at the same time.
  • sentinel auth-pass – To perform the secure authentication when requirepass is enabled in valkey configurations.

Then we can restart the valkey/valkey-sentinel services as below.

We can see the sentinel process running on the default port [26379].

Let’s do some failover now.

I stopped the Valkey service on the original master[172.31.21.83]. And then after a few seconds, slave[172.31.58.227] becomes the master.

Looking over the sentinel logs [/var/log/valkey/sentinel.log], we can see the master switchover events captured.

Again, once we start the service on the old master [172.31.21.83], it automatically connects as a slave.

The status of the new master also reflects the changes.

Conclusion

A basic replication setup in Valkey/Redis would be very handy to have multiple copies of data over the different machines and also act as a backup/disaster recovery option. Still, it’s better, especially for production setups, to have a sentinel layer in the topology to have a minimal impact on the application in case the master node goes down due to some reasons.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments