Redis Replication Setup guide

Replication is one of the main concepts of distributed systems. This document presents the step by step guide to configure master-slave Redis replication.

Redis is an open source key-value data store, using an in-memory storage model with optional disk writes for persistence. It features transactions, pub/sub, and automatic fail-over, among other functionality. Redis has clients written in most languages, with recommended ones featured on their website. When using redis in production, it is a best practice to replicate your cache data across various redis nodes. This replication provides redundancy for node failures and helps in quickly recovering with help of slave servers.

In this document, we use CentOS 7 and Redis version 3.2. The same procedure applies for RHEL, Oracle Linux and Fedora. We will be setting up as the master server and as the slave.

Setting up Master:

Redis is available in CentOS epel repo. Install epel.

# rpm -ivh

Use yum to install redis

# yum install redis

Edit /etc/redis.conf and ensure that the master listens your IP address, by specifying the bind variable and port. If bind variable is already specified, edit it.

# vi /etc/redis.conf
port 6379

Note : In this document, we are not setting up password for redis. It is advisable to setup passwords for your redis. Refer to SECURITY section in

Start redis using systemctl.

/bin/systemctl restart redis.service

Your master server is started. You can verify it by using below commands.

[[email protected] redis]# ps -ef | grep redis
redis 2439 1 1 20:55 ? 00:01:19 /usr/bin/redis-server
root 3050 1787 0 22:30 pts/0 00:00:00 grep --color=auto redis
[[email protected] redis]#

[[email protected] redis]# netstat -nltp | grep 6379
tcp 0 0* LISTEN 2439/redis-server 1
[[email protected] redis]#

[[email protected] redis]# redis-cli -h -p 6379 info server | grep uptime
[[email protected] redis]#

Setting up Slave:

Install redis using yum, as we did for master server.

# rpm -ivh
# yum install redis

Edit /etc/redis.conf and ensure that the slave listens your IP address, by specifying the bind variable and port. If bind variable is already specified, edit it.

port 6379

In the slave section of /etc/redis.conf, mention that this server is slave of master Make sure this server is able to reach 6379 port of master.

slaveof 6379
slave-read-only yes

Start redis slave using systemctl.

/bin/systemctl restart redis.service

Confirming Master-Slave replication from the logs:

As soon as the slave server is started, it will reach master and sync up the data. You can see it in the log – /var/log/redis/redis.log of slave and master.

Slave :
[[email protected] redis]# tail /var/log/redis/redis.log
1335:S 06 Mar 20:59:23.362 * Connecting to MASTER
1335:S 06 Mar 20:59:23.362 * MASTER <-> SLAVE sync started
1335:S 06 Mar 20:59:23.362 * Non blocking connect for SYNC fired the event.
1335:S 06 Mar 20:59:23.362 * Master replied to PING, replication can continue...
1335:S 06 Mar 20:59:23.363 * Partial resynchronization not possible (no cached master)
1335:S 06 Mar 20:59:23.364 * Full resync from master: a08e95c23225e7507dc2a740b9ee00fc7bcb220e:1
1335:S 06 Mar 20:59:23.417 * MASTER <-> SLAVE sync: receiving 77 bytes from master
1335:S 06 Mar 20:59:23.417 * MASTER <-> SLAVE sync: Flushing old data
1335:S 06 Mar 20:59:23.417 * MASTER <-> SLAVE sync: Loading DB in memory
1335:S 06 Mar 20:59:23.417 * MASTER <-> SLAVE sync: Finished with success
[[email protected] redis]#
Master :
[[email protected] redis]# tail /var/log/redis/redis.log
2439:M 06 Mar 20:59:22.603 * Slave asks for synchronization
2439:M 06 Mar 20:59:22.603 * Full resync requested by slave
2439:M 06 Mar 20:59:22.603 * Starting BGSAVE for SYNC with target: disk
2439:M 06 Mar 20:59:22.604 * Background saving started by pid 2445
2445:C 06 Mar 20:59:22.607 * DB saved on disk
2445:C 06 Mar 20:59:22.607 * RDB: 4 MB of memory used by copy-on-write
2439:M 06 Mar 20:59:22.656 * Background saving terminated with success
2439:M 06 Mar 20:59:22.657 * Synchronization with slave succeeded
[[email protected] redis]#

You can also use INFO REPLICATION command from redis-cli, to see the master server details, slave details, number of slaves, master_link_status etc.

[[email protected] redis]# redis-cli -h -p 6379> info replication
# Replication
[[email protected] redis]# redis-cli -h -p 6379> info replication
# Replication

That’s it! Redis Master-Slave replication setup is completed.