We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install and Configure Redis Server on Rocky Linux 9

Learn how to – Install and Configure Redis Server on Rocky Linux 9.

Redis is a free and open-source in-memory data store used all over the world. It mainly serves as a distributed in-memory key-value database, cache, or message broker. Redis supports several data structures such as Lists, Hashes, Sets, Strings e.t.c

Redis can be installed as a standalone server or as a high availability cluster in a production environment using Redis Sentinel. More information on how to set up a Redis cluster has been captured in the guide below:

Redis is known for holding a high reputation for high performance, broad support, high availability, and flexibility. There are several other features associated with Redis such as:

  • In-memory data structures: Redis is a well-known data structure server that offers support for lists, strings, hashes, sorted sets, sets, streams e.t.c
  • Extensibility: It provides a module API that can be used to build extensions in C, C++, and Rust languages.
  • Persistence: It keeps that dataset in memory to allow fast access. Redis can also be configured to persist the data by writing it to a permanent storage disk to survive reboots.
  • Clustering: It supports horizontal scalability with hash-based sharding and can scale to millions of nodes with automatic re-partitioning when the cluster is growing.
  • High availability: It offers replication with automatic failover in both standalone and clustered modes.
  • Programmability: It allows server-side scripting with Lua and server-side stored procedures with Redis Functions.

Due to the amazing features, Redis finds use in the following areas:

  • Streaming and messaging: where high-speed data ingestion, messaging, event sourcing, and notifications are required.
  • Caching and session storage: its speed makes it perfect for caching database queries, API calls, complex computations, and session state
  • Real-time data store: Redis provides in-memory data structures that can allow you to build real-time applications with low latency and high performance.

By following this guide to the end, you should be able to install and configure Redis Server on Rocky Linux 9.

1. Install Redis on Rocky Linux 9

Redis 6.2 resides in the default Rocky Linux 9 repositories and can be installed by running the command:

sudo dnf install vim redis

Dependency Tree:

Transaction Summary
================================================================================
Install  1 Package

Total download size: 1.3 M
Installed size: 4.7 M
Is this ok [y/N]: y

Wait for the installation to complete then start and enable the service:

sudo systemctl start redis
sudo systemctl enable redis

Verify if the service is 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 Fri 2022-07-29 14:07:36 CEST; 7s ago
   Main PID: 31290 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 23441)
     Memory: 7.3M
        CPU: 15ms
     CGroup: /system.slice/redis.service
             └─31290 "/usr/bin/redis-server 127.0.0.1:6379"

Check the installed version:

$ redis-server --version
Redis server v=6.2.6 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=9664d239a7dd0d5e

$ redis-cli --version
redis-cli 6.2.6

2. Configure Redis on Rocky Linux 9

Redis stores its configuration file at /etc/redis/redis.conf. This file can be edited as desired.

First, open the file for editing;

sudo vim /etc/redis/redis.conf

In the file, there are several adjustments you can make. These are:

Bind Address

by default, Redis is configured to listen on localhost. This can be changed to you IP address as shown:

bind 127.0.0.1 -::1 192.168.205.12

Daemonize Redis

This allows Redis to run as a daemon

daemonize yes

Set the service to interact with systemd.

supervised systemd

Secure Redis

The Redis server is not secure by default, anyone can access it and manipulate the data. To secure it, set a password as shown;

requirepass  StrongPassword

Once the desired configurations have been made, save the file and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart redis

Redis listens on port 6379 by default, allow this port through the firewall;

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

3. Using Redis on Rocky Linux 9

Redis can be accessed and used using the Redis CLI from the localhost as shown;

$ redis-cli

Then authenticate to the database using the command:

127.0.0.1:6379> auth StrongPassword
OK
127.0.0.1:6379> exit

Alternatively, you can authenticate to the database with a single command. For example;

$ redis-cli -a StrongPassword -n 1 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379[1]> exit

To avoid the above warning, you can use the –no-auth-warning flag

$ redis-cli -a StrongPassword -n 1 --no-auth-warning
127.0.0.1:6379[1]> 

To switch to another database, provide the database-ID. For example:

127.0.0.1:6379[1]> select 2
OK

Get information about your server

 127.0.0.1:6379[2]> info 
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9664d239a7dd0d5e
redis_mode:standalone
os:Linux 5.14.0-70.13.1.el9_0.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:11.2.1
process_id:31419
process_supervised:systemd
run_id:90b6e7b18bb5137d47f8c5539c46ffa05a30f451
tcp_port:6379
server_time_usec:1659097502902846
uptime_in_seconds:731
uptime_in_days:0
hz:10
......

To view connected clients, use:

127.0.0.1:6379> client list 
id=5 addr=127.0.0.1:60324 laddr=127.0.0.1:6379 fd=9 name= age=446 idle=0 flags=N db=2 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=40928 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 events=r cmd=client user=default redir=-1

To terminate the connection to a client, use the command with the syntax;

127.0.0.1:6379> client kill 10.0.0.51:53338 
OK

To save data, use the command:

127.0.0.1:6379> save 
OK

You can also save data in the background:

127.0.0.1:6379> bgsave 
Background saving started

To obtain the UNIX timestamp of the last save:

127.0.0.1:6379> lastsave 
(integer) 1659097794

To save data and shutdown Redis, use the commands:

127.0.0.1:6379> shutdown 
not connected> quit 

After this, the Redis server will not be running, verify this:

ps aux | grep [r]edis

You need to start it again to use it;

sudo systemctl start redis

Connect to Redis Remotely

It is possible to connect to a running Redis instance remotely using the command with the syntax below:

redis-cli -h IP_Address/Domain_name

For example:

Using Redis on Node.js

This is an example of how Redis can be used with Node.js. Begin by installing the Redis Client Module;

sudo dnf install npm -y 
npm install redis

Now create the Node.js application that uses the Redis database:

vim redis_sample.js

In the file, add the lines.

const redis = require('redis');
const client = redis.createClient({
    socket: {
        host: '127.0.0.1',
        port: 6379
    },
    password: 'StrongPassword'
});
async function addToSortedSet() {
  await client.connect();
  await client.zAdd('mysortedset', [
    {
      score: 97,
      value: 'Ninety seven'
    },
   {
      score: 98,
      value: 'Ninety eight'
    },
    {
      score: 99,
      value: 'Ninety Nine'
    },
    {
      score: 100,
      value: 'One Hundred'
    },
    {
      score: 101,
      value: 'One Hundred and One'
    }
  ]);

  // Get all of the values/scores from the sorted set using
  // the scan approach:
  // https://redis.io/commands/zscan
  for await (const memberWithScore of client.zScanIterator('mysortedset')) {
    console.log(memberWithScore);
  }

  await client.quit();
}

addToSortedSet();

The above example demonstrates how to create a sorted list. Remember to replace your Redis Server password accordingly. Save the file and run the application.

node redis_sample.js

Sample Output;

If the password is wrong, you’ll get output like below:

$ node redis_sample.js
node:internal/process/promises:265
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[ErrorReply: WRONGPASS invalid username-password pair or user is disabled.]

For easier use, you can use a GUI tool. The below guide shows how to install and use the Redis Desktop Manager

Closing Thoughts

We have gone through how to install and configure Redis Server on Rocky Linux 9. We also learned the basic commands on Redis CLI and how Redis can be used with Node.js. I hope this was significant.

More guides:

Cart

Your Cart is Empty

Back To Shop