Redis is an open source, in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis is an acronym that stands for REmote DIctionary Server. It is similar to memcached but the dataset is not volatile and other datatypes (such as lists and sets) are natively supported. Redis comes with redis-cli which provides a simple command-line interface to the Redis server. This tutorial walks you through the steps to install and configure Redis server in Ubuntu. I tested this guide on Ubuntu 18.04 LTS server, however this guide might work on other Ubuntu/Debian-based distros.
In order for Redis to work efficiently, you must tweak your Ubuntu server with the following settings.
1. First, set the Linux kernel overcommit memory setting to 1. To do so, add
vm.overcommit_memory = 1 to
/etc/sysctl.conf. Then, reboot or run the command
sysctl vm.overcommit_memory=1 to activate the setting. Setting
1 tells Linux to relax and perform the fork in a more optimistic allocation fashion, and this is indeed what you want for Redis.
2. Next, ensure the Linux kernel feature Transparent Huge Pages does not impact Redis memory usage and latency. To do so, run this command:
$ echo never > sudo tee -a /sys/kernel/mm/transparent_hugepage/enabled
3. Make sure swap is enabled and that your swap file size is equal to amount of memory on your system. If your Linux system does not have swap set up, and your Redis instance accidentally consumes too much memory, Redis can crash when it is out of memory, or the Linux kernel OOM killer can kill the Redis process. When swapping is enabled, you can detect latency spikes and act on them.
Install Redis Server in Ubuntu
Redis Server is available in the default repositories of Ubuntu. So, you can install it using the apt-get package manager.
Run the following command to install Redis Server on your Ubuntu system:
$ sudo apt-get install redis-server
After installing, check if Redis Server service is start or not with command:
$ sudo systemctl status redis-server
Sample output would be:
● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-06-02 12:40:26 IST; 6s ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 2623 (redis-server) Tasks: 4 (limit: 2322) CGroup: /system.slice/redis-server.service └─2623 /usr/bin/redis-server 127.0.0.1:6379 Jun 02 12:40:26 ubuntuserver systemd: Starting Advanced key-value store... Jun 02 12:40:26 ubuntuserver systemd: redis-server.service: Can't open PID file /var/run/red Jun 02 12:40:26 ubuntuserver systemd: Started Advanced key-value store.
Just in case, if it is not started already, you can start it as shown below.
$ sudo systemctl enable redis-server
$ sudo systemctl start redis-server
To check the installed version of Redis Server, just run:
$ redis-server -v
Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=1bc80a08306a3efd
Configure Redis Server
The default configuration file of Redis is /etc/redis/redis.conf. By default Redis listens for connections from all the network interfaces available on the server. It is also possible make it to listen to just one or multiple interfaces using the "bind" configuration directive, followed by one or more IP addresses.
To make Redis server to listen to a particular IP address, edit /etc/redis/redis.conf file:
$ sudo vi /etc/redis/redis.conf
Find the following line:
Change it as shown below.
To make it listen to multiple IP addresses, then change it like below.
bind 192.168.43.2 192.168.43.3
Replace the IP addresses with your own values. Then, save and close the file.
If you're using UFW firewall, you must allow 6379 port. To do so, run:
$ sudo ufw allow 6379/tcp
$ sudo ufw reload
Restart Redis service to apply the changes.
$ sudo systemctl restart redis-server
Configuring Redis as a cache
If you want to use Redis as a cache where every key will have an expire set, you can use following example configuration (assuming a max memory limit of 2 megabytes as an example):
maxmemory 2mb maxmemory-policy allkeys-lru
In this configuration, there is no need for the application to set a time to live for keys using the
EXPIRE command (or equivalent) since all the keys will be evicted using an approximated LRU algorithm as long as we hit the 2 megabyte memory limit.
Restart the redis service to take effect the changes.
$ sudo systemctl restart redis-server
Test Redis Server
Connect to the Redis Server using redis-cli from command line.
Now, you're connected to your Redis server.
From here, you can run Redis commands to test its functionality. Let us see a simple example.
127.0.0.1:6379> set test "Welcome To OSTechNix" OK 127.0.0.1:6379> get test "Welcome To OSTechNix" 127.0.0.1:6379>
To quit from Redis command prompt, type exit.
To learn more about Redis usage, refer the its official documentation page.
In this guide, we discussed what is Redis server, how to install and configure Redis server in Ubuntu Linux. We also discussed how to configure Redis as a cache and finally we showed you the steps to test if Redis server is working as expected.