Redis Master-Slave Replication Setup

To Setup Redis Master-Slave replication, the first thing is to install redis on your servers.

For this example, we are going to assume that we have three servers, 1 master with two slaves.

redis-master-1: 192.168.2.100
redis-slave-1:  192.168.2.101
redis-slave-2:  192.168.2.102

Redis installation

On each server, install Redis:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:chris-lea/redis-server 
sudo apt-get update
sudo apt-get install -y redis-server

On the master, make a backup of you config /etc/redis/redis.conf file, and create a new one with the following config:

activerehashing yes
aof-load-truncated yes
aof-rewrite-incremental-fsync yes
appendfilename "appendonly.aof"
appendfsync everysec
appendonly no
auto-aof-rewrite-min-size 64mb
auto-aof-rewrite-percentage 100
bind 0.0.0.0
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 512mb 512mb 0
client-output-buffer-limit pubsub 32mb 8mb 60
cluster-config-file "node.conf"
cluster-enabled no
cluster-node-timeout 15000
cluster-require-full-coverage no
cluster-slave-validity-factor 0
daemonize yes
databases 16
dbfilename "dump.rdb"
dir "/var/lib/redis"
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
hll-sparse-max-bytes 3000
hz 10
latency-monitor-threshold 0
list-compress-depth 0
list-max-ziplist-size -2
logfile "/var/log/redis/redis-server.log"
loglevel notice
lua-time-limit 5000
#masterauth redispwd
maxmemory 38gb #80% of total RAM
maxmemory-policy allkeys-lru
no-appendfsync-on-rewrite no
notify-keyspace-events ""
pidfile "/var/run/redis/redis-server.pid"
port 6379
protected-mode yes
rdbchecksum yes
rdbcompression yes
repl-backlog-size 64mb
repl-backlog-ttl 0
repl-disable-tcp-nodelay no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 5
repl-timeout 360
#requirepass redispwd
##Config persistence depending on your workload and needs
save 300 1000
save 900 1
set-max-intset-entries 512
slave-priority 100
slave-read-only yes
slave-serve-stale-data yes
slowlog-log-slower-than 10000
slowlog-max-len 128
stop-writes-on-bgsave-error yes
supervised no
tcp-backlog 65536
tcp-keepalive 60
timeout 0
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

Use the same Redis config  file above on replicas too.

When all servers have been configured, restart redis on each one of them.

sudo service redis-server restart

To see the role of each server, enter the following command:

redis-cli role

On the master, you should see results like this:

george.chilumbu@redis-test-1:~$ redis-cli role
1) "master"
2) (integer) 332585
3) 1) 1) "192.168.2.101"
 2) "6379"
 3) "332289"
 2) 1) "192.168.2.102"
 2) "6379"
 3) "332289"
george.chilumbu@redis-master-1:~$

On slaves-1, you should see something like this:

george.chilumbu@redis-test-2:~$ redis-cli role
1) "slave"
2) "192.168.2.100"
3) (integer) 6379
4) "connected"
5) (integer) 329709
george.chilumbu@redis-test-2:~$

And on redis-test-2, you should see similar results as in redis-slave-1.

Conclusion:

For the redis.conf files, please tune them according to your needs. Just like with MySQL, the configuration file is no one-size-fits-all. You have to perform some tests and play with the parameters to get the best results for your Redis servers.

Leave a comment