Author : Rajeswari Menon

Published on: October 1, 2019

Category : Technical Feed

Redis Sentinel

INTRODUCTION TO REDIS

Redis is an open-source (BSD licensed), in-memory data structure store – where data is not written to disk but kept in volatile RAM. It is also used as a database, cache and message broker.

Features of Redis

Fast: Redis is extremely fast as it stores the whole dataset in primary memory.

Persistence: Based on the number of updates or elapsed time, data can be asynchronously saved to disk.

Data replication: Master-slave cache nodes can be set up. Slave node always listens to master i.e. when master node is updated, slave is updated automatically.

Clients available in all popular programming languages: Redis client API is developed in almost all popular languages like C, C++, Java, JavaScript, Python etc.

Lightweight: Redis is written in ANSI C, with no external dependencies.

Snapshotting: Redis by default stores snapshots of your data in dumb.rdb file. In the case of server restart, Redis will load the data from a backup file and put them into memory.

High Availability: To ensure high availability data, there is built-in support for asynchronous master/slave replication, for which Redis Sentinel can be used.

REDIS SENTINEL

Redis Sentinel is a system that can resist Redis deployment without human intervention.

Capabilities of Redis Sentinel

Monitoring: Check all instances i.e. master and slave are working properly.

Notification: If one of the Redis instances are not working as expected, Sentinel can notify system administrators or another program through an API.

Automatic failover: If the master is not working as expected, Sentinel promotes one of the slaves as master and then make the additional slaves use the new master.

Sentinel is the source of authority for clients. The client connects to Sentinel for asking the address of the current Redis master.

How Sentinels work

The idea of Sentinel is that it’s an independent node that keeps track on master node and other slave nodes. In a master-slave scenario, master is meant for writing and slaves are for reading. When the master goes down, a slave node will become a master and it will have the capability to do both reading and writing. All other slaves will become slave to the new master.

A simple example on Redis Sentinel in Windows

The Redis project does not officially support windows. However, Redis has been ported to Windows (64 bit) by Microsoft Open Technology Project. Install Redis on your machine. The default installation folder is ‘C:\Program Files\Redis’.

This example consists of 3 Redis instances and one Sentinel instance. For the sake of easiness, all these instances are hosted in one machine itself.

Redis_Master = Master instance

Redis_Slave1 = Slave 1 instance i.e. slave of Redis_Master

Redis_Slave2 = Slave 2 instance i.e. slave of Redis_Master

Sentinel = Sentinel instance

To deploy 3 Redis instances, create three copies of ‘redis.windows.conf’ file in ‘C:\Program Files\Redis’ as below

redis.windows-service-redis1.conf

  • Give port as 6379 (default port of Redis)

redis.windows-service-redis2.conf

  • Given port as 6380
  • Replace slaveof <masterip> <masterport> with slaveof 127.0.0.1 6379

redis.windows-service-redis3.conf

  • Given port as 6381
  • Replace slaveof <masterip> <masterport> with slaveof 127.0.0.1 6379

To deploy Sentinel instance, create sentinel.conf file in ‘C:\Program Files\Redis’ as below

sentinel.conf

port 26379

sentinel monitor Redis_Master 127.0.0.1 6379 1

sentinel down-after-milliseconds Redis_Master 30000

sentinel parallel-syncs Redis_Master 1

sentinel failover-timeout Redis_Master 180000

The syntax for above configuration is

sentinel monitor MasterName RedisMasterServerIP RedisMasterPort Quorum

sentinel down-after-milliseconds MasterName  DownTimeout

sentinel failover-timeout MasterName  FailoverTimeout

sentinel parallel-syncs MasterName  ConcurrentServerSync

where

Line 1 – tells the master to be monitored. The quorum is used to detect failures. When the master is not reachable, the number of sentinels in the quorum should agree about it.

Line 2 – master will be considered as down when it’s not reachable for the Sentinel for the time specified in milliseconds.

Line 3 – The timeout in milliseconds indicates the time that Sentinel will wait after a failover before starting a new failover.

Line 4 – The number of slaves that can synch with the new master at the same time after failover.

Let’s create 3 Redis instances and one Sentinel instance as windows service. The below commands needs to be run in command prompt (Run as administrator) to do the same:

Redis Master and Slaves

  • sc create Redis_Master DisplayName= “Redis_Master” binPath= “\”C:\Program Files\Redis\redis-server.exe\” –service-run \”C:\Program Files\Redis\redis.windows-service-redis1.conf\”
  • sc create Redis_Slave1 DisplayName= “Redis_Slave1” binPath= “\”C:\Program Files\Redis\redis-server.exe\” –service-run \”C:\Program Files\Redis\redis.windows-service-redis2.conf\”
  • sc create Redis_Slave2 DisplayName= “Redis_Slave2” binPath= “\”C:\Program Files\Redis\redis-server.exe\” –service-run \”C:\Program Files\Redis\redis.windows-service-redis3.conf\”

Redis Sentinel

sc create Redis_Sentinel DisplayName= “Redis_Sentinel” binPath= “\”C:\Program Files\Redis\redis-server.exe\” –service-run \”C:\Program Files\Redis\sentinel.conf\” –sentinel

Start Master, Slaves and Sentinel services respectively from Windows Services (services can be opened by through run [windows key + R] by giving services.msc)

The status of Sentinel can be checked through redis-cli using given below command:

redis-cli -p 26379 info sentinel
Redis Master Service

As you see, Sentinel is monitoring ‘Redis_Master’ and its status is Ok.

To know the current master, the following command can be used:

redis-cli –p 26379 sentinel get-master-addr-by-name Redis_Master

Redis Master Sentinel

Now stop Redis_Master service from Windows Services. Execute the above command again after 30 seconds. You can see that one of the slaves is re-configured as Master.

Redis Master Service

Points to be considered before deploying Redis Sentinel

  1. For a robust deployment, at least three sentinel instances are needed.
  2. The three instances should be in different computers or virtual machines that are believed to be failed in independently.
  3. Sentinel should be supported in the client library.
  4. Docker or other forms of port mapping should be mixed with care as it will break the auto-discovery of other sentinels and a list of slaves.

Redis is designed to be accessed by trusted clients inside trusted environments. Contact us today, if you are willing to scale up your business; we offer Redis hosting and fully managed solutions on a cloud of your choice.

Author Bio:

Content

Rajeswari Menon: Rajeswari has been working at ThinkPalm as Senior Technical Lead.  She is passionate about Java Programming and her hobbies include dancing.

FacebooktwitterlinkedinFacebooktwitterlinkedin