How to install and configure Redis 6 on Ubuntu 20.04

Redis is an in-memory data structure store, used as a distributed, in-memory key–value database, cache and message broker, with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, HyperLogLogs, bitmaps, streams, and spatial indices.

In this tutorial we are going to learn how to install Redis 6 on Ubuntu 20.04.

# Prerequisites

To follow along, ensure that you have:

  • An updated Ubuntu server
  • Access to the Internet
  • Root access to the server or user with sudo access

# Table of Content

  1. Update Ubuntu Server
  2. Installing Redis
  3. Configuring Redis
  4. Connecting and performing basic operations in Redis
  5. Performing Redis Benchmark

# 1. Update the Ubuntu Server

Before proceeding, ensure that the server is updated using this command (as a root user):

sudo apt update
sudo apt -y upgrade 

Let us also ensure vim is installed using this command since we will use it later:

sudo apt install -y vim

# 2. Installing redis

You can install recent stable versions of Redis from the official APT repository. Add the repository to the apt index, update it and install:

$ curl | sudo apt-key add -
$ echo "deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
$ sudo apt-get update
$ sudo apt-get install redis

Use this command to confirm the redis package installed:

$ apt-cache policy info redis
  Installed: 6.7.0.dfsg.2-5
  Candidate: 6.7.0.dfsg.2-5
  Version table:
 *** 6.7.0.dfsg.2-5 500
        500 focal/main amd64 Packages
        100 /var/lib/dpkg/status
  Installed: 6:6.2.6-3rl1~focal1
  Candidate: 6:6.2.6-3rl1~focal1
  Version table:
 *** 6:6.2.6-3rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages
        100 /var/lib/dpkg/status
     6:6.2.6-2rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages
     6:6.2.6-1rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages
     6:6.2.5-1rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages
     6:6.2.4-1rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages
     6:6.0.16-3rl1~focal1 500
        500 focal/main amd64 Packages
        500 focal/main all Packages

Now that the service has been installed, let’s start it with this command:

sudo systemctl start redis-server

Enable the service so it starts on boot:

<meta charset="utf-8">sudo systemctl enable redis-server

After the service starts, use this command to check the status of the service:

$ sudo systemctl status redis-server
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-11-19 13:46:56 EAT; 1min 3s ago
   Main PID: 250930 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 4631)
     Memory: 2.1M
     CGroup: /system.slice/redis-server.service
             └─250930 /usr/bin/redis-server

Nov 19 13:46:56 systemd[1]: Starting Advanced key-value store...
Nov 19 13:46:56 systemd[1]: Started Advanced key-value store.

The above indicates that the service has been started successfully.

# 3. Configuring Redis

The redis configuration file is located in this path /etc/redis/redis.conf. In this section, we are going to update the redis configuration file to allow remote access, to set an authentication password, to add a pid file and to Set Persistent Store for Recovery.

Edit redis config file using this:

vim /etc/redis/redis.conf

To allow remote access to the redis instance, bind redis to using this line:

bind * -::*

To set password in redis, use this:

requirepass j2GfJuLFR8

To add a pid file to redis:

pidfile /var/run/redis/

Set Persistent Store for Recovery by changing the appendonlyvalue to yes

appendonly yes
appendfilename "appendonly.aof"

Restart redis service to apply changes:

sudo systemctl restart redis-server

# 4. Connecting and performing basic operations in Redis

Connecting to redis locally:

# redis-cli

To authenticate:> auth j2GfJuLFR8

You should receive OK in the output. If you input a wrong password, Authentication should fail.

Check redis information.> INFO

This will output a long list of data. You can limit the output by passing Section as an argument. E.g.> INFO Server
# Server
os:Linux 5.11.0-1019-aws x86_64

# 5. Performing Redis Benchmarking

Run the benchmark with 15 parallel connections, for a total of 10k requests, against local redis to test its performance.

# redis-benchmark -h -p 6379 -n 10000 -c 15 -a j2GfJuLFR8
====== PING_INLINE ======
  10000 requests completed in 0.23 seconds
  15 parallel clients
  3 bytes payload
  keep alive: 1
  host configuration "save": 3600 1 300 100 60 10000
  host configuration "appendonly": no
  multi-thread: no

Latency by percentile distribution:
0.000% <= 0.031 milliseconds (cumulative count 1)
50.000% <= 0.095 milliseconds (cumulative count 6342)
75.000% <= 0.103 milliseconds (cumulative count 7740)


Cumulative distribution of latencies:
29.540% <= 0.103 milliseconds (cumulative count 2954)
99.810% <= 0.207 milliseconds (cumulative count 9981)
99.990% <= 0.303 milliseconds (cumulative count 9999)
100.000% <= 0.407 milliseconds (cumulative count 10000)

  throughput summary: 91743.12 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.121     0.032     0.127     0.167     0.183     0.319

For more options and examples, use:

$ redis-benchmark --help

# Conclusion

In this guide, we have managed to install and configure Redis 6 on an Ubuntu server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy