Redis is an open-source (BSD licensed), in-memory key-value data structure store used as a database, cache, and message broker. Redis supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyper log logs, geospatial indexes, and streams. Redis also provides high availability with Redis Sentinel software logic, automatically partitioning across commodities by ensuring data is replicated in multiple locations. For example, when adding a new node to the cluster, the system will automatically detect the new node and begin replicating data to it. If a node fails or becomes unresponsive, the system automatically removes it from the cluster and routes traffic to the next available node. This way, you can be sure that your data is always accessible and live updates are always possible. Redis has become one of the most popular database options available today, thanks to features like these.
The following tutorial will teach you how to install Redis on Debian 11 Bullseye using the terminal command line and the official Redis APT repository for users to install the latest version or for existing users to upgrade their Redis version that Debian, which is often outdated along with some basic setup instructions to get you started.
Table of Contents
Before installing Redis, start by ensuring your systems’ packages are up-to-date to avoid any conflicts during the installation using the following command.
sudo apt update && sudo apt upgrade
Install Required Packages
To complete the installation, you will need to install the following software packages by using the following command in your terminal.
sudo apt install software-properties-common apt-transport-https curl ca-certificates gnupg2 -y
Default Debian Repository Method
By default, Redis 5 series comes included in the Debian 11 Bullseye repository, which can be installed with the following command.
sudo apt install redis
Debian considers this version stable, but it is far behind. In fact, at the time of this tutorial, Redis version 7 was released with many improvements. If you prefer more recent, the following section can also be used for existing Redis installs to upgrade.
Redis.io APT Repository
The second method is to import the APT repository from the official repository. This is always up-to-date for bug and security fixes and the newest features which can be handy for users wanting to stick with Debian 11 but require or like to use a more updated version of Redis.
First, import the GPG key.
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
Now import the repository.
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
Update your sources list to reflect the new repository source.
sudo apt-get update
Now install Redis; you may see an upgrade if you have Redis installed. I would still advise using the install command.
sudo apt install redis
Check using the apt-cache policy command to ensure you have installed the Redis.io version.
apt-cache policy redis
As above, you installed the latest Redis version.
Now activate and enable on system boot the Redis instance.
sudo systemctl enable redis-server --now
Next, verify the status and make sure Redis is running and, more importantly, with no errors:
systemctl status redis-server
Note that Reddis actively listens to localhost on the default port 6379. To confirm this type, the following:
ps -ef | grep redis
While testing, everything is working and operational, connecting to your Redis service is good, and performing a ping test.
To perform the test, enter the following command:
Once connected, your terminal will display (127.0.0.1:6379). Now ping the Redis service as follows:
Type the following to exit the Redis instance:
Congratulations, you have installed Redis on your system and verified it is operational.
Next, you can configure Redis.
How to Configure Redis
Redis can be configured in a few ways. The most notable action of why people use Redis is for caching purposes. To do this, you need to open the “/etc/redis/redis.conf” file using nano editor.
sudo nano /etc/redis.conf
Configure Max Memory
Now, add the following to the end of the file. Note that you can change the memory value to whatever you like or, more importantly, optimal for your web application and server hardware.
maxmemory 500mb maxmemory-policy allkeys-lru
As you can see, the setting in the guide has 500MB dedicated to Redis as it is on a dedicated host with lots of RAM to spare. Once the 500MB is exhausted, Redis removes any key per the LRU algorithm.
Configure Network Access
Another option is to listen to all services or set an IP address/subnet if you like your Redis service.
First, find line 69 in the configuration file.
First, to listen to all network interfaces, Comment “#” the line bind to IP.
# bind 127.0.0.1 ::1
Ensure your internal network is trustworthy and has appropriate security controls.
To bind to an IP address, make sure it is a static IP address.
To bind a network subnet.
Note that it is highly suggested to set a password when using subnet or to access all interfaces to listen to.
Another security feature to further harden Redis is to set a password on the Redis instance.
Navigate to line 507, uncomment the “# requiredpass” line, and set a password.
Make sure this password is robust, numbers, letters, special symbols, and capitals randomized as Redis servers can be brute forced on a decent box very well.
Next, when invoking the Redis-CLI, use the following command with the password that was set for the user.
“THEPASSWORDSET” is the password that was created.
Users who fail to log in will see the following error message.
(error) NOAUTH Authentication required.
When a user successfully logs in, they will see the following message.
Once done, save your changes CTRL+O then exit CTRL+X. Now restart the Redis service by typing:
sudo systemctl restart redis-server
Configure UFW Rules
If you use the UFW firewall installed by default on your Debian systems, you will need to create allow rules on the TCP port 6379.
First, ensure UFW is installed.
sudo apt install ufw -y
Next, enable UFW.
sudo ufw enable
Some examples are below, depending on your installation and requirements if using singular or in a cluster network.
Additional network IP server instance:
sudo ufw allow proto tcp from <ip address> to any port 6379
Cluster network with many instances:
sudo ufw allow proto tcp from <ip address>/24 to any port 6379
Note that the second UFW rule is a subnet rule; ensure the internal network is secure and trustworthy before allowing it.
Now, as you tested at the start of the guide by pinging your Redis service to make sure it was operational, you can try the firewall rules and changes in IP by using the “redis-cli” command:
redis-cli -h <ip address> ping
If setup correctly, the output should be:
Comments and Conclusion
Redis is a powerful open-source data structure store that can be used as a database, cache, and message broker. By using Redis in your applications, you can improve performance and scalability. For more information about managing your Redis installation, visit the Redis documentation page.