[Walkthrough] Install and set up Redis

Check system info.

cat /etc/os-release

Follow a walkthrough listed here. The below works on CentOS 7.

Installing

Install EPEL (Extra Packages for Enterprise Linux).

sudo yum install epel-release

Install Redis. Use the -y option to automatically answer yes to installing additional software dependencies.

sudo yum install redis -y

Start Redis and check the status.

sudo systemctl start redis.service
sudo systemctl status redis.service

Configuring

The configuration file is located at /etc/redis.conf, and it is self-documented.

  • Comment the line bind 127.0.0.1 to allow connections from anywhere.
  • protected-mode yes -> protected-mode no
  • Uncomment the line # requirepass foobared, and replace foobared with a password. You may generate a simple password like this:
echo "digital-ocean" | sha256sum
  • daemonize no -> daemonize yes
  • # maxmemory <bytes> -> maxmemory 3GB for example
  • # maxmemory-policy noeviction -> maxmemory-policy volatile-lru for example

After configuring, save and close the file then restart Redis:

sudo systemctl restart redis.service

Alternatively, start with

sudo redis-server /etc/redis.conf

Remember to check advice for configuring and managing Redis in production (refer to this gist).

Set the Linux kernel overcommit memory setting to 1. Add vm.overcommit_memory = 1 to /etc/sysctl.conf. Then, reboot or run the command sysctl vm.overcommit_memory=1 to activate the setting.

Managing

Redis 主从, 哨兵, 和集群这三个有什么区别?

Replication (master-slave)

Redis replication: How Redis supports high availability and failover with replication

(Show more »)

The replica will automatically reconnect to the master every time the link breaks, and will attempt to be an exact copy of it regardless of what happens to the master.

Redis uses by default asynchronous replication with low latency and high performance.

Since Redis 2.6 by default slaves are read-only.

REPLICATION section in conf file.

In setups where Redis replication is used, it is strongly advised to have persistence turned on in the master and in the replicas.

Configuring master

  • appendonly no -> appendonly yes for additional persistence

Configuring slave

Uncomment and configure the following lines

  • # slaveof <masterip> <masterport>
  • # masterauth <master-password>

By default, a replica will ignore maxmemory. It means that the eviction of keys will be handled by the master, sending the DEL commands to the replica as keys evict in the master side.

High availability with Sentinel

High availability with Redis Sentinel: High availability for non-clustered Redis

Start Sentinel.

redis-server /etc/sentinel.conf --sentinel

It is mandatory to use a configuration file when running Sentinel, as this file will be used by the system in order to save the current state that will be reloaded in case of restarts.

Sentinels by default run listening for connections to TCP port 26379.

The sample configuration file is located at /etc/redis-sentinel.conf.

  • Add daemonize yes. Note that it is not documented in early version.
  • Uncomment # protected-mode no.
  • Set sentinel monitor <master-name> <ip> <redis-port> <quorum> instead of the default sentinel monitor mymaster 127.0.0.1 6379 2 if needed. It tells Sentinel to monitor this master, and to consider it in O_DOWN (Objectively Down) state only if at least sentinels agree.
    • Slaves are auto-discovered, so you don’t need to specify slaves in any way.
    • The quorum is only used to detect the failure. In order to actually perform a failover, one of the Sentinels need to be elected leader for the failover and be authorized to proceed. This only happens with the vote of the majority of the Sentinel processes.
  • Set sentinel auth-pass <master-name> <password>. Note that the master password is also used for slaves, so it is not possible to set a different password in masters and slaves instances if you want to be able to monitor these instances with Sentinel.

One of the capabilities of Sentinel is automatic failover. You need at least three Sentinel instances for a robust deployment. See Example Sentinel deployments for further explanation.

Scaling (cluster)

Scaling with Redis Cluster: Horizontal scaling with Redis Cluster

Note that the minimal cluster that works as expected must contain at least three master nodes. For deployment, we strongly recommend a six-node cluster, with three masters and three replicas. Source

Not covered in this post.

Sample Makefile

SHELL := /bin/bash

SOURCE_HOST := USER@HOST
MASTER_CONF_PATH := /etc/redis.conf
SLAVE_CONF_PATH := /etc/redis-slave.conf
SENTINEL_CONF_PATH := /etc/redis-sentinel.conf

.PHONY: install, pull
install:
	sudo yum install epel-release
	sudo yum install redis -y
pull:
	for path in "${MASTER_CONF_PATH}" "${SLAVE_CONF_PATH}" "${SENTINEL_CONF_PATH}"; do \
	  sudo rsync --verbose "${SOURCE_HOST}":"$${path}" "$${path}"; \
	done

.PHONY: master, slave, stop
master: stop
	sudo redis-server "${MASTER_CONF_PATH}"
	sudo redis-server "${SENTINEL_CONF_PATH}" --sentinel
slave: stop
	sudo redis-server "${SLAVE_CONF_PATH}"
	sudo redis-server "${SENTINEL_CONF_PATH}" --sentinel
stop:
	-pgrep -f "redis-server" | xargs sudo kill -9