title: "[Walkthrough] Install and set up Redis" categories: Tech updated: 2023-01-04 comments: true
Check system info.
cat /etc/os-release
Follow a walkthrough listed here. The below works on CentOS 7.
<!-- more -->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
The configuration file is located at /etc/redis.conf
, and it is self-documented.
bind 127.0.0.1
to allow connections from anywhere.protected-mode yes
-> protected-mode no
# 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 exampleAfter 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 commandsysctl vm.overcommit_memory=1
to activate the setting.
Redis replication: How Redis supports high availability and failover with replication
<details><summary><b></b><font color="deepskyblue"> (Show more »)</font></summary> <p>The replica will automatically reconnect to the master every time the link breaks, and will attempt to be an exact copy of it <em>regardless</em> of what happens to the master.</p> <p>Redis uses by default asynchronous replication with low latency and high performance.</p> <p>Since Redis 2.6 by default slaves are read-only.</p></details>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 persistenceConfiguring 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 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
.
daemonize yes
. Note that it is not documented in early version.# protected-mode no
.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 <quorum> sentinels agree. 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 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.
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