Knowledgebase

Install and Configure Redis on Debian 10 Print

  • 0

Introduction

Redis is an open-source in-memory data structure store. You can use it as a Memcached alternative to store simple key-value pairs, a NoSQL database, or even a message broker with the Pub-Sub pattern. This guide will show you how to install, configure, fine-tune, and secure Redis on Debian 10.

Prerequisites

1. Install Redis

On a Debian 10 system, you can choose to install one of two versions of Redis: the stable but old version from the default repositories, or the testing but newer version from the backports repositories.

Option 1: Install the stable version

  1. Update the package index files:

     $ sudo apt-get update
  2. Install the Redis default package:

     $ sudo apt-get install redis-server -y

Option 2: Install the testing version

  1. Add backports to the APT sources list:

     $ echo 'deb http://deb.debian.org/debian buster-backports main' | sudo tee /etc/apt/sources.list.d/backports.list
  2. Update the package index files:

     $ sudo apt-get update
  3. Install the Redis backported package:

     $ sudo apt-get -t buster-backports install redis-server -y

Regardless of your choice, the installation process always involves starting the Redis service and enabling it to run at boot time.

2. Configure Redis

  1. Open the Redis configuration file in your favorite editor:

     $ sudo nano /etc/redis/redis.conf
  2. Set the desired memory capacity for your application:

     maxmemory 128mb

    By default, when maxmemory is reached, Redis will stop writing new data. If you want Redis to write new data by removing old data automatically, you have to tell Redis how to remove it. The allkeys-lru eviction policy is the right choice for most users. Add the following line:

     maxmemory-policy allkeys-lru

    Learn more about eviction methods here.

  3. Set the save-to-disk policy.

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

     save 900 1
     save 300 10
     save 60 10000

    That means 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 restore your previous in-memory data to memory every time it restarts. 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 or add an appropriate Linux swap file to ensure that Redis's memory 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.

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

     $ sudo systemctl restart redis-server.service

3. Fine-Tune the System

  1. Examine the Redis log file:

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

    You will see some information like this:

     540:M 19 Sep 2020 08:51:19.490 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
     540:M 19 Sep 2020 08:51:19.490 # Server initialized
     540:M 19 Sep 2020 08:51:19.490 # 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.
     540:M 19 Sep 2020 08:51:19.490 # 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 madvise > /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 (set to 'madvise' or 'never').
  2. The first warning indicates a mismatch between a Redis setting and a system setting. To fix it, enter the following command:

     $ echo 'net.core.somaxconn = 512' | sudo tee -a /etc/sysctl.conf > /dev/null
  3. The second warning indicates the save-to-disk process may fail under low memory conditions. Enter the following command to fix it:

     $ echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null
  4. Reload the sysctl values:

     $ sudo sysctl -p
  5. Transparent Huge Pages (THP) is a Linux memory management system that, when enabled, will create latency and memory usage issues with Redis. So, you will create a systemd service to disable THP at boot time before starting the Redis service.

    Create the configuration file for the systemd service:

     $ sudo nano /etc/systemd/system/disable-transparent-hugepage.service

    Paste the following text into the file:

     [Unit]
     Description=Disable Transparent Huge Pages (THP) for Redis.
     Before=redis-server.service
    
     [Service]
     Type=exec
     ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'
    
     [Install]
     WantedBy=multi-user.target

    Save and close the file, then enable the service:

     $ sudo systemctl enable disable-transparent-hugepage.service

4. Verify the Setup

  1. Restart the Redis server:

     $ sudo reboot
  2. After the server restarts, check the Redis log file to ensure there are not any warnings:

     $ sudo tail /var/log/redis/redis-server.log
  3. Use the redis-cli program to connect to Redis through the default loopback IP 127.0.0.1 and port 6379:

     $ redis-cli -h 127.0.0.1 -p 6379

    If the connection succeeds, you will see the Redis command prompt:

     127.0.0.1:6379>
  4. Enter some Redis commands to ensure it works:

     set testkey testvalue
     get testkey
     exit

    If you see the following result, then Redis is working correctly:

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

