Redis is an open-source, in-memory, data structure store with optional disk writes for persistence. It can be used as a key-value database, or as a cache and message broker. Redis features built-in transactions, replication, and support for a variety of data structures such as strings, hashes, lists, sets, and others. Redis can be made highly available with Redis Sentinel and supports automatic partitioning with Redis Cluster.
This document provides both instructions for deploying the Redis server, and an overview of best practices for maintaining Redis instances on CentOS 7. Since Redis serves all data from memory, we recommend using a high memory Linode with this guide.
sudo yum update
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide. To utilize the replication steps in this guide, you will need at least two Linodes.
In this section you’ll add the EPEL repository, and then use it to install Redis.
sudo yum install epel-release
sudo yum update
sudo yum install redis
sudo systemctl start redis
Optional: To automatically start Redis on boot:
sudo systemctl enable redis
Verify that Redis is running with redis-cli
:
redis-cli ping
If Redis is running, it will return:
PONG
In this section, you’ll configure some basic persistence and tuning options for Redis.
Redis provides two options for disk persistence:
Each option has its own pros and cons which are detailed in the Redis documentation. For the greatest level of data safety, consider running both persistence methods.
Because the Point-in-time snapshot persistence is enabled by default, you only need to set up AOF persistence:
appendonly
and appendfsync
settings in redis.conf
:12 | appendonly yesappendfsync everysec |
---|
sudo systemctl restart redis
To improve Redis performance, set the Linux kernel overcommit memory setting to 1:
sudo sysctl vm.overcommit_memory=1
This immediately changes the overcommit memory setting, but the change will not persist across reboots. To make it permanent, add vm.overcommit_memory = 1
to /etc/sysctl.conf
:
1 | vm.overcommit_memory = 1 |
---|
Depending upon your usage, you may find it necessary to add extra swap disk space. You can add swap by resizing your disk in the Linode Manager. The Redis documentation recommends the size of your swap disk match the amount of memory available to your system.
Redis provides several options for setting up distributed data stores. The simplest option, covered below, is master/slave replication, which creates copies of data. It will also allow distribution of reads among groups of slave copies as long as all write operations are handled by the master server.
The master/slave setup described above can be made highly available with Redis Sentinel. Sentinel can be configured to monitor both master and slave instances, and will perform automatic failover if the master node is not working as expected. That means that one of the slave nodes will be elected master and all other slave nodes will be configured to use the new master.
With Redis version 3.0 and above, you can use Redis Cluster, a data sharding solution that automatically manages replication and failover. With Redis Cluster, you are able to automatically split your dataset among multiple nodes, which is useful when your dataset is larger than a single server’s RAM. It also gives you the ability to continue operations when a subset of the nodes are experiencing failures or are unable to communicate with the rest of the cluster.
The following steps will guide you through master/slave replication, with the slaves set to read-only mode.
For this section, you will use two Linodes, a master and a slave.
NoteTo communicate over the private network, your master and slave Linodes must reside in the same data center.
bind
configuration option in redis.conf
. Replace 192.0.2.100
with the master Linode’s private IP address:1 | bind 127.0.0.1 192.0.2.100 |
---|
sudo systemctl restart redis
slaveof
directive into redis.conf
to setup the replication. Again replace 192.0.2.100
with the master Linode’s private IP address:
The slaveof
directive takes two arguments: the first is the IP address of the master node; the second is the Redis port specified in the master’s configuration.1 | slaveof 192.0.2.100 6379 |
---|
sudo systemctl restart redis
After restarting, the slave Linode will attempt to synchronize its data set to master and then propagate the changes.Test that the replication works. On your master Linode, run redis-cli
and execute command set 'a' 1
redis-cli
127.0.0.1:6379> set 'a' 1
OK
Type exit
or press Ctrl-C to exit from redis-cli
prompt.
Next, run redis-cli
on the slave Linode and execute get 'a'
, which should return the same value as that on the master:
redis-cli
127.0.0.1:6379> get 'a'
"1"
Your master/slave replication setup is working properly.
Since Redis is designed to work in trusted environments and with trusted clients, you should control access to the Redis instance. Some recommended security steps include:
Additionally, to ensure that no outside traffic accesses your Redis instance, we suggest that you only listen for connections on the localhost interface or your Linode’s private IP address.
For an added layer of security, use password authentication to secure the connection between your master and slave Linodes.
requirepass
line in your Redis configuration and replace master_password
with a secure password:1 | requirepass master_password |
---|
sudo systemctl restart redis
masterpass
, and then create a unique password for the slave Linode with requirepass
:
Replace master_password
with the password you configured on your master, and replace slave_password
with the password to use for your slave Linode.12 | masterpass master_passwordrequirepass slave_password |
---|
sudo systemctl restart redis
redis-cli
on your master Linode, and use AUTH
to authenticate with your master password:
redis-cli
127.0.0.1:6379> AUTH master_password
INFO
. This provides a lot of information, so you can specifically request the “Replication” section in your command:
127.0.0.1:6379> INFO replication
Output should be similar to the following:
# Replication
role:master
connected_slaves:1
slave0:ip=192.0.2.105,port=6379,state=online,offset=1093,lag=1
It should confirm the master role of your Linode, as well as how many slave Linodes are connected to it.redis-cli
and authenticate using your slave password:
redis-cli
127.0.0.1:6379> AUTH slave_password
INFO
to confirm your slave Linode’s role, and its connection to the master server:
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:192.0.2.100
master_port:6379
master_link_status:up
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.