Caching

How to install and configure Redis 6 on Ubuntu 22.04 

Pinterest LinkedIn Tumblr

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.

Related Content

Prerequisites

To follow along, ensure that you have:

  • An updated Ubuntu 22.04 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 packages.redis.io APT repository. Add the repository to the apt index, update it and install:

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

Use this command to confirm the redis package installed:

$ apt-cache policy redis-server
redis-server:
  Installed: 6:7.0.0-1rl1~jammy1
  Candidate: 6:7.0.0-1rl1~jammy1
  Version table:
 *** 6:7.0.0-1rl1~jammy1 500
        500 https://packages.redis.io/deb jammy/main amd64 Packages
        100 /var/lib/dpkg/status
     6:6.2.7-1rl1~jammy1 500
        500 https://packages.redis.io/deb jammy/main amd64 Packages
     5:6.0.16-1ubuntu1 500
        500 http://europe-west6.gce.archive.ubuntu.com/ubuntu jammy/universe amd64 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:

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 2022-05-27 13:42:54 UTC; 13s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1483 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 4394)
     Memory: 5.8M
        CPU: 103ms
     CGroup: /system.slice/redis-server.service
             └─1483 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" ""

May 27 13:42:54 ubuntusrv.citizix.com systemd[1]: Starting Advanced key-value store...
May 27 13:42:54 ubuntusrv.citizix.com 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 0.0.0.0 using this line:

bind * -::*

To set password in redis, use this:

requirepass j2GfJuLFR8

To add a pid file to redis:

pidfile /var/run/redis/redis-server.pid

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:

127.0.0.1:6379> auth j2GfJuLFR8
OK
127.0.0.1:6379>

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

Check redis information.

127.0.0.1:6379> INFO

This will output a long list of data. You can limit the output by passing Section as an argument. E.g.

127.0.0.1:6379> INFO
# Server
redis_version:7.0.0
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:698b5df4772f6164
redis_mode:standalone
os:Linux 5.15.0-1005-gcp x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:11.2.0
process_id:1652
process_supervised:systemd
run_id:8b6c708c9b58b6977f64187b8fb65f102c2cf586
tcp_port:6379
server_time_usec:1653659326493731
uptime_in_seconds:30
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9492158
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf
io_threads_active:0

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 127.0.0.1 -p 6379 -n 10000 -c 15 -a Vi9PQD2FCDtkLF
====== PING_INLINE ======
  10000 requests completed in 0.34 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.063 milliseconds (cumulative count 1)
50.000% <= 0.247 milliseconds (cumulative count 5027)
75.000% <= 0.359 milliseconds (cumulative count 7523)
87.500% <= 0.455 milliseconds (cumulative count 8751)
93.750% <= 0.527 milliseconds (cumulative count 9431)
96.875% <= 0.567 milliseconds (cumulative count 9728)
98.438% <= 0.599 milliseconds (cumulative count 9845)
99.219% <= 0.647 milliseconds (cumulative count 9927)

....

Summary:
  throughput summary: 38759.69 requests per second
  latency summary (msec):
          avg       min       p50       p95       p99       max
        0.237     0.072     0.231     0.351     0.559     1.743

[email protected]:~#

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

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment