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 192.168.200.101 as the master server and 192.168.200.103 as the slave.

Setting up Master:

Redis is available in CentOS epel repo. Install epel.

# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

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
bind 192.168.200.101

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 http://download.redis.io/redis-stable/redis.conf

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 192.168.200.101:6379
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 192.168.200.101:6379 0.0.0.0:* LISTEN 2439/redis-server 1
[[email protected] redis]#

[[email protected] redis]# redis-cli -h 192.168.200.101 -p 6379 info server | grep uptime
uptime_in_seconds:5653
uptime_in_days:0
[[email protected] redis]#

Setting up Slave:

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

# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# 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
bind 192.168.200.103

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

slaveof 192.168.200.101 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 192.168.200.101:6379
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 192.168.200.103:6379 asks for synchronization
2439:M 06 Mar 20:59:22.603 * Full resync requested by slave 192.168.200.103:6379
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 192.168.200.103:6379 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.

Slave:
[[email protected] redis]# redis-cli -h 192.168.200.103 -p 6379
192.168.200.103:6379> info replication
# Replication
role:slave
master_host:192.168.200.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:295
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.200.103:6379>
Master:
[[email protected] redis]# redis-cli -h 192.168.200.101 -p 6379
192.168.200.101:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.200.103,port=6379,state=online,offset=131208380,lag=1
master_repl_offset:131208380
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:130159805
repl_backlog_histlen:1048576
192.168.200.101:6379>

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