5. (Optional) Configure Redis for Private Network Access

If you set up a production environment with multiple servers for your application, the application servers need access to the Redis server. It's recommended to use a private network for safety.

Configure the private network

  1. Follow this guide to enable and configure a private network for this Redis server and the application servers that need to communicate with Redis.

  2. Edit the Redis configuration file:

     $ sudo nano /etc/redis/redis.conf
  3. Add the private IP address that Redis should bind to. For example, if Redis should bind to both the internal loopback (127.0.0.1) and a private IP address (192.168.0.100):

     bind 127.0.0.1 192.168.0.100
  4. Save and close the configuration file, then restart Redis to apply the changes:

     $ sudo systemctl restart redis-server.service
  5. (Optional) Install and configure Ubuntu Firewall.

    Install the ufw package:

     $ sudo apt-get install ufw

    Allow SSH connections:

     $ sudo ufw allow ssh

    Allow Redis connections through the private IP 192.168.0.100 and port 6379:

     $ sudo ufw allow to 192.168.0.100 port 6379 proto tcp

    Enable the firewall:

     $ sudo ufw enable

    You will see the following output:

     Command may disrupt existing ssh connections. Proceed with operation (y|n)?

    Press Y, then press Enter to confirm.

Test the private network

  1. Connect to one of your application servers on your private network via SSH.

  2. Assuming the application server is also Debian 10, temporarily install the redis-tools package to get the redis-cli software:

     $ sudo apt-get install redis-tools
  3. Use the redis-cli program to connect to the Redis server through the private IP 192.168.0.100 and port 6379:

     $ redis-cli -h 192.168.0.100 -p 6379

    If the connection succeeds, you will see the Redis command prompt:

     192.168.0.100:6379>
  4. Enter some Redis commands to ensure it works:

     set testkey testvalue2
     get testkey
     exit
  5. If you see the following result, then Redis is working correctly.

     192.168.0.100:6379> set testkey testvalue2
     OK
     192.168.0.100:6379> get testkey
     "testvalue2"
     192.168.0.100:6379> exit
  6. Uninstall the redis-tools package on the application server.

     $ sudo apt-get remove redis-tools

Conclusion

To learn more about Redis, see these resources:

