How to Install and Configure Redis on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install and configure Redis on Ubuntu 20.04, let's briefly understand – What is Redis?

Redis is a powerful and fast open-source in-memory data structure store. It is commonly used as a database, cache, and message broker. Redis excels in delivering lightning-fast performance by storing data in memory, enabling rapid access and retrieval.

With its versatile data structures and extensive features, Redis is ideal for applications requiring real-time analytics, caching, session management, and high-performance data processing.

Its flexibility and scalability make Redis a popular choice for modern web and mobile applications, helping businesses achieve optimal efficiency and responsiveness.

In this tutorial, we will describe how to install and configure Redis on Ubuntu 20.04. We will also address a few FAQs on how to install and configure Redis on Ubuntu 20.04.

Advantages of Redis

  1. Blazing Fast Performance: Redis is designed for speed, utilizing in-memory storage to deliver lightning-fast data access and retrieval.
  2. Versatile Data Structures: Redis supports a wide range of data structures, making it flexible for various use cases like caching, messaging, and more.
  3. High Scalability: Redis can handle large data sets and high traffic loads, making it suitable for scaling applications as user demand increases.
  4. Built-in Replication and High Availability: Redis offers built-in replication, ensuring data redundancy and high availability in distributed environments.
  5. Extensive Feature Set: Redis provides a rich set of features like pub/sub messaging, transactions, and Lua scripting, enabling developers to build complex applications efficiently.

Install Redis on Ubuntu 20.04

Redis installation on Ubuntu is a simple procedure.

The standard Ubuntu 20.04 repositories include Redis version 5.0.x. Run the following commands as root or as a user with sudo permissions to install it:

sudo apt update
sudo apt install redis-server

The Redis service will start automatically when the installation is finished. Enter the next command to verify the service's status:

sudo systemctl status redis-server

You should see something like this:

Output

● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-06-06 20:03:08 UTC; 10s ago
...
💡
If IPv6 is turned off on your server, Redis service won't launch.

Redis is installed and running on your Ubuntu 20.04 server.

Configure Redis Remote Access

The Redis server disallows external connections by default. Only the computer on which Redis is operating, 127.0.0.1 (localhost), is able to connect to Redis.

You shouldn't allow remote access if you are utilizing a single server arrangement and the client connecting to the database is also operating on the same host.

Open the Redis configuration file in your text editor to configure Redis to accept remote connections:

sudo nano /etc/redis/redis.conf

Find and comment on the line that starts with bind 127.0.0.1::1.

# bind 0.0.0.0 ::1
💡
Instead of commenting the line, add the private IP address after 127.0.0.1 if your server has a private IP address and you want Redis to only be accessible from the private network.

In order for modifications to take effect, save the file and restart the Redis service:

sudo systemctl restart redis-server

To make sure that Redis is accessible on all interfaces at port 6379, use the command shown below:

ss -an | grep 6379

It should look like the image below. 0.0.0.0 denotes all the machine's IPv4 addresses.

Output

tcp  LISTEN 0   511   0.0.0.0:6379   0.0.0.0:*
tcp  LISTEN 0   511      [::]:6379      [::]:*  

The next step is to set up your firewall to permit traffic on the TCP port 6379.

Normally, you would want to restrict access to the Redis server to a certain IP address or range of IP addresses. In order to only let connections from the 192.168.121.0/24 subnet, for instance, you would use the following command:

sudo ufw allow proto tcp from 192.168.121.0/24 to any port 6379
💡
Verify that your firewall is set up to only accept connections from recognized IP ranges.

At this point, distant locations ought should be able to connect to Redis on TCP port 6379.

Using the redis-cli tool, you may attempt to ping the Redis server from your remote workstation to make that everything is configured correctly:

redis-cli -h <REDIS_IP_ADDRESS> ping

The command ought to result in a PONG response:

Output

PONG

FAQs to Install and Configure Redis on Ubuntu 20.04

How can I start the Redis service after installation?

To start the Redis service, run the command sudo systemctl start redis-server.

How do I enable Redis to start on system boot?

You can enable Redis to start on system boot by running the command sudo systemctl enable redis-server.

How can I check if Redis is running on my Ubuntu 20.04 system?

Run the command sudo systemctl status redis-server to check the status of the Redis service.

How can I secure my Redis installation on Ubuntu 20.04?

To secure your Redis installation, you can configure password authentication in the Redis configuration file and enable a firewall to restrict access to Redis ports.

How do I connect to Redis from a client application?

You can connect to Redis using the Redis command-line interface (CLI) by running the command redis-cli. Alternatively, you can use Redis client libraries available in various programming languages.

Can I use Redis as a cache in my web application?

Yes, Redis is widely used as a caching solution due to its high performance and versatile data structures. You can integrate Redis into your web application to improve response times and reduce database load.

How can I monitor Redis performance on Ubuntu 20.04?

Redis provides a built-in monitoring system called Redis Monitoring (redis-cli MONITOR) that allows you to track key operations in real-time. Additionally, you can use external monitoring tools like RedisInsight or implement Redis-specific metrics in your preferred monitoring solution.

Conclusion

You have shown how to set up Redis on Ubuntu 20.04. Visit the Redis documentation page for further details on managing your Redis installation.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.