| | |

Install latest version of Redis on Ubuntu 22.04.

Redis is a popular open-source in-memory data structure storage that is often used as a caching layer for Web applications. In the context of PHP MySQL applications, such as WordPress, Redis can be used as an object cache to speed up application performance.

Object caching is a technique that involves storing the results of expensive database queries, complex calculations, or API requests in memory so that they can be quickly retrieved without having to be recalculated or fetched from the database or API on subsequent requests. Redis is particularly well-suited to object caching because it is designed for fast read and write operations on in-memory data.

By caching frequently-accessed data in Redis, PHP MySQL applications can reduce the number of database queries and API requests that need to be made, which can significantly improve the performance of the application. This is especially important for applications with a high volume of traffic or complex queries that can take a long time to execute.

In this tutorial, we are going to install Redis on an Ubuntu 22.04 system and then tweak the configuration to optimize operation and performance. We will assume you are logged in as root, so switch to root if you are not already.

sudo -i

Before you begin installing Redis, it is wise to ensure that your system is up-to-date and that all packages have been upgraded to the latest version.

apt update && apt upgrade -y

To take advantage of the latest functionality, best performance and assurance that all available security updates have been implemented, we use the “apt” installation packages available in Redis’ own repository to perform the installation.

To allow our system to install APT packages from this external source, it is necessary to first add the Redis signing key.

wget -O- https://packages.redis.io/gpg | gpg --dearmor -o /etc/apt/trusted.gpg.d/redis.gpg

Although not strictly necessary, the presence of the directory ” /root/.gnupg” is required to prevent us from getting error messages during verification in the next step. The command “mkdir -p” creates the directory only if it does not already exist, to avoid overwriting existing content.

mkdir -p -m 600 /root/.gnupg

It is always advisable to verify that the downloaded file contains the correct key.

gpg --dry-run --quiet --import --import-options import-show /etc/apt/trusted.gpg.d/redis.gpg

Which should give an output similar to this:

pub   rsa4096 2021-07-21 [SC]
      54318FA4052D1E61A6B6F7BB5F4349D6BF53AA0C
uid                      Redis (Package Signing) <redis-packages@redis.io>
sub   rsa4096 2021-07-21 [E]

To set up the apt repository for “stable” Redis packages, run the following command:

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

Which should give you the following output:

deb https://packages.redis.io/deb jammy main

Update the repository info.

apt update

Install Redis.

apt install redis -y

Enable the Redis service to start at boot.

systemctl enable redis-server

Start Redis.

systemctl start redis-server

Check status.

systemctl status redis-server

Which should give you the following output:

● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-03-17 12:54:58 CET; 1h 57min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 79527 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 28700)
     Memory: 4.3M
        CPU: 5.334s
     CGroup: /system.slice/redis-server.service
             └─79527 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" ""

Mar 17 12:54:58 lemp-server systemd[1]: Starting Advanced key-value store...
Mar 17 12:54:58 lemp-server systemd[1]: Started Advanced key-value store.

Check the version.

redis-server -v

Which should give an output similar to this:

Redis server v=7.0.11 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=3af367a78d5e21e9

With the default configuration, you should now have a running Redis server reachable on localhost at port 6379.

We can test this with the “redis-cli” command.

redis-cli

Which should give you following prompt:

127.0.0.1:6379>

To check the response, type “ping” after the prompt.

127.0.0.1:6379> ping

Which should give you “PONG” as response.

PONG
127.0.0.1:6379>

To exit the Redis prompt simply type “quit” and enter.

127.0.0.1:6379> quit

While this configuration already allows us to connect applications such as WordPress to Redis to enable object caching, it is far from ideal and will require some fine-tuning. To optimize the default configuration, we will need to modify some system variables stored in the Redis configuration file.

The original configuration file “redis.conf” is very cluttered with lots of comments for every available system variable. To make this file more manageable we will first remove all unnecessary text.

Before we begin stripping, it is wise to make a backup of the original “redis.conf” configuration file so that we can always fall back on it or still read the comments for certain parameters, if desired.

cp /etc/redis/redis.conf /etc/redis/redis.conf_orig

Strip the original “redis.conf” file from all the comments.

grep -v -E "^#|^;" /etc/redis/redis.conf_orig | grep . > /etc/redis/redis.conf

We now have a clean configuration file in which all the system variables are neatly listed under each other and can be easily modified or added.

An essential part of configuring Redis is setting the correct amount of RAM to use and determining the most appropriate eviction policy. By default, Redis has no limit on the amount of RAM it can use from the host system (maxmemory 0), which means it can keep eating up memory until the operating system runs out of RAM and the system crashes. This, of course, must be avoided at all times, which is why it is necessary to limit Redis’ memory usage.

Redis allows specifying memory in all usual forms as in the examples below.

1000 => 1000 bytes
1k => 1000 bytes
1kb => 1024 bytes
1m => 1000000 bytes
1mb => 1024*1024 bytes
1g => 1000000000 bytes
1gb => 1024*1024*1024 bytes 

Note that units are case insensitive, so whether you use 1GB 1Gb, 1gB or 1gb, it’s all the same.

On most systems, an allocation of 64mb to 256mb is sufficient for good performance, provided a proper eviction policy is also in place where unused keys are removed and sufficient free space remains available.

