Setting Redis replication

Redis supports 1:N master-slave replication. Communcation between them is bi-directional. The following is step by step procedure. (Tested on version 4.0.6)

Test environment

2 Redis are running on a host. One is master (port : 6379) and another is slave (port : 6479)

Setting up replication

Setting up replication happens only at slaves. The following options are need to set up at slave side

  • slaveof 127.0.0.1 6379 # Required option for replication, which points master ip and port
  • slave-read-only yes # Since Redis 2.6 slaves are read only by default. If you want to make a slave writable, set “no”. But in this case, you can lose some data
  • masterauth # If master is set up password with “requirepass”, then slave must set master password

Verifying the result

Redis status can be checked by “INFO REPLICATION” command

Master status before a slave starts

# Replication
role:master
connected_slaves:0
master_replid:4d9f69cf252a8c5598079dde2957b2da79010bd6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
  • Notice “connected_slaves” and “role”

Master status after a slave starts

# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6479,state=online,offset=0,lag=1
master_replid:8773a13d4a5fa6fe872630be925cf638cae816ef
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
  • Notice that “role” is master, “connected_slaves” is 1 and slave0 is 127.0.0.1:6479
  • Master automatically detects a slave when a slave connects to the master

Slave status after replication is set up

# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:17
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8773a13d4a5fa6fe872630be925cf638cae816ef
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
  • Notice that “role” is slave and “master_host” and “master_port” is as set up

How it works

  • When a Redis starts, it runs in standalone mode (the role is master)
  • When a Redis starts with “slaveof”, it connects to the master and registers itself as a slave. At this time, master and slave recognize each other
  • From that time, synchronization starts. (More on replication)
  • Master sends ping periodically to check if slaves are ok. The interval is set as “repl_ping_slave_period” at master. (Redis config document says that ping is from slave to master, but Redis source is implemented as from master to slave)
  • master_repl_offset” value at both sides increases when 1) some data changes at master (even in standalone mode) or 2) a master sends ping to slaves
  • If a slave is shut down, master’s ping fails and the slave is deleted from the master

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.