A well-known means to improve the efficiency of modern computer applications is by enabling caching. Caching simply means keeping active data in a temporary store to make it easier and faster to access and Redis is one of the most popular caching solutions out there which supports most programming languages including Node.js, Python, PHP, Java, C, and much more.
Redis is an advanced and feature-rich key-value store that works in most POSIX systems such as Linux (which is the recommended platform for production deployment), *BSD, and OS X without external dependencies. It has three major uses: as a database, cache and message broker. It supports various data structures including strings, lists, sets, hashes, sorted sets with range queries, bitmaps and much more.
Read Also: How to Install Redis in RHEL 8
Some of its key features include built-in replication, cluster mode, partitioning (distributing data among multiple instances), transactions, notifications of key-space events, Lua scripting, mass insertion of data in a Redis instance in a short time, memory optimization, and so much more. Importantly, it supports an API which allows you to extend Redis functionality using external modules.
In this article, we will explain to you how to install, configure and test a Redis server on CentOS 7 Linux.
Installing Redis Server on CentOS 7
1. To install Redis package on CentOS 7, you need to install the EPEL repository on your system using the YUM package manager as shown.
# yum install epel-release
2. Once EPEL has been installed, you can now install Redis package from the repository as follows.
# yum install redis
3. Once you have installed the package, you need to set up your server to achieve high performance when using Redis. You need to perform some settings in the kernel as explained.
First, ensure that you have set up swap space in the server. It is recommended to set up as much as swap as memory.
4. Next, set the Linux kernel overcommit memory setting to
1 by adding
vm.overcommit_memory = 1 to /etc/sysctl.conf configuration file.
# sysctl vm.overcommit_memory=1
and then apply the change by rebooting the system Or enable the setting immediately by running the following command.
# sysctl vm.overcommit_memory=1
Also make sure that the transparent huge pages kernel feature is disabled because it negatively has an impact on both memory usage and latency using the following echo command.
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
Configuring Redis Server on CentOS 7
5. The default configuration for Redis is /etc/redis.conf. Before you can edit it, create a back up of it as follows. This enables you to revert to the back up of the default configurations in case of any mistakes.
# cp /etc/redis.conf /etc/redis.conf.orig
6. Then open the original Redis configuration file for editing using your any of your favorite text-based editors as shown.
# vi /etc/redis.conf
There are several configuration directives, and their meaning and intended usage is available and well explained in the file.
A typical configuration example is allowing remote access to the Redis server. By default, Redis is configured to accept connections only on the local server where it is running, i.e on the loopback interface (127.0.0.1) and it listens on port 6379.
7. To allow remote access, you can set it to listen to a specific interface or multiple selected interfaces using the
"bind" configuration directive, followed by one or more IP addresses as follows.
bind 127.0.0.1 bind 10.0.2.15 192.168.0.105
8. To accept connections on a different port, change the value of the port directive.
After making all the necessary changes, save the file and exit it.
9. At this point, you have set up your server to efficiently run Redis and configured the Redis server to work the way you want. Now you need to start the Redis service, for now, enable it to automatically start every time the system is rebooted, and check its status using the systemctl utility as shown.
# systemctl start redis # systemctl enable redis # systemctl status redis
10. To check the interface and port the Redis server is listening on, use the netstat command.
# netstat -tlpn
11. If you have the firewalld service running on your system, you need to open port 6379 in the firewall configuration to allow external connections to the Redis server.
# firewall-cmd --permanent --zone=public --add-port=6379/tcp # firewall-cmd --reload
Testing Connectivity to Redis Server
12. To test connectivity to the Redis server, you can open the Redis client program and run a test command (in this case list connected clients) as follows.
# redis-cli > client list #list clients connected to the server
Now you can build fast, dynamic and modern applications on your CentOS 7 server using Redis. Consult the Redis documentation for more information and configuration options. If you have any queries or thoughts to share, use the feedback form below to reach us.