Home CentOS Install and Configure Redis Server on RHEL 8 / CentOS 8

Install and Configure Redis Server on RHEL 8 / CentOS 8

0
462
(Last Updated On: January 9, 2019)

How can I install Redis on RHEL 8?. This guide will help you to install and configure Redis server on RHEL 8. 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.

$ yum module list redis
Updating Subscription Management repositories.
Updating Subscription Management repositories.
Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs)
Name Stream Profiles Summary
redis 4.0 [d] default [d] Redis persistent key-value database
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

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

sudo yum install -y @redis

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

sudo systemctl enable --now redis

Service status should show running.

$ sudo 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 Sat 2018-12-29 10:00:40 EAT; 5s ago
Main PID: 30348 (redis-server)
Tasks: 4 (limit: 11510)
Memory: 6.4M
CGroup: /system.slice/redis.service
└─30348 /usr/bin/redis-server 127.0.0.1:6379
Dec 29 10:00:40 rhel8.local systemd[1]: Starting Redis persistent key-value database…
Dec 29 10:00:40 rhel8.local systemd[1]: Started Redis persistent key-value database.

Configure Redis Server on RHEL 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 <->

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 --permanenent
sudo firewall-cmd --reload

Check redis service status:

$ sudo 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 Sat 2018-12-29 10:11:56 EAT; 9s ago
Process: 30485 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=0/SUCCESS)
Main PID: 30500 (redis-server)
Tasks: 4 (limit: 11510)
Memory: 6.4M
CGroup: /system.slice/redis.service
└─30500 /usr/bin/redis-server 0.0.0.0:6379
Dec 29 10:11:56 rhel8.local systemd[1]: Stopped Redis persistent key-value database.
Dec 29 10:11:56 rhel8.local systemd[1]: Starting Redis persistent key-value database…
Dec 29 10:11:56 rhel8.local systemd[1]: Started Redis persistent key-value database.

 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:4.0.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:fdf31b4ab3504500
redis_mode:standalone
os:Linux 4.18.0-32.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.2.1
process_id:30500
run_id:d8c5ba56a0735a6831a0b3467c3efa95ac174cdd
tcp_port:6379
uptime_in_seconds:222
uptime_in_days:0
hz:10
lru_clock:2563866
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:

How to Monitor Redis Server with Prometheus and Grafana in 5 minutes