Knowledgebase

Install and Configure Redis on Ubuntu 20.04 Print

  • 0

Introduction

Redis is an open-source (BSD licensed), in-memory data structure store. You can use it as a Memcached alternative to store simple key-value pairs. Moreover, you can use it as a NoSQL database, or even a message broker with the Pub-Sub pattern.

This guide will show you how to install, configure, and fine-tune a recent Redis version on Ubuntu 20.04 or Ubuntu 18.04.

Prerequisites

1. Install Redis

The Redis version comes from the official Ubuntu repo is usually far behind the latest version available. This guide will use the chris-lea/redis-server PPA, a long-time, up-to-date, and community-trusted PPA, to install a recent release.

Add the PPA to the system's Software Sources (press Enter when prompted):

$ sudo add-apt-repository ppa:chris-lea/redis-server

Update package lists then install Redis.

$ sudo apt-get update
$ sudo apt-get install redis-server -y

Make Redis run at boot-time:

$ sudo systemctl enable redis-server.service 

2. Configure Redis

Open the Redis configuration file in your favorite editor.

$ sudo nano /etc/redis/redis.conf

Update the IP address that Redis will listen on:

  • If you installed Redis on your application server, then you are safe with the default setting.

      bind 127.0.0.1 ::1

    Don't change that line. Your application will use the loopback IP 127.0.0.1 to connect to Redis.

  • Otherwise, enable and configure a private network between your Redis server and your application server. Then add the private IP, for example, 192.0.2.1, to the end of the line above, separate by a space.

      bind 127.0.0.1 ::1 192.0.2.1

    Your application will use the private IP 192.0.2.1 to connect to Redis. Warning: Letting Redis listen on a public IP is dangerous and will expose the instance to everyone on the internet.

Set the desired memory capacity for your application, for example, 128mb (mean 128 MB):

maxmemory 128mb

By default, when maxmemory is reached, Redis will stop writing new data. If you want Redis to continue to write new data by removing old data automatically, you have to tell Redis how to remove it. Set the recommended value allkeys-lru for the maxmemory-policy directive.

maxmemory-policy allkeys-lru

By default, Redis will save its in-memory data on disk after a specified period and number of write operations against the DB. The default settings are:

save 900 1
save 300 10
save 60 10000

That means the saving will occur:

  • after 900 sec (15 min) if at least 1 key changed
  • after 300 sec (5 min) if at least 10 keys changed
  • after 60 sec if at least 10000 keys changed

With the default settings above, Redis will load the saved data into memory every time it restarts. So your previous in-memory data will be restored. If you don't need this feature, you can disable it entirely by commenting out those lines.

#save 900 1
#save 300 10
#save 60 10000

If you decide to keep this feature, you should upgrade the server to a bigger plan to ensure that the memory reserved for Redis is double the maxmemory declared above. Otherwise, in the worst-case scenario, when the maxmemory is reached, the saving process can cause your server to run out of memory.

Save and close the configuration file, then restart Redis to apply the changes:

$ sudo systemctl restart redis-server.service 

3. Fine-Tuning the System

Open the Redis log file:

$ sudo tail /var/log/redis/redis-server.log

You will see some information like this:

50570:M 24 Jul 2020 09:53:24.644 # Server initialized
50570:M 24 Jul 2020 09:53:24.644 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
50570:M 24 Jul 2020 09:53:24.644 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
50570:M 24 Jul 2020 09:53:24.645 * Loading RDB produced by version 6.0.5
50570:M 24 Jul 2020 09:53:24.645 * RDB age 0 seconds
50570:M 24 Jul 2020 09:53:24.645 * RDB memory usage when created 0.76 Mb
50570:M 24 Jul 2020 09:53:24.645 * DB loaded from disk: 0.000 seconds
50570:M 24 Jul 2020 09:53:24.645 * Ready to accept connections

To fix the first warning above, enter the following commands:

$ echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null
$ sudo sysctl -p

To fix the second warning above, create a new text file:

$ sudo nano /etc/rc.local

Paste the following text into that file:

