One of the features that make REDIS a good caching application is the ability to configure a cluster with one master/primary & one or more slaves/secondary servers. In this tutorial, we will learn to set up redis replication in our Linux servers.

For the purpose of this tutorial, we will be creating a single Primary & a single secondary server. So let’s discuss the pre-requisites for setting up redis replication.

Recommended Read: How to create a free SSL certificate using Let’s Encrypt in Linux

Also Read: How to Schedule a Shutdown in Linux using Crontab?


Pre-Requisites

2 servers with Redis installed, one will act as the Primary server (10.10.10.10 IP address for our scenario) & another will act as the Secondary server (IP address 10.10.10.11 ).

You can read the following tutorials to install Redis in Ubuntu or in CentOS/RHEL. 

Now let’s move on to the configuration part.


Configuring the Redis replication

Primary Server

First, we will start with the Primary server & will configure it. Open the redis configuration file on the primary server. 

Depending on how you have installed it, the configuration file can be ‘/etc/redis/redis/conf’ or ‘/etc/redis/6379.conf’,

$ sudo vi /etc/redis/redis.conf

& look for ‘append’ & ‘appendfilename’, then change it to following,

appendonly yes

appendfilename “appendonly.aof​”

Now restart the redis service to implement the changes made,

$ sudo systemctl restart redis

OR

$ sudo systemctl restart redis_6379 

Now let’s move on to configuring the secondary server.


Secondary Server

Open the redis configuration file on the secondary server, 

$ sudo vi /etc/redis/redis.conf

& look for ‘slaveof’ & change it to following,

slaveof 10.10.10.10 6379

here, ‘10.10.10.10’ is the IP address for the Primary redis server. & ‘6379’ is the port number for redis on that server.  Once changes have been done, restart the service to implement changes,

$ sudo  systemctl restart redis

OR

$ sudo systemctl restart redis_6379

That’s it our setup for redis replication is complete, we will now verify it.


Checking the replication

To check if the master-slave setup is working, connect to the master redis instance using the following command,

$ redis-cli -h 10.10.10.10 -p 6379

 

& run command ‘info’,

10.10.10.10:6379> info

 

We will get the following output, here look for ‘replication’,

# Replication

role:master

connected_slaves:1

Slave0:

ip=10.10.10.11,

port=6379,

state=online,

offset=215,

lag=0

master_repl_offset:215

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:214

 

Here we can see the IP address for the secondary server i.e. 10.10.10.11, also it shows the number of secondary servers i.e. 1.  

We can also try adding a key on the Primary server, that will be replicated to the secondary server in real-time. To create a test key-value, login to redis instance & execute the following command,

10.10.10.10:6379> set test “testing”

 

now to check the key on the master, run command ‘get test’

10.10.10.10:6379> get test “testing”

 

Now, login to slave server & check if the key ‘test’ has been replicated or not,

$ redis-cli -h 10.10.10.11 -p 6379

 

& run ‘get test’ command,

10.10.10.11:6379> get test “testing”

 

This shows that the key has been replicated to the slave server as well & our master-slave data replication is working fine.


Promoting Secondary server as Primary

So in case, the Primary server fails or there is any other issue, we can promote the secondary server as the primary server. To do this connect to the redis instance of the secondary server,

$ redis-cli -h 10.10.10.11 -p 6379

& execute the following command,

10.10.10.11:6379> SLAVEOF NO ONE

Now the secondary server will act as the primary server. You will be required to update the IP address of redis to the application where you have configured it.

Also if there is more than one redis secondary server, then you are required to update the field,

slaveof 10.10.10.11 6379

on all the secondary servers to point to a new primary server.


Reconnecting to Original Primary server

Once the original primary server is up, we can then reconnect the secondary server to the primary server by the following method, connect to the redis instance of the secondary server,

$ redis-cli -h 10.10.10.11 -p 6379

& execute the following command,

10.10.10.11:6379> SLAVEOF 10.10.10.10 6379

That’s it, this was our tutorial on how setup Redis Replication in Linux. Please do send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.