Redis replication is based on master Redis instances that have their contents mirrored with one or more Redis slave instances.
To illustrate the basic operations of using Redis replication, we
will start-up two running Redis instances and two
connecting to the two instances.
Open a terminal session and for this example we will use the unix
utility to run all of our sessions. If you don't have
you can open four individual terminal sessions.
$ ~/redis/src/redis-server --dbfilename master.rdb --port 6379
$ ~/redis/src/redis-cli -p 6379 127.0.0.1:6379> DBSIZE (integer) 0
Next, we will add some field-values to a Redis hash key in our Master datastore
127.0.0.1:6379> HMSET Book:1 name "Infinite Jest" author "David Foster Wallace" OK
$ ~/redis/src/redis-server --dbfilename slave.rdb --port 6380
$ ~/redis/src/redis-cli -p 6380 127.0.0.1:6380> DBSIZE (integer) 0
Now, we'll issue the SLAVEOF command to make this instance replicate the master's content:
127.0.0.1:6380> SLAVEOF localhost 6379 OK
After adding the
Book:1 Redis hash to our Master instance,
we'll check to see if the key has been replicated to our slave instance:
127.0.0.1:6380> KEYS * 1) "Book:1" 127.0.0.1:6380> HGETALL Book:1 1) "name" 2) "Infinite Jest" 3) "author" 4) "David Foster Wallace"
The default mode for Redis slaves is readonly, meaning that if we issue any Redis commands that change the value stored at a key, we'll receive the following:
127.0.0.1:6380> HSET Book:1 copyrightYear 1996 (error) READONLY You can't write against a read only slave.
Replication is non-blocking on both master and slave instances; a master can continue to respond to queries while slaves are synchronizing, a slave can be configured to respond to queries with old data while synchronizing with the master
If the link between the master and slave goes down, the old behavior of Redis is to do a full synchronization with the slave when the connection is re-established. As of Redis 2.8 experimental support was added with PSYNC command that will only do a partial synchronization with MASTER based on if a time threshold has been exceeded or not. If time has been exceeded, a full SYNC is triggered between the master and slave.
An experimental feature that sends the full RDB file to the slave without forking the master and using the disk as an intermediate storage.
If persistence is turned off on the master A and two slaves B,C the following problem can occur:
Master-Slave replication can be set-up in the redis.conf file for each slave.
# Master-Slave replication. Use slaveof to make a Redis instance a copy of # another Redis server. slaveof
127.0.0.1:6379> SLAVE OF localhost 6379