#!/bin/bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
systemctl restart redis-server.service
exit 0

Save and close that file. Then make it runnable and owned by the root account.

$ sudo chown root:root /etc/rc.local
$ sudo chmod 770 /etc/rc.local

Optional:

  • If this Redis server is separate from your application server and you also installed UFW, you have to configure UFW to allow connections to the Redis port, default is 6379:

      $ sudo ufw allow 6379
  • If you install Redis on Ubuntu 18.04, Redis will trigger an additional warning about "The TCP backlog." To fix this warning, enter the following commands:

      $ echo 'net.core.somaxconn = 512' | sudo tee -a /etc/sysctl.conf > /dev/null
      $ sudo sysctl -p

4. Verify the Setup

Reboot the system:

$ sudo reboot

Open the Redis log file to ensure the two warnings above have gone:

$ sudo tail /var/log/redis/redis-server.log

Switch to your application server (assuming it's also a Ubuntu 20.04 or Ubuntu 18.04 server). Make sure it can talk to the Redis server by performing the following actions:

  • Install the redis-cli program provided by the redis-tools package from the official repo:

      $ sudo apt-get install redis-tools
  • Make redis-cli connect to the Redis server with the Redis IP address configured above, for example, 192.0.2.1:

      $ redis-cli -h 192.0.2.1

    If the connecting is successful, you will see the Redis command prompt like this:

      192.0.2.1:6379> _

    Enter some Redis commands to ensure it works:

      set testkey testvalue
      get testkey
      exit

    If you see the following result, then your Redis setup is perfect:

      192.0.2.1:6379> set testkey testvalue
      OK
      192.0.2.1:6379> get testkey
      "testvalue"
      192.0.2.1:6379> exit

Conclusion

Installing a recent Redis version on Ubuntu 20.04 or Ubuntu 18.04 is an easy task. But, properly configuring it is a bit tricky. To learn more about Redis, see these resources:

Introduction Redis is an open-source (BSD licensed), in-memory data structure store. You can use it as a Memcached alternative to store simple key-value pairs. Moreover, you can use it as a NoSQL database, or even a message broker with the Pub-Sub pattern. This guide will show you how to install, configure, and fine-tune a recent Redis version on Ubuntu 20.04 or Ubuntu 18.04. Prerequisites Deploy a new Ubuntu 20.04 server or use your existing one. It has to have enough free memory for Redis (1 Million small Keys -> String Value pairs use ~ 85 MB). If you're deploying a new Ubuntu server for Redis, you should enable and configure a private network between your Redis server and your application server. Follow Rcs's best practices guides: Create a sudo user. Update the Ubuntu server. (Optional) Install and configure the Ubuntu firewall (UFW) 1. Install Redis The Redis version comes from the official Ubuntu repo is usually far behind the latest version available. This guide will use the chris-lea/redis-server PPA, a long-time, up-to-date, and community-trusted PPA, to install a recent release. Add the PPA to the system's Software Sources (press ENTER when prompted): $ sudo add-apt-repository ppa:chris-lea/redis-server Update package lists then install Redis. $ sudo apt-get update $ sudo apt-get install redis-server -y Make Redis run at boot-time: $ sudo systemctl enable redis-server.service 2. Configure Redis Open the Redis configuration file in your favorite editor. $ sudo nano /etc/redis/redis.conf Update the IP address that Redis will listen on: If you installed Redis on your application server, then you are safe with the default setting. bind 127.0.0.1 ::1 Don't change that line. Your application will use the loopback IP 127.0.0.1 to connect to Redis. Otherwise, enable and configure a private network between your Redis server and your application server. Then add the private IP, for example, 192.0.2.1, to the end of the line above, separate by a space. bind 127.0.0.1 ::1 192.0.2.1 Your application will use the private IP 192.0.2.1 to connect to Redis. Warning: Letting Redis listen on a public IP is dangerous and will expose the instance to everyone on the internet. Set the desired memory capacity for your application, for example, 128mb (mean 128 MB): maxmemory 128mb By default, when maxmemory is reached, Redis will stop writing new data. If you want Redis to continue to write new data by removing old data automatically, you have to tell Redis how to remove it. Set the recommended value allkeys-lru for the maxmemory-policy directive. maxmemory-policy allkeys-lru By default, Redis will save its in-memory data on disk after a specified period and number of write operations against the DB. The default settings are: save 900 1 save 300 10 save 60 10000 That means the saving will occur: after 900 sec (15 min) if at least 1 key changed after 300 sec (5 min) if at least 10 keys changed after 60 sec if at least 10000 keys changed With the default settings above, Redis will load the saved data into memory every time it restarts. So your previous in-memory data will be restored. If you don't need this feature, you can disable it entirely by commenting out those lines. #save 900 1 #save 300 10 #save 60 10000 If you decide to keep this feature, you should upgrade the server to a bigger plan to ensure that the memory reserved for Redis is double the maxmemory declared above. Otherwise, in the worst-case scenario, when the maxmemory is reached, the saving process can cause your server to run out of memory. Save and close the configuration file, then restart Redis to apply the changes: $ sudo systemctl restart redis-server.service 3. Fine-Tuning the System Open the Redis log file: $ sudo tail /var/log/redis/redis-server.log You will see some information like this: 50570:M 24 Jul 2020 09:53:24.644 # Server initialized 50570:M 24 Jul 2020 09:53:24.644 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 50570:M 24 Jul 2020 09:53:24.644 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 50570:M 24 Jul 2020 09:53:24.645 * Loading RDB produced by version 6.0.5 50570:M 24 Jul 2020 09:53:24.645 * RDB age 0 seconds 50570:M 24 Jul 2020 09:53:24.645 * RDB memory usage when created 0.76 Mb 50570:M 24 Jul 2020 09:53:24.645 * DB loaded from disk: 0.000 seconds 50570:M 24 Jul 2020 09:53:24.645 * Ready to accept connections To fix the first warning above, enter the following commands: $ echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null $ sudo sysctl -p To fix the second warning above, create a new text file: $ sudo nano /etc/rc.local Paste the following text into that file: #!/bin/bash echo never > /sys/kernel/mm/transparent_hugepage/enabled systemctl restart redis-server.service exit 0 Save and close that file. Then make it runnable and owned by the root account. $ sudo chown root:root /etc/rc.local $ sudo chmod 770 /etc/rc.local Optional: If this Redis server is separate from your application server and you also installed UFW, you have to configure UFW to allow connections to the Redis port, default is 6379: $ sudo ufw allow 6379 If you install Redis on Ubuntu 18.04, Redis will trigger an additional warning about "The TCP backlog." To fix this warning, enter the following commands: $ echo 'net.core.somaxconn = 512' | sudo tee -a /etc/sysctl.conf > /dev/null $ sudo sysctl -p 4. Verify the Setup Reboot the system: $ sudo reboot Open the Redis log file to ensure the two warnings above have gone: $ sudo tail /var/log/redis/redis-server.log Switch to your application server (assuming it's also a Ubuntu 20.04 or Ubuntu 18.04 server). Make sure it can talk to the Redis server by performing the following actions: Install the redis-cli program provided by the redis-tools package from the official repo: $ sudo apt-get install redis-tools Make redis-cli connect to the Redis server with the Redis IP address configured above, for example, 192.0.2.1: $ redis-cli -h 192.0.2.1 If the connecting is successful, you will see the Redis command prompt like this: 192.0.2.1:6379> _ Enter some Redis commands to ensure it works: set testkey testvalue get testkey exit If you see the following result, then your Redis setup is perfect: 192.0.2.1:6379> set testkey testvalue OK 192.0.2.1:6379> get testkey "testvalue" 192.0.2.1:6379> exit Conclusion Installing a recent Redis version on Ubuntu 20.04 or Ubuntu 18.04 is an easy task. But, properly configuring it is a bit tricky. To learn more about Redis, see these resources: Redis's FAQ Using Redis as an LRU cache Redis Persistence

Was this answer helpful?
Back

Powered by WHMCompleteSolution