How to Install a Redis Server on Ubuntu or Debian 8

Select distribution:
Traducciones al Español
Estamos 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.
Deprecated

This guide has been deprecated and is no longer being maintained.

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

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.

  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

  3. Install the software-properties-common package:

    sudo apt-get install software-properties-common
    
Note

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 sudo 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.

  1. Review the list of mirrors Dotdeb provides and select the one closest to your Linode.

  2. 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
  3. 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.

  1. Make sure that the following values are set for appendonly and appendfsync settings in redis.conf:

    File: /etc/redis/redis.conf
    1
    2
    
    appendonly yes
    appendfsync everysec
  2. Restart Redis with:

    sudo service redis-server restart
    

Basic System Tuning

To improve Redis performance, make the following adjustment to the Linux system settings.

  1. Set the Linux kernel overcommit memory setting to 1:

    sudo sysctl vm.overcommit_memory=1
    
  2. 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.

  1. 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 Cloud Manager.

  2. Configure Private IP Addresses on both Linodes, and make sure you can access the master Linode’s private IP address from slave. You will use only private addresses for replication traffic for security reasons.

  3. Configure the master Redis instance to listen on a private IP address by updating the bind configuration option in redis.conf. Replace 192.0.2.100 with the master 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

  1. Configure a slave instance by adding the slaveof directive into redis.conf to setup the replication. Again replace 192.0.2.100 with the master 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.

  2. 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:

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


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.