Redis Tutorial Part 2 – Redis Sentinel and master/slave usage with Node.js ioredis

April 20, 2019

Redis Sentinel 

System designed to help manage Redis instances. As we discussed earlier, one Redis instance can be master and other instances can be slaves. The slaves can have exact data copies of the master server. The master will have replication ID. The biggest advantage of this architecture is that the slave can act as a master if the master fails. Redis Sentinel is the system that helps to make Redis instances with High Availability (HA) by managing failovers.

Redis Sentinel Does the following jobs

Monitoring – Checking if your master and slave instances are working as expected.

Notification – Notifying something went wrong. May be via an API.

Automatic failover – If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master

 

 

The configuration file for sentinel is sentinel.conf 

redis-server /path/to/sentinel.conf –sentinel

 

In the sentinel.conf file, there is a line like

sentinel monitor mymaster 127.0.0.1 6379 2

quorum – 2

This line determines which master the sentinel is going to monitor. The number 2 is called quorum. The number determines how many sentinels need to agree that the master is having a Failover so that the slave can be promoted as the new master.

The sentinel.conf file has a line like port 26379 which will determine on which port the sentinel instance will run
MAKE SURE the instance is protected from the outside world via firewalling or other means.

So now we have our monitoring the sentinel instance running on port 26379. This sentinel will monitor sentinel mymaster 127.0.0.1 6379 2

Create 3 new sentinels running on ports 26379,26380 and 26381 so that two of them will have the quorum decision.

Get the Redis instance with sentinels go to the following link  http://download.redis.io/releases/redis-5.0.4.tar.gz

sentinel

sentinel

Creating multiple Redis instances on different ports master and slave on the LINUX machine

sudo apt-get install redis-server

The Redis configuration file is in /etc/redis/redis.conf

to open the file use sudo gedit /etc/redis/redis.conf.You can see the port is in port 6379

 

Now we create a slave running in 6380

Create slave file

cp redis.conf slave.conf

This will copy the redis.conf to slave.conf

Now open the slave.conf file using command

sudo gedit /etc/redis/slave.conf

change port to 6380

# slaveof <masterip> <masterport>
line to be modified as slave of 127.0.0.1 6379

This means that the server running on 6380 is the slave of the instance running on 6379

Starting the Redis Master-Slave
/usr/bin/redis-server /etc/redis/redis.conf
/usr/bin/redis-server /etc/redis/slave.conf

Now we have slave running on port 6380 and master running on port 6379

Example code for Nodejs with SENTINELS

How do sentinels work

SDOWN AND ODOWN

SDOWN – Determined by a single sentinel that the master is failing. SDOWN is broadcasted to all sentinels which confirms that the master is down when the number of sentinels reaches quorum which in turn changes the error status promoted to ODOWN  . When ODOWN has reached the slave with the highest priority is selected as the new master. The config files are rewritten accordingly.

 

This is just an example code on how to run sentinels. You need to create the sentinels and master-slave instances on different nodes to achieve high availability in an ideal way. This just gives an example on how to set it up on a single LINUX machine

If you don’t start the sentinels and run the above ioredis code you will get the following error

Unhandled error event: Error: All sentinels are unreachable. Retrying from scratch after 460ms. Last error: Connection is closed.

 

So start the sentinels, start the master instance and slave instances while testing your code always

 

5 Comments

  • Adelia Surrett April 21, 2019 at 9:54 am

    Thank you for truly being simply just respectful for making a choice relating to a number of essential items an incredible number of everyone

  • hilario ventura April 29, 2019 at 9:43 am

    Pretty properly published tips. It can be useful to everyone that uses this,
    this includes all of us. Keep doing your work As can’r simply
    wait to read more items.

  • Rosaria May 6, 2019 at 7:56 am

    I spent a great deal of time to find something like this

  • Winston Weeks May 7, 2019 at 12:39 am

    I really fully grasp this report. I have been looking right
    across for this particular! Thank heavens I recently found
    doing it on the subject of Request. You’ve
    prepared my best morning! Thx yet again

  • reinaldo rudd May 15, 2019 at 2:20 am

    You’re fantastic Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *