Redis Database

How to Setup Redis Cluster from Source

What is redis

Redis is an open source in-memory database. It stores data in key-value format. Because of residing in memory, redis is an excellent tool for caching. Redis provides a rich set of data types. This gives redis upper hand over Memcached. Apart from caching, redis can be used as distributed message broker.

Redis Cluster and Sentinels

To achieve high availability, redis can be deployed in cluster along with Sentinels. Sentinel is a feature of redis. Multiple sentinels are deployed across redis clusters for monitoring purpose. When redis master goes down, sentinels elect a new master from slaves. When old master comes up again, it is added as slave.

Another use case of clustering is a distribution of load. In high load environment, we can send write requests to master and read request to slaves.

This tutorial is specifically focused on Redis Cluster Master Slave model. We will not cover data sharding across cluster here. In data sharding, keys are distributed across multiple redis nodes.

Setup for tutorial

For this tutorial, we will use 3 (virtual) servers. On one server Redis master will reside while other two servers will be used for slaves. Standard redis port is 6379. To differentiate easily, we will run master on 6379 port and slaves on
6380 and 6381 ports. Same will be applied for sentinel services. Master sentinel will listen on 16379 port while slave sentinels will be on 16380 and 16381.

Lets put this easy way.

This tutorial is tested on CentOS 6.9. For CentOS 7.X, check below Notes Section.

Installation

We will follow same installation steps for setting up of all servers. Only difference will be in configurations.

  • Step 1: Grab redis source, make and install
  • Step 2: Setup required directories
  • Step 3: Configure redis master
  • Step 4: Configure redis master sentinel
  • Step 5: Add low privileged user to run redis
  • Step 6: Setup init scripts
  • Step 7: Start service

Server 1 (Redis Master)


Install Redis

Setup required directories

Configure redis master

Edit config file /etc/redis/6379.conf in your favorite editor and change below options.

Configure redis master sentinel

Add config file for sentinel at /etc/redis/sentinel_6379.conf. Open a file and add below content

Add non-privileged user

Setup init scripts

You can find sample init scripts in Notes section below.

Start service

Server 2 (Redis Slave 1)


Install Redis

Setup required directories

Configure redis slave 1

Edit config file /etc/redis/6380.conf in your favorite editor and change below options.

Configure redis slave 1 sentinel

Add config file for sentinel at /etc/redis/sentinel_6380.conf. Open a file and add below content

Add non-privileged user

Setup init scripts

You can find sample init scripts in Notes section below. Change $HOST and $PORT values accordingly

Start service

Server 3 (Redis Slave 2)


Install Redis

Setup required directories

Configure redis slave 2

Edit config file /etc/redis/6381.conf in your favorite editor and change below options.

Configure redis slave 2 sentinel

Add config file for sentinel at /etc/redis/sentinel_6381.conf. Open a file and add below content

Add non-privileged user

Setup init scripts

You can find sample init scripts in Notes section below. Change $HOST and $PORT values accordingly

Start service

Sentinel Testing

Redis Fail-over Testing

For fail-over testing, we can take down redis-master either using init script or below command.

Also we can force sentinel to run fail over using below command

Sample init scripts

Redis Init Script

Sentinel Init Script

Notes

Security

  • NEVER EVER run redis on public interface
  • If redis is deployed in cloud environment like AWS, set up security groups/firewalls carefully. Most of times, cloud providers use ephemeral ips. Because of ephermal ips, even redis is bound to private ip, it can be accessed over public interface.
  • For more security, dangerous commands can be disabled(renamed). But be careful with them in cluster environment.
  • Redis also provides simple authentication mechanism. It is not covered here because of scope.

Sentinel management

  • During redis fail-over, config files are rewritten by sentinel program. So when restarting redis-cluster, be careful.

Sources

  • https://redis.io/topics/cluster-tutorial
  • https://redis.io/topics/security
  • https://redis.io/commands/debug-segfault

Published by

Rohit Chormale

I am software engineer from Hyderabad, India with primary interests in distributed systems. In my leisure time, I love to read technical papers and hacking emacs.

5 thoughts on “How to Setup Redis Cluster from Source”

  1. The sentinel init script has some errors:
    * the EXEC should call redis-sentinel rather than redis-server
    * the PIDFILE should be /var/run/redis/sentinel_xx.pid rather than /var/run/run/sentinel_xx.pid

    1. Thanks for comment. PIDFILE path updated.
      But ‘the EXEC should call redis-sentinel rather than redis-server’ is not exactly true. You can use either redis-sentinel or redis-server with –sentinel flag.

  2. Facing this issue while starting a server.Please help

    service redis_6381 start
    Starting redis_6381 (via systemctl): Job for redis_6381.service failed because the control process exited with error code. See “systemctl status redis_6381.servi
    ce” and “journalctl -xe” for details.
    [FAILED]
    [root@app1 redis-4.0.9]# service redis_6381 status
    Usage: {start|stop|restart}
    [root@app1 redis-4.0.9]# systemctl status redis_6381.service
    ‚óŹ redis_6381.service – SYSV: Redis is persistent key-value database
    Loaded: loaded (/etc/rc.d/init.d/redis_6381; bad; vendor preset: disabled)
    Active: failed (Result: exit-code) since Tue 2018-10-23 10:31:19 UTC; 45s ago
    Docs: man:systemd-sysv-generator(8)
    Process: 5602 ExecStart=/etc/rc.d/init.d/redis_6381 start (code=exited, status=1/FAILURE)
    Oct 23 10:31:19 app1 systemd[1]: Starting SYSV: Redis is persistent key-value database…
    Oct 23 10:31:19 app1 redis_6381[5602]: Starting Redis server…
    Oct 23 10:31:19 app1 runuser[5607]: pam_unix(runuser:session): session opened for user redis by (uid=0)
    Oct 23 10:31:19 app1 redis_6381[5602]: bash: /usr/local/bin/redis-server: No such file or directory
    Oct 23 10:31:19 app1 runuser[5607]: pam_unix(runuser:session): session closed for user redis
    Oct 23 10:31:19 app1 redis_6381[5602]: [FAILED]
    Oct 23 10:31:19 app1 systemd[1]: redis_6381.service: control process exited, code=exited status=1
    Oct 23 10:31:19 app1 systemd[1]: Failed to start SYSV: Redis is persistent key-value database.
    Oct 23 10:31:19 app1 systemd[1]: Unit redis_6381.service entered failed state.
    Oct 23 10:31:19 app1 systemd[1]: redis_6381.service failed.

Leave a Reply

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