Introduction Redis is an open-source in-memory data structure store. You can use it as a Memcached alternative to store simple key-value pairs, a NoSQL database, or even a message broker with the Pub-Sub pattern. This guide will show you how to install, configure, fine-tune, and secure Redis on Debian 10. Prerequisites A Debian 10 server with enough free memory for Redis. One million small Key -> String-Value pairs use ~ 85 MB. Follow Rcs's best practices guides: Create a sudo user Update the Debian system 1. Install Redis On a Debian 10 system, you can choose to install one of two versions of Redis: the stable but old version from the default repositories, or the testing but newer version from the backports repositories. Option 1: Install the stable version Update the package index files: $ sudo apt-get update Install the Redis default package: $ sudo apt-get install redis-server -y Option 2: Install the testing version Add backports to the APT sources list: $ echo 'deb http://deb.debian.org/debian buster-backports main' | sudo tee /etc/apt/sources.list.d/backports.list Update the package index files: $ sudo apt-get update Install the Redis backported package: $ sudo apt-get -t buster-backports install redis-server -y Regardless of your choice, the installation process always involves starting the Redis service and enabling it to run at boot time. 2. Configure Redis Open the Redis configuration file in your favorite editor: $ sudo nano /etc/redis/redis.conf Set the desired memory capacity for your application: maxmemory 128mb By default, when maxmemory is reached, Redis will stop writing new data. If you want Redis to write new data by removing old data automatically, you have to tell Redis how to remove it. The allkeys-lru eviction policy is the right choice for most users. Add the following line: maxmemory-policy allkeys-lru Learn more about eviction methods here. Set the save-to-disk policy. By default, Redis will save its in-memory data on disk after a specified period or a specified number of write operations against the DB. The default settings are: save 900 1 save 300 10 save 60 10000 That means 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 restore your previous in-memory data to memory every time it restarts. 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 or add an appropriate Linux swap file to ensure that Redis's memory 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-Tune the System Examine the Redis log file: $ sudo tail /var/log/redis/redis-server.log You will see some information like this: 540:M 19 Sep 2020 08:51:19.490 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 540:M 19 Sep 2020 08:51:19.490 # Server initialized 540:M 19 Sep 2020 08:51:19.490 # 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. 540:M 19 Sep 2020 08:51:19.490 # 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 madvise > /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 (set to 'madvise' or 'never'). The first warning indicates a mismatch between a Redis setting and a system setting. To fix it, enter the following command: $ echo 'net.core.somaxconn = 512' | sudo tee -a /etc/sysctl.conf > /dev/null The second warning indicates the save-to-disk process may fail under low memory conditions. Enter the following command to fix it: $ echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf > /dev/null Reload the sysctl values: $ sudo sysctl -p Transparent Huge Pages (THP) is a Linux memory management system that, when enabled, will create latency and memory usage issues with Redis. So, you will create a systemd service to disable THP at boot time before starting the Redis service. Create the configuration file for the systemd service: $ sudo nano /etc/systemd/system/disable-transparent-hugepage.service Paste the following text into the file: [Unit] Description=Disable Transparent Huge Pages (THP) for Redis. Before=redis-server.service [Service] Type=exec ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null' [Install] WantedBy=multi-user.target Save and close the file, then enable the service: $ sudo systemctl enable disable-transparent-hugepage.service 4. Verify the Setup Restart the Redis server: $ sudo reboot After the server restarts, check the Redis log file to ensure there are not any warnings: $ sudo tail /var/log/redis/redis-server.log Use the redis-cli program to connect to Redis through the default loopback IP 127.0.0.1 and port 6379: $ redis-cli -h 127.0.0.1 -p 6379 If the connection succeeds, you will see the Redis command prompt: 127.0.0.1:6379> Enter some Redis commands to ensure it works: set testkey testvalue get testkey exit If you see the following result, then Redis is working correctly: 127.0.0.1:6379> set testkey testvalue OK 127.0.0.1:6379> get testkey "testvalue" 127.0.0.1:6379> exit 5. (Optional) Configure Redis for Private Network Access If you set up a production environment with multiple servers for your application, the application servers need access to the Redis server. It's recommended to use a private network for safety. Configure the private network Follow this guide to enable and configure a private network for this Redis server and the application servers that need to communicate with Redis. Edit the Redis configuration file: $ sudo nano /etc/redis/redis.conf Add the private IP address that Redis should bind to. For example, if Redis should bind to both the internal loopback (127.0.0.1) and a private IP address (192.168.0.100): bind 127.0.0.1 192.168.0.100 Save and close the configuration file, then restart Redis to apply the changes: $ sudo systemctl restart redis-server.service (Optional) Install and configure Ubuntu Firewall. Install the ufw package: $ sudo apt-get install ufw Allow SSH connections: $ sudo ufw allow ssh Allow Redis connections through the private IP 192.168.0.100 and port 6379: $ sudo ufw allow to 192.168.0.100 port 6379 proto tcp Enable the firewall: $ sudo ufw enable You will see the following output: Command may disrupt existing ssh connections. Proceed with operation (y|n)? Press Y, then press ENTER to confirm. Test the private network Connect to one of your application servers on your private network via SSH. Assuming the application server is also Debian 10, temporarily install the redis-tools package to get the redis-cli software: $ sudo apt-get install redis-tools Use the redis-cli program to connect to the Redis server through the private IP 192.168.0.100 and port 6379: $ redis-cli -h 192.168.0.100 -p 6379 If the connection succeeds, you will see the Redis command prompt: 192.168.0.100:6379> Enter some Redis commands to ensure it works: set testkey testvalue2 get testkey exit If you see the following result, then Redis is working correctly. 192.168.0.100:6379> set testkey testvalue2 OK 192.168.0.100:6379> get testkey "testvalue2" 192.168.0.100:6379> exit Uninstall the redis-tools package on the application server. $ sudo apt-get remove redis-tools Conclusion 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