Redis (Remote Dictionary Server) is an open-source, in-memory database that is used as a cache and message broker. It is also known as a data structure server. What sets it apart from other main databases is its ability to store high-level data types (including maps, lists, sets and more), an easy-to-use interface, atomic manipulation of data and exceptional performance that one cannot find with other existing databases. That’s exactly why in this tutorial, we’ll show you how to install Redis on Ubuntu 18.04 to improve your VPS server!
What makes Redis useful?
As mentioned above, it is the performance and the exceptional features of Redis that make it better than traditional databases. The typical uses of Redis are:
- Caching – its enhanced capability to persist the data to a disk makes it a superior alternative to traditional caching solutions
- Queuing – Redis can be used for queuing jobs in the background.
- Countering – Redis allows the simple creation and implementation of counters without any need of reading data or updating schemes to the database. The counters in Redis will maintain consistent
- Publishing and subscribing – users can easily distribute data using the Publish/Subscribe paradigm
How to Install Redis on Ubuntu?
Redis is present in the official package repository of Ubuntu. Remember, first you need to access your VPS using SSH. If you’re having trouble, check out our PuTTY tutorial. Let’s install Redis on Ubuntu:
1. Update APT Repository Cache
In order to install Redis, you first need to update the APT repository cache of your Ubuntu. You can do that with the following command:
sudo apt update
2. Install Redis on Ubuntu Using the APT Command
Now, you can enter the command to install Redis. Enter the following string into the command line:
sudo apt install redis
Press y and then hit enter to continue.
3. Check Redis Version
In order to check if Redis is installed properly and working correctly, you can enter the command:
The output will display the version of the utility currently installed on your machine.
Starting and Stopping Redis
Once you complete the installation, you can check if the Redis is running. You can do this with the following command:
sudo systemctl status redis
In the output, locate Active: active (running).
If Redis hasn’t been started, you can launch it by entering the command:
sudo systemctl start redis-server
If Redis is already running and you want to stop it, you can use the command:
sudo systemctl stop redis
After this, you will see Active: inactive (dead) in the output of the first command in this section.
How to Configure a Redis Server on Ubuntu
The default configuration of Redis is placed in /etc/redis/redis.conf. By default, the server listens for connections from all interfaces available on the server. You can make it listen to the interfaces of your choice, which can be one, or multiple interfaces depending on your needs. This can be done by using the bind configuration directive that would be followed by a single, or multiple IP addresses.
To instruct the Redis server to listen for a particular IP address, you need to edit the /etc/redis/redis.conf file. Open it with your prefered editor. We’ll use vi. We open the file with the following command:
sudo vi /etc/redis/redis.conf
Locate the line bind 127.0.0.1
Now, change the IP address by entering the values of the interfaces you want the redis server to listen for. For example:
And if you want to add multiple IP addresses, simply separate them with a space like so:
bind 192.168.43.2 192.168.43.3
Here you need to enter the IP addresses of your own network.
However, if you want the server to listen to all the interfaces on the network, you can use the command:
Once you are done with making changes, save and close the file. In vi you can do this by hitting : and executing wq. Then restart the Redis server to apply the changes. The command for restarting is:
sudo systemctl restart redis-server
Basic Redis Command Examples
There are different groups of commands in Redis which include:
- String commands
- List commands
- Set commands
- Hash commands
- Sorted set commands
- Publish/Subscribe commands
- And others
Here we have mentioned a mix of some commands used in Redis:
|Redis-server /path/redis.conf||Starts Redis with the particular configuration file|
|Redis-cli||Opens the Redis prompt|
|APPEND key value||Append a value to a key|
|BITCOUNT key [start end]||Sets bit in a string|
|SET key value||Set a value in the key|
|EXPIRE key 120||Expire the key in 120 seconds|
|INCR key||Increment the value in the key|
|KEYS pattern||Finds all the keys matching a particular pattern|
How to install Redis on Ubuntu? It’s easy! With the help of this tutorial you successfully added the utility to your arsenal and can start using it! We hope you put this powerful tool to good use!