How to Install a Redis Server on Ubuntu or Debian 8
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
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.
Since Redis serves all data from memory, we recommend using a High Memory Linode with this guide.
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
Install the
software-properties-common
package:sudo apt-get install software-properties-common
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 the second half of this guide, you will need at least two Linodes.
Install Redis
Ubuntu
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
Debian
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.list
and copy the appropriate mirror information to it:- File: /etc/apt/sources.list.d/dotdeb.list
1 2
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 redis-server
package:
sudo apt-get update
sudo apt-get install redis-server
Verify the Installation
Verify Redis is up by running redis-cli
:
redis-cli
Your prompt will change to 127.0.0.1:6379>
. Run the command ping
, which should return a PONG
:
127.0.0.1:6379>ping
PONG
Enter exit
or press Ctrl-C to exit from the redis-cli
prompt.
Configure Redis
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
andappendfsync
settings inredis.conf
:- File: /etc/redis/redis.conf
1 2
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 = 1
to/etc/sysctl.conf
:- File: /etc/sysctl.conf
1
vm.overcommit_memory = 1
Distributed Redis
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 master
and slave
.
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
master
Linode’s private IP address fromslave
. You will use only private addresses for replication traffic for security reasons.Configure the
master
Redis instance to listen on a private IP address by updating thebind
configuration option inredis.conf
. Replace192.0.2.100
with themaster
Linode’s private IP address- File: /etc/redis/redis.conf
1
bind 127.0.0.1 192.0.2.100
Restart
redis-server
to apply the changes:sudo service redis-server restart
Configure the Slave Linode
Configure a slave instance by adding the
slaveof
directive intoredis.conf
to setup the replication. Again replace192.0.2.100
with themaster
Linode’s private IP address:- File: /etc/redis/redis.conf
1
slaveof 192.0.2.100 6379
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.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.
Confirm Replication
Test that the replication works. On 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.
Then, run redis-cli
on slave
and execute get 'a'
, which should return the same value as that on master
:
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.
More Information
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 page was originally published on