maxmemory 128mb

On systems with applications where the database is extremely heavily loaded, such as e-commerce platforms with large volumes of products, significantly more RAM must be allocated to enable Redis to store database queries in memory and make them available from the cache.

The servers running these systems usually have plenty of RAM but it is still advisable to limit “maxmemory” to about 10% of the available system memory.

If you are not sure how much system memory your server has, use the following command that will give you a summary in kilobyte (KB), megabyte (MB) and gigabyte (GB).

free --kilo | grep Mem | echo "KB: $(awk '{print $2}')" && \
free --mega | grep Mem | echo "MB: $(awk '{print $2}')" && \
free --giga | grep Mem | echo "GB: $(awk '{print $2}')"

On a system with 16GB of RAM this would give the following output:

KB: 16503877
MB: 16503
GB: 16

So if you use the 10% rule you could allocate up to 1650 megabyte.

maxmemory 1650mb

You could also set a number in gigabytes, but since Redis does not allow decimals, you can therefore only specify single numbers as values, which for this example would mean setting less “1gb” or more “2gb” than 10%. In most cases, “1gb” will be more than sufficient.

maxmemory 1gb

Of course, over time, the allocated cache will reach its limit and must be partially or completely emptied before new data can be stored. Fortunately, however, Redis has a built-in mechanism that will automatically delete keys once the “maxmemory” limit is reached. The “maxmemory-policy” setting can be used to determine which type of keys will be deleted first. All available modes are described below.

  • volatile-lru – evict the least recently used key with an expiration.
  • allkeys-lru – evict the least recently used key, expiration or not.
  • volatile-lfu – evict the least frequently used key with an expiration.
  • allkeys-lfu – evict the least frequently used key, expiration or not.
  • volatile-random – evict a random key with an expiration.
  • allkeys-random – evict a random key, expiration or not.
  • volatile-ttl – evict the key with the closest expiration.
  • noeviction – do nothing, let the server burn.

Since the release of version 4.0, Redis has introduced a new eviction policy “allkeys-lfu” that evicts keys that have been used the least often, regardless of their expiration status. This is a welcome addition to “allkeys-lru” that removes keys that have been used the least recently, which we have recommended so far.

So we recommend using “allkeys-lfu”, which ensures that frequently used keys remain in memory and are always available to the application, even though they may have been one of the first keys saved, which with “allkeys-lru” would result in eviction.

maxmemory-policy allkeys-lfu

Redis has long been a single-threaded application, but as of version 6, this has changed and it is now possible to make Redis multi-threaded, which can provide a significant performance boost. By default, threading is disabled in Redis, which is the recommended configuration for systems with fewer than 4 cores.

To enable multi-threading, simply add the “io-threads” configuration directive followed by the number of cores you want to assign to the Redis configuration file “redis.conf”. Be sure to leave at least one free core and never more than the total number of cores available on your system. So for example, on a system with 4 cores, you can use a maximum of 3 cores for the I/O threads. Since using more than 8 threads does not provide a significant performance increase, it makes little sense to set the “io-threads” to a higher number, even on a system with, say, 12 cores.

If you are not sure how many cores are running on your system, you can quickly determine this by running the “nproc” command.

nproc

In this example, that gives us the number 4, which represents the number of cores of the CPU in our system.

4

This means we can use up to 3 cores and thus set the “io-threads” in the configuration file for Redis to 2 or 3.

io-threads 3

If you are running Redis on the same server as the application that will use object caching, it is strongly recommended that you access Redis via the Unix domain socket instead of TCP/IP loop-back.

Depending on the platform, Unix domain sockets can achieve more than 50% throughput than TCP/IP loop-back.

To make Redis listen on a Unix domain socket, we need to make some additional adjustments.

By default, Redis runs on Ubuntu with user and group “redis,” so we need to add other system users who want to access Redis to the group “redis.”

In this example, we are going to add the web server (Nginx, Apache) running on Ubuntu by default with user and group “www-data” to the group “redis”.

usermod -a -G redis www-data

Next, create a new folder in which the Unix socket will reside.

mkdir -p /var/run/redis/

Open the Redis configuration file.

vi /etc/redis/redis.conf

Insert the following code:

unixsocket /var/run/redis/redis.sock
unixsocketperm 660

Furthermore, look for the configuration directive “port” and change it from “6379” to “0”.

port 0

Now that we’ve made all the modifications, your “redis.conf” with all the custom configuration directives should look something like this:

io-threads 3
maxmemory 1gb
# bind 127.0.0.1 ::1
maxmemory-policy allkeys-lfu
unixsocket /var/run/redis/redis.sock
unixsocketperm 660
...
port 0

Restart Redis and php-fpm (in our case php8.1-fpm) to implement the new configuration.

systemctl restart redis php8.1-fpm

To test whether “www-data” does indeed have access to that Redis we run the following command:

sudo -u www-data redis-cli -s /var/run/redis/redis.sock

Which should give you following prompt:

redis /var/run/redis/redis.sock>

To check the response, type “ping” after the prompt.

redis /var/run/redis/redis.sock> ping

Which should give you “PONG” as response.

PONG
redis /var/run/redis/redis.sock>

To exit the Redis prompt simply type “quit” and enter.

Similar Posts