Deploy Redis on Ubuntu or Debian 8
Updated by Edward Angert Contributed by Sergey Pariev
This is a Linode Community guide. Write for us and earn $250 per published guide.
Redis is an open-source, in-memory, data-structure store with optional disk writes for persistence, which can be used as key-value database, 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.
Before You Begin
Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.
Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.
Update your system:
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install software-properties-common
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo. If you’re not familiar with the
sudocommand, you can check our Users and Groups guide.
To utilize the replication steps in the second half of this guide, you will need at least two Linodes.
The Redis package in the Ubuntu repositories is outdated and lacks several security patches; consequently, we’ll use a third-party PPA for installation.
Add the Redis PPA repository to install the latest version:
sudo add-apt-repository ppa:chris-lea/redis-server
Dotdeb is a popular, third-party repository for Debian users looking for newer versions of the LAMP stack and related software than what’s been provided by Debian.
Review the list of mirrors Dotdeb provides and select the one closest to your Linode.
Create the file
/etc/apt/sources.list.d/dotdeb.listand copy the appropriate mirror information to it:
deb http://ftp.utexas.edu/dotdeb/ stable all deb-src http://ftp.utexas.edu/dotdeb/ stable all
Download and install the GPG key, as documented in the Dotdeb instructions:
wget https://www.dotdeb.org/dotdeb.gpg sudo apt-key add dotdeb.gpg
Update and Install
Update packages and install
sudo apt-get update sudo apt-get install redis-server
Verify the Installation
Verify Redis is up by running
Your prompt will change to
127.0.0.1:6379>. Run the command
ping, which should return a
exit or press Ctrl-C to exit from the
Configure Persistence Options
Redis provides two options for disk persistence:
- Point-in-time snapshots of the dataset, made at specified intervals (RDB)
- Append-only logs of all the write operations performed by the server (AOF).
Each option has its own pros and cons, which are detailed in 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 setup AOF persistence.
Make sure that the following values are set for
appendonly yes appendfsync everysec
Restart Redis with:
sudo service redis-server restart
Basic System Tuning
To improve Redis performance, make the following adjustment to the Linux system settings.
Set the Linux kernel overcommit memory setting to 1:
sudo sysctl vm.overcommit_memory=1
This immediately changes the overcommit memory setting. To make the change permanent, add
vm.overcommit_memory = 1to
vm.overcommit_memory = 1
Redis provides several options for setting up distributed data stores. The simplest option, covered below, is the master/slave replication, which creates a real-time copy (or multiple copies) of master/server 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 a new master.
Starting with Redis version 3.0, 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.
Set Up Master/Slave Replication
Prepare Two Linodes and Configure the Master Linode
For this section of the guide, you will use two Linodes, respectively named
Set up both Linodes with a Redis instance, using Redis Installation and Redis Configuration steps from this guide. You can also copy your initially configured disk to another Linode using the Clone option in the Linode Manager.
Configure Private IP Addresses on both Linodes, and make sure you can access the
masterLinode’s private IP address from
slave. You will use only private addresses for replication traffic for security reasons.
masterRedis instance to listen on a private IP address by updating the
bindconfiguration option in
masterLinode’s private IP address
bind 127.0.0.1 192.0.2.100
redis-serverto apply the changes:
sudo service redis-server restart
Configure the Slave Linode
Configure a slave instance by adding the
redis.confto setup the replication. Again replace
masterLinode’s private IP address:
slaveof 192.0.2.100 6379
slaveofdirective 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.
Restart the slave Redis instance:
sudo service redis-server restart
After restart, the
slave Linode will attempt to synchronize its data set to
master and then propagate the changes.
Test that the replication works. On master Linode, run
redis-cli and execute command
set 'a' 1
1 2 3
redis-cli 127.0.0.1:6379> set 'a' 1 OK
exit or press Ctrl-C to exit from
slave and execute
get 'a', which should return the same value as that on
1 2 3
redis-cli 127.0.0.1:6379> get 'a' "1"
Your master/slave replication setup is working properly.
Secure the Redis Installation
Since Redis is designed to work in trusted environments and with trusted clients, you should take care of controlling access to the Redis instance. Security methods include:
Setting up a firewall using iptables.
Encrypting Redis traffic, using an SSH tunnel or the methods described in the Redis Security documentation.
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.
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.
This guide is published under a CC BY-ND 4.0 license.