How to Install and Configure Redis on Ubuntu 22.04


Redis is an in-memory data store also referred to as the NoSQL database. The in-memory storage functionality of Redis makes it fast in read/write operations. Because of its speed, Redis is mainly used in caching, leaderboards/counters, background jobs/queues, and analytics. This is where Redis outperforms its NoSQL competitors.

Redis is an open source and thus liked by Linux/Ubuntu users. Today, we will guide you to install, use, and configure/secure Redis on Ubuntu 22.04.

How to Install Redis on Ubuntu 22.04

Redis is readily available on the Ubuntu default repositories, the Redis repository, and the snap store. Let’s demonstrate this one by one:

Default Repositories

Redis is available with the redis and redis-server names. There is only one difference, i.e., redis contains the redis-server package whereas the redis-server does not contain the redis package. You can install anyone.

Step 1: Update the Ubuntu Repsotires Packages List

sudo apt update

Step 2: Install Redis

To install Redis, it is recommended to get the “redis” package (which contains all the essentials):

sudo apt install redis

The default repositories method installs the 6.0.16 version:

Now, let’s check the status of the redis service:

sudo systemctl status redis

Our Redis server is running actively. If not in your case, start it using the command:

sudo systemctl start redis

Now let’s have a look at Redis usage on Ubuntu 22.04.

Redis Repository

Redis repository is available to embed with the apt and install the Redis packages/tools in that repository. Let’s learn how it installs Redis from its repository:

Step 1: Install the Prerequisites

The “lsb_release” tool is required to get the Ubuntu/system version you are using, the “curl” and “gpg” utilities help in downloading the GPG key:

sudo apt install lsb-release curl gpg

Step 2: Add the GPG Key

Now, use the below command to add the GPG key to authenticate the Redis repository (to be added in the next step):

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

Step 3: Add the Redis Repository

The following command traces your Ubuntu version (with the help of the lsb_release command) and adds the Redis repository to your sources list accordingly:

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

Step 4: Update Packages and Install Redis

Update the packages index and then use the apt install redis command to install Redis:

sudo apt update
sudo apt install redis

Check the redis-server version:

Redis repository has the latest version as compared to the version installed from the default repositories method.

Snap

Redis is also available on the Snap store but managed/maintained differently as compared to the default repositories method. Let’s see how you can install and manage a Redis through Snap:

Step 1: Install Redis

It is available with the name redis and can be installed using the command:

sudo snap install redis

Step 2: Enable/Manage Redis

Unlike the default repositories-based installation, Snap manages Redis on its own. The following snap commands refer to managing the Redis server:

sudo snap start redis #Start Redis
sudo snap restart redis #Restart Redis
sudo snap enable #Enable Redis

Access the Redis Server Installed Via Snap

In Snap-based installation, you can access the Redis CLI using the command:

redis.cli

The “redis-cli” command does not work on the Redis. However, you can create an alias to use “redis-cli” in a snap-based installation:

sudo snap alias redis.cli redis-cli

Let’s explore some basic usage of Redis.

Basic Usage of Redis on Ubuntu 22.04

While installing Redis, a package named “redis-tools” is installed automatically. The “redis-tools” support the “redis-cli” command which allows you to access the Redis server from the command line. Redis deals with the key value while storing and managing data. Let’s dig into this:

Dealing With Key-Value Expressions and Its Importance

The key-value phenomenon is fundamental to Redis in getting a high-speed performance. Keys majorly refer to the strings/integers (binary sequence), and values could be datatypes (strings/integers) and complex data structures (JSON/BSON). Let’s understand it via an example:

Example 1: Set/Get a Key-Value Pair

We will first access our Redis server using “redis-cli”. Then, a key-value pair is set where the “test” is the “Key” and the “Welcome to Linux Genie!” is the “Value” of the key:

redis-cli
set test "Welcome to LinuxGenie!"
get test

The “get” command can be used to check the value of the key:

 

To get all the keys, you can use the below command:

keys *

Right now, there is only one key.

Example 2: Set/Create a Key With an Expiry

If you store a key value, you will be able to use it even after closing the session and logging back again.

Moreover, you can create a key and set its expiry (in seconds). For instance, the following fruit key will expire after 10 seconds of its creation:

setex <key> <seconds> <value>

You can also set the expiry using the command:

set <key> <value> EX <seconds>

Example 3: Delete a Key

To delete any key from Redis server, use the “del” with the key-name:

del test

This is the basic usage of Redis. Let’s now configure and secure Redis on Ubuntu 22.04.

