Focused Topics in Redis Day 1 - #3

Master-Slave Replication

Redis replication is based on master Redis instances that have their contents mirrored with one or more Redis slave instances.

Facts about Master-Slave Replication

Basic Replication

To illustrate the basic operations of using Redis replication, we will start-up two running Redis instances and two redis-cli sessions connecting to the two instances.

Open a terminal session and for this example we will use the unix screen utility to run all of our sessions. If you don't have screen installed, you can open four individual terminal sessions.

Redis Master

$ ~/redis/src/redis-server --dbfilename master.rdb --port 6379

Redis CLI session of Redis Master

$ ~/redis/src/redis-cli -p 6379> DBSIZE
(integer) 0

Next, we will add some field-values to a Redis hash key in our Master datastore> HMSET Book:1 name "Infinite Jest" author "David Foster Wallace"

Redis Slave

$ ~/redis/src/redis-server --dbfilename slave.rdb --port 6380

Redis CLI session of Redis Slave

$ ~/redis/src/redis-cli -p 6380> DBSIZE
(integer) 0

Now, we'll issue the SLAVEOF command to make this instance replicate the master's content:> SLAVEOF localhost 6379

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:> KEYS *
1) "Book:1"> 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:> HSET Book:1 copyrightYear 1996
(error) READONLY You can't write against a read only slave.

SYNC, Partial Resynchronization & Diskless Replication

Redis Replication

  1. The master then starts background saving, and starts to buffer all new commands received that will modify the dataset.
  2. When the background saving is complete, the master transfers the database file to the slave, which saves it on disk, and then loads it into memory.
  3. The master will then send to the slave all buffered commands. This is done as a stream of commands and is structured in the Redis.

Partial Resynchronization

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.

Diskless replication

An experimental feature that sends the full RDB file to the slave without forking the master and using the disk as an intermediate storage.

Critical Commands for Master-Slave Replication

Caveats when using Replication

If persistence is turned off on the master A and two slaves B,C the following problem can occur:

Configuring Master-Slave Replication

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.

Runtime> SLAVE OF localhost 6379

Use Case: Linked Data Fragments Server