In order to maintain fault tolerance, we must have to adopt master and slave model meaning that a master node should have 1 to N complete replicas; more replicas more fault tolerance. Fault tolerance can also be achieved in a Cluster Mode with a number of master nodes having at least one replica for each slot.

In this particular article we will setup a single master node with two slave nodes. We will also see how to switch to the slave server and set it up as a temporary master. (Switching has to be done manually here, for automatic fault tolerance one has to configure redis sentinal as well - will see in later articles)

Configure Redis Mater Slave

We are going to setup a single master node with two slave nodes.

Step1: Install Compiler for c on each machine

$sudo apt-get update
$sudo apt-get install build-essential
$sudo apt-get install tcl8.5

Step2: Download, extract and compile Redis

$ wget http://download.redis.io/releases/redis-3.2.5.tar.gz
$ tar xzf redis-3.2.5.tar.gz
$ cd redis-3.2.5
$ make

Step3: Configure and start master node
A minimal configuration file(redis.conf) for master node must have following values:

bind 127.0.0.1
port 6380

Start Master

sudo src/redis-server redis.conf

           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 7240
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               


Step4: Configure and start both slave nodes
A minimal configuration file(redis.conf) for slave node must have following values:

bind 127.0.0.1 port 6381 slaveof 127.0.0.1 6380


Start slave one by one

$sudo src/redis-server redis.conf

           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6381
 |    `-._   `._    /     _.-'    |     PID: 7252
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'           
                                 
.....
.....
7252:S 02 Sep 16:27:36.151 * MASTER <-> SLAVE sync: receiving 98 bytes from master
7240:M 02 Sep 16:27:36.151 * Synchronization with slave 127.0.0.1:6381 succeeded
7252:S 02 Sep 16:27:36.151 * MASTER <-> SLAVE sync: Flushing old data
7252:S 02 Sep 16:27:36.151 * MASTER <-> SLAVE sync: Loading DB in memory
7252:S 02 Sep 16:27:36.152 * MASTER <-> SLAVE sync: Finished with success


Step5: Verify the Master-Slave Replication

First, we connect to Redis master via our terminal, next "INFO" command is used to get all necessary details regarding the master slave setup:

$sudo src/redis-cli -h 127.0.0.1 -p 6380
$127.0.0.1:6380> INFO REPLICATION

# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=379,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=379,lag=0
master_repl_offset:379
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:378


Step6: Switching a slave to master
By configuring master slave, we should be able to handle failures in such a way that we ensure data integrity and as little downtime as possible for our application. To promote any slave to be a master we have to do following:

On a slave machine, we should connect to the Redis instance:

$sudo src/redis-cli -h 127.0.0.1 -p 6381
$127.0.0.1:6381>

1) When master is UP and Running

$127.0.0.1:6381> INFO REPLICATION

# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:169
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
....


2) Lets stop the master (127.0.0.1:6380) and see what slave says:
In slave's log you will see something like below:

3506:S 02 Sep 23:11:20.155 * MASTER <-> SLAVE sync started
3506:S 02 Sep 23:11:20.155 # Error condition on socket for SYNC: Connection refused

Lets check the replication info:

127.0.0.1:6381> INFO REPLICATION
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:505
master_link_down_since_seconds:192
slave_priority:100
slave_read_only:1
connected_slaves:0
....

Now you can see, "master_link_status:down" in info, lets make this slave temporary master:

127.0.0.1:6381> SLAVEOF NO ONE
OK
127.0.0.1:6381>

Now lets see INFO of this newly migrated to master, slave node:

127.0.0.1:6381> INFO REPLICATION
# Replication
role:master
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

Step7: Switching a master to slave again
Now if the master is up, we can switch the temporary master to slave of original master again:

127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
127.0.0.1:6381>

Now the working slave request for a synchronization to the master and on master node logs we can see something like this printed.

3819:M 02 Sep 23:29:20.595 * Slave 127.0.0.1:6381 asks for synchronization
3819:M 02 Sep 23:29:20.595 * Full resync requested by slave 127.0.0.1:6381
3819:M 02 Sep 23:29:20.596 * Background saving started by pid 3872
3872:C 02 Sep 23:29:20.638 * DB saved on disk
3872:C 02 Sep 23:29:20.638 * RDB: 8 MB of memory used by copy-on-write
3819:M 02 Sep 23:29:20.689 * Background saving terminated with success
3819:M 02 Sep 23:29:20.689 * Synchronization with slave 127.0.0.1:6381


When master(6380) was down no node was accepting request, we migrated slave1 (6381) to master and it started serving requests; in the meantime connection was lost.

When master was manually up, another slave(6382) started synchronization with its master, migrated master (6381) was switched to slave of master(6380) to start synchronization again.

In this particular article we have seen how to setup a single master node with two slave nodes. We have also seen how to switch to the slave server and set it up as a temporary master. In upcoming articles we will see more about redis configuration and connection with different clients.
  • By Techburps.com
  • Sep 2, 2017
  • Big Data