How to Configure/Secure Redis on Ubuntu

Configuration depends upon the requirement/usage of the Redis. Here we will perform the necessary configurations that are necessary after installing Redis. Let’s look at some recommended prerequisites before configuring:

  • First, stop the Redis service.
  • Open the configuration file of Redis placed at “/etc/redis/redis.conf”.
  • After each change, you need to start/restart the service to see the changes.

Let’s start configuring Redis:

Secure Redis | Set the Password

By default, there is no restriction to use redis. You just type “redis-cli” and start working. It is necessary to set the password and secure your write process/command execution on Redis. For this, open the “redis.conf” file in any editor and trace the line “requirepass foobared”. Uncomment it and choose a strong password in place of “foobared”:

Restart the redis service to apply the changes:

sudo systemctl restart redis

Let’s verify:

  • When you type any command inside the Redis command prompt, you will get an error “NOAUTH Authentication required”.
  • You need to use the “auth <your-password>” command to get access to Redis.
  • When the password is authenticated successfully, you will be able to use Redis.

Here’s the demonstration.

Now, the Redis is more secure than its default state.

Bind Redis to Specific Network Interface

Initially, the Redis server is bound with any specific interface, i.e., localhost in most cases. If not, your redis is exposed and listens to all the interfaces. It is recommended to keep the Redis with localhost or your network interface. Let’s check and configure this:

Open the configuration file in edit mode and look for the line “bind” keyword. Initially, you will see the local host address. Uncomment the line to keep it listening to localhost:

Note: Change the localhost address to your own if required.

Manage Redis Persistence

The Redis configuration file determines the time when the RDB file is saved to the disk (called snapshot). The snapshot settings are found in the redis.conf file with the names “save 900 1”, “save 300 10”, and “save 60 10000” (by default). If you need to change the RDB file persistence, you have to change these parameters. Let’s first understand these parameters:

  • save 900 1: Saves the file when 900 seconds have passed since the last change and at least one key is changed.

The crash might occur before taking the next snapshot and the data after the last snapshot will be lost. Thus, customize the save parameter to get the snapshot at the desired time. To do so, open the config file and change these values as per your requirement:

That’s how you optimize the RDB file.

Bonus: Modify the AOF Persistence

Redis has another persistence called Append Only File (AOF) Persistence that refers to the logs. If AOF is enabled, the commands/operations (captured by Redis at specific intervals received from clients) are logged to AOF. Redis reads this AOF to restore the dataset at the time of its startup.

Thus it is necessary to enable Append Only Persistence and set the time as per your requirement. To do so, open the config file and do the following:

Change the value of the “appendonly” parameter to “yes”.

Uncomment the “appendfsync” parameter and keep its value as required (we are setting always here). The possible values are:

    • no: Your system decides when to sync.
    • always: Sync on every write.
    • everysec: Sync at every second.

That’s how you can configure the Redis persistence.

Prevent Dangerous Commands | Modify Access Control List

Redis has some dangerous commands such as FLUSHALL (removes all keys), FLUSHDB (removes all keys from the current database), or FUNCTION FLUSH (deletes the libraries/functions). You can set/unset the permission to the Redis commands from the Access Control List.

For instance, the below command defines the user “genie” in the ACL and removes the access of the FLUSHALL command:

ACL SETUSER genie on >01478520 +@all -FLUSHALL

Now, check the configuration of a specific user:

ACL GETUSER genie

That’s how you can configure and secure Redis.

How to Remove Redis From Ubuntu 22.04

It is often seen that you might encounter a Redis crash. If you disable the redis service, you may not be able to use the Redis server. To get rid of this, you might have to go for removal/uninstallation. Let’s check the various methods to uninstall Redis (based on the installation methods):

Remove an apt-based Redis

If you have installed the Redis using Method 1 and Method 2, you can remove it using the following command:

sudo apt autoremove redis --purge

Remove a Snap-Based Redis

The snap-based Redis can be removed using the below command:

sudo snap remove redis --purge

Tip: Remove the Redis RDF File

You can also remove the RDB file snapshot from your system. The below command removes the dump.rdb file forcefully from your system:

sudo rm -rf /etc/redis/dump.rdb

That’s all from this Redis tutorial.

Bottom Line

Redis is a NoSQL pattern data structure that offers more caching speed than other NoSQL databases. Redis can be installed from the default repositories, snap store, and the Redis repository. The Redis repository offers the latest and stable version whereas snap offers the latest version but Snap itself manages (offers the least control over Redis) the Redis after installation.

You have learned the potential ways to install Redis on Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories