Redis is an acronym for Remote dictionary server. It’s an open source, in-memory and persistent key-value database / store which stores data as key-value pairs and also doubles up as a message broker. Redis supports a wide array of data structures including sets, lists, hashes, strings, HyperLogLogs and so many more.
What is a key-value pair?
A key-value pair is a set or pair of two linked items. Consider the following:
car = Mercedes
In this case, car is the key and Mercedes is the value. In a Redis database, this information can be written using the syntax:
SET “key1” “value1”
Our example will translate to:
SET “car” “Mercedes”
Benefits of using Redis
- Unlike relational databases like MySQL, Redis is a NoSQL database that stores data as a key value pair. This makes it simple and flexible as there’s no need of creating any tables , columns and rows which are associated with relational databases. Feeding data to Redis is simple and straightforward.
- One of the apparent uses of Redis is its use as a caching system. While it does so, it also offers persistence to data being written on it.
- The in-memory architecture of Redis makes it super-fast in storage and retrieval of data.
- The redis cache system is quite robust and has the capacity of withstanding failures and interruptions.
- Redis ships with a Master-Slave replication feature. When changes are made to the master node, they are automatically replicated on the slave nodes to ensure high availability.
- Redis has the ability to store large key & value pairs of up to 512 MB.
- Given its small footprint, Redis can be installed in IoT devices such as Raspberry Pi and Arduino to support IoT apps.
- Redis is a cross-platform database and caching system which can be installed in Windows , mac and Linux.
Redis Use Cases
With the above-outlined benefits, Redis can be used in the following ways:
- Analysis of real time statistical data.
- Used for integration with AWS services such as CloudTrail, CloudWatch, and Amazon EC2 instances.
- It can be an excellent option for caching websites.
- Social media platforms such as Instagram use Redis for storing images
- Redis supports a wide range of data types such as hyperloglogs, hashes and geospatial data.
Now let’s see how you can install Redis on CentOS 8 / RHEL 8
Step 1: Update system repositories
Login to your CentOS 8 / RHEL 8 system and update system packages and repositories using the command:
$ sudo dnf update -y
Step 2: Install Redis with dnf
Redis version 5.0.x is now included in CentOS 8 AppStream repository and installing it is a walk in the park. Simply run the command:
$ sudo dnf install redis -y
Once installed, you can verify the version of Redis installed by running the command:
[[email protected] ~]$ rpm -q redis redis-5.0.3-1.module_el8.0.0+6+ab019c03.x86_64 [[email protected] ~]$
From the output , it is clear that we have installed Redis version 5.0.3. To retrieve more information about Redis such as the version, architecture, license and a brief description, run the command:
$ rpm -qi redis
To start and enable Redis service, run following systemctl commands:
$ sudo systemctl start redis $ sudo systemctl enable redis
Once started, you can verify its status by executing
$ sudo systemctl status redis
By default, Redis runs on port 6379. You can confirm this by running the netstat command:
$ sudo netstat -pnltu | grep redis
Step 3: Configure Redis for remote access
The default installation only allows connections from localhost or Redis server and blocks any external connections. We are going to configure Redis for remote connection from a client machine.
Access the configuration file as shown:
$ sudo vim /etc/redis.conf
Locate the bind parameter and replace 127.0.0.1 with 0.0.0.0
Save and close the configuration file. For the changes to come into effect, restart Redis.
$ sudo systemctl restart redis
To log in to Redis shell, run the command:
Try to ping redis server. You should get a ‘PONG’ response as shown.
[[email protected] ~]$ redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
Step 4: Securing Redis Server
Our Redis setup allows anyone to access the shell and databases without authentication which poses a grave security risk. To set a password, head back to the configuration file /etc/redis.conf
Locate and uncomment the requirepass parameter and specify a strong password as shown
Restart Redis and head back to the server.
$ sudo systemctl restart redis
If you attempt to run any command before authenticating, the error shown below will be displayed
[[email protected] ~]$ redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379>
To authenticate, type ‘auth’ followed by the password set. In our case this will be :
auth [email protected]
Thereafter, you can continue running your commands.
[[email protected] ~]$ redis-cli 127.0.0.1:6379> auth [email protected] OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379>
To come out from redis-cli, type exit
Step 5: Configuring the firewall for redis
Lastly, we need to configure the firewall to allow remote connections to the Redis server. To do this, we need to open the redis port which is 6379.
So, run the commands below.
$ sudo firewall-cmd --add-port=6379/tcp --permanent $ sudo firewall-cmd --reload
To access Redis remotely, use the syntax below.
$ redis-cli -h REDIS_IP_ADDRESS
Next authenticate and hit ‘ENTER’
The IP address of our Redis server is 192.168.1.5 The command from another client PC will be
$ redis-cli -h 192.168.1.5
Next, provide the password and hit ‘ENTER’
auth [email protected]
Above confirms that Redis Server has installed successfully, let’s move to our next section.
How to perform Redis Benchmark
Redis comes with a built-in tool known as redis-benchmark that gives insights on the system’s performance statistics such as data transfer rate, throughput and latency to mention a few.
Some of the command options you can use with Redis include
- -n: This defines the number of requests to be made. The default is 100000
- -c: Defines the number of parallel connections to be simulated. By default, this value is 50
- -p: This is the Redis port which by default is 6379
- -h: Used to define the host. By default, this value is set to localhost (127.0.0.1)
- -a: Used to prompt for a password if the server needs authentication
- -q: Stands for quiet mode. Displays the average requests made per second
- -t: Used to run a combination of tests
- -P: Used for pipelining for enhanced performance.
- -d: Specifies the data size in bytes for GET and SET values. By default, this is set to 3 bytes
To confirm the average no. of requests that your Redis server can handle run the command:
$ redis-benchmark -q
Pay attention to the last line. It indicates that Redis can handle 23046 requests per second.
Additionally, you can use the -t option to execute a subset of commands. In the example below, we are using it to display the average no. requests for SET and GET commands.
$ redis-benchmark -t set,get -q
[[email protected] ~]$ redis-benchmark -t set,get -q SET: 26102.84 requests per second GET: 25555.84 requests per second [[email protected] ~]$
From the output, we can see that we have 26102.84 requests per second for SET command and 25555.84 requests for GET command.
By default, the number of parallel connections is 50. To specify a different number of client connections, say 1000, use the -c parameter as shown:
[[email protected] ~]$ redis-benchmark -t set,get -q -c 1000 SET: 21997.36 requests per second GET: 22119.00 requests per second [[email protected] ~]$
As you may have observed, the number of requests per seconds has gone down because of the increase in the number of parallel connections from 50 to 1000.
Let’s step up and set the number of requests to 1000000
[[email protected] ~]$ redis-benchmark -t set,get -q -c 1000 -n 1000000 SET: 16660.28 requests per second GET: 21550.79 requests per second [[email protected] ~]$
Again, the values for SET and GET goes down due to the increase in the number of requests from the default 100,000 to 1,000,000.
And this wraps up our today’s topic. We have successfully installed Redis server on CentOS 8 and managed to perform a few bench marking tests on our server. You are most welcome to share your feedback and comments.