Friday, November 15, 2024
Google search engine
HomeData Modelling & AIInstall and Configure Redis Server on RHEL 8 / CentOS 8

Install and Configure Redis Server on RHEL 8 / CentOS 8

How can I install Redis on CentOS 8 / RHEL 8?. This guide will help you to install and configure Redis server on RHEL 8 / CentOS 8 Linux system. Redis is a free and open Source in-memory data structure store. It can be used as a database server, as a message broker or for caching data in memory for faster retrieval.

The data structures supported by Redis are:

  • Hashes
  • sets with range queries
  • Strings
  • sorted lists
  • Hyperloglogs
  • Bitmaps
  • Geospatial indexes e.t.c.

Install Redis on RHEL 8 / CentOS 8

Redis on RHEL 8 is available on AppStream repository.

$ sudo yum module list redis
AlmaLinux 8 - AppStream
Name                                     Stream                                    Profiles                                      Summary
redis                                    5 [d]                                     common [d]                                    Redis persistent key-value database
redis                                    6                                         common [d]                                    Redis persistent key-value database

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

As seen from the output, Redis 6.0 is available on AppStream. Install it using yum package manager.

sudo dnf module reset redis -y
sudo yum install -y @redis:6 vim

Once the package is installed, start and enable Redis service to start on boot.

sudo systemctl enable --now redis

Service status should show running.

$ systemctl status redis
redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since Thu 2023-08-24 13:20:42 UTC; 4s ago
 Main PID: 50649 (redis-server)
    Tasks: 4 (limit: 49359)
   Memory: 6.6M
   CGroup: /system.slice/redis.service
           └─50649 /usr/bin/redis-server 127.0.0.1:6379

Aug 24 13:20:42 almalinux-8.localdomain systemd[1]: Starting Redis persistent key-value database...
Aug 24 13:20:42 almalinux-8.localdomain systemd[1]: Started Redis persistent key-value database.

Configure Redis Server on RHEL 8 / CentOS 8

Now that Redis server is installed, the next part is configuration. There are many tunables you can set to fit your use case, but I’ll cover the basic settings you need to get started.

Enable Redis Service to listen on all interfaces

By default, Redis service listens on 127.0.0.1. Allow the service to listen on all network interfaces if you need remote clients to connect to it.

$ ss -tunelp | grep 6379
tcp   LISTEN  0    128   127.0.0.1:6379    0.0.0.0:*  users:(("redis-server",pid=30348,fd=6)) uid:986 ino:71091 sk:4 <->  
tcp   LISTEN 0      511            [::1]:6379          [::]:*    users:(("redis-server",pid=12730,fd=7)) uid:993 ino:41067 sk:6 v6only:1 <->  

Open the file /etc/redis.conf with your favorite text editor

sudo vim /etc/redis.conf

Then change line 69 bind 127.0.0.1 to below:

bind 0.0.0.0

Restart Redis after making the change

sudo systemctl  restart redis

Confirm new bind address.

$ ss -tunelp | grep 6379
 tcp   LISTEN  0    128   0.0.0.0:6379    0.0.0.0:*  users:(("redis-server",pid=30348,fd=6)) uid:986 ino:71091 sk:4 <-> 

Configure Redis Authentication

Configure Redis Authentication for clients to require AUTH <PASSWORD> before processing any other commands.

requirepass  <AuthPassword>

Example:

requirepass StrongPassword

Set Persistent Store for Recovery

Set persistence mode by changing the appendonlyvalue to yes

appendonly yes
appendfilename "appendonly.aof"

Restart redis service after making the changes

sudo systemctl restart redis

If you have an active firewalld service, allow port 6379

sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload

Check redis service status:

systemctl status redis

Connect to Redis

Confirm that you can connect to Redis Server locally:

$ redis-cli
127.0.0.1:6379> INFO
 NOAUTH Authentication required.

Test authenticate:

127.0.0.1:6379> AUTH <AuthPassword>
OK

You should receive OK in the output. If you input a wrong password, Authentication should fail:

127.0.0.1:6379> AUTH WrongPassword
(error) ERR invalid password

Check redis information.

127.0.0.1:6379> INFO

This will output a long list of data. You can limit the output by passing Section as an argument. E.g.

127.0.0.1:6379> INFO Server
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:14b81d825631ff0c
redis_mode:standalone
os:Linux 4.18.0-348.2.1.el8_5.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.4.1
process_id:50649
run_id:68cf1e816bddaf49f8bb0ea9abba1a9091a9ad0f
tcp_port:6379
uptime_in_seconds:4993
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:8911378
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

Perform Redis Benchmarking

Run the benchmark with 10 parallel connections, for a total of 100k requests, against local redis to test its performance.

# redis-benchmark -h 127.0.0.1 -p 6379 -n 100000 -c 10
..................................................
100.00% <= 0 milliseconds
 85470.09 requests per second
 ====== LRANGE_500 (first 450 elements) ======
   100000 requests completed in 1.17 seconds
   10 parallel clients
   3 bytes payload
   keep alive: 1
 100.00% <= 0 milliseconds
 85397.09 requests per second
 ====== LRANGE_600 (first 600 elements) ======
   100000 requests completed in 1.18 seconds
   10 parallel clients
   3 bytes payload
   keep alive: 1
 100.00% <= 0 milliseconds
 84530.86 requests per second
 ====== MSET (10 keys) ======
   100000 requests completed in 1.18 seconds
   10 parallel clients
   3 bytes payload
   keep alive: 1
 100.00% <= 0 milliseconds
 84961.77 requests per second

For more options and examples, use:

redis-benchmark --help

To show connected clients, use:

127.0.0.1:6379> client list 
 id=185 addr=127.0.0.1:54300 fd=8 name= age=75 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

Using Redis with Python

To use redis with Python, install Python Redis Client Library:

sudo yum -y install  python-redis

Using Redis with PHP

To connect to Redis server with PHP, install PHP Redis Client Module.

sudo yum -y install php-pecl-redis

Enjoy using Redis on RHEL 8 / CentOS 8. If you’re interested in setting up monitoring for your Redis Server, then check:

RELATED ARTICLES

Most Popular

Recent Comments