redis cache clustering with failover

Hi Techrunnr Readers, Today document deals with redis cache clustering with failover.

Redis is an open-source in-memory database project implementing a distributed, in-memory key-value store with optional durability. Redis is mainly used to store cache hence results in application performance. It is used as a message broker.


So setting up a standalone Redis server is a single point of failure. Standalone setup is ok for the Dev Environment but when we are moving to a production environment it’s highly recommended to have a High available system.

Redis supports clustering and automatic failover using an extra component called Redis Sentinel.
Redus uses 6379 and sentinel uses 26379 as the port numbers, so enable these ports in the firewall.

Now let’s see how to achieve this,

Let’s consider we have 3 servers where Redis and Redis Sentinel installed.
Use the following command to install both the services,

1. Add the repository

apt-add-repository ppa:chris-lea/redis-server

2. Update the System with the new repository

apt-get update
apt-get install redis-server -y

apt-get install redis-sentinel

So after installing let’s move to the configuration part.

Redis Sentinel is one of the major parts of Redis cluster failover. It detects the cluster failover and brings backs to the stable state with any human intervention.
Sentinel detects the current master and master details sent to the client application. Sentinel always check which is the master node, if it detects any failure to master node, it does the failover operation.

Redis cluster with failover uses master-slave architecture, where the only primary server has read and write operation, and other servers(secondary) remains read-only.

Electing the new server is based upon the quorum results. Quarom is the number of sentinels that need to agree about the fact the master is not reachable. However, the quorum is only used to detect the failure. In order to actually perform a failover, one of the Sentinels needs to be elected leader for the failover and be authorized to proceed.

Consider the below image, here we have host2 as the master and other as slaves.

Here is the configuration to Redis cluster failover,

Host1 (10.0.0.251):
vi /etc/redis/redis.conf

bind 0.0.0.0

protected-mode no

slaveof 10.0.0.249 6379

sentinel.conf

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.249 6379 2

sentinel down-after-milliseconds mymaster 5000

host 2(10.0.0.249):

In redis.conf

bind 0.0.0.0

protected-mode no

In sentinel.conf

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.249 6379 2

sentinel down-after-milliseconds mymaster 5000

host 3(10.0.0.248):
redis.conf

bind 0.0.0.0

protected-mode no

slaveof 10.0.0.249 6379

sentinel.conf

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.249 6379 2

sentinel down-after-milliseconds mymaster 5000

Where mymaster is the cluster name, monitor mymaster 172.17.0.117 6379 2 means sentinel will check the host2 redis master is available or not and the number of sentinels need to agree master is down is 2.
down-after-milliseconds mymaster 5000 defines after 5seconds the master node is down, then it moves to failover.

slaveof tells Redis cluster to make this particular server instance as a SLAVE instance of the given MASTER node.

Now, let’s start the Redis and Sentinel service,

service redis-server start

service redis-sentinel start

Now if you check the replication status on master and slave as follows,

Master


Slave:

if you stop any Redis server, from the sentinel logs you can see master server got switched from node2 to node3.

That’s it!!!

Hope you all understood the Redis cluster with failover.

© 2018, Techrunnr. All rights reserved.

#1
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

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

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.