Redis server setup on RHEL

In this blog, we will learn how to set up a Redis server step-by-step

Prerequisite:

Launch the AWS instance RHEL and ssh into it.

What is Redis

Redis is in the family of databases called key-value stores.

The essence of a key-value store is the ability to store some data, called a value, inside a key. This data can later be retrieved only if we know the exact key used to store it.

Often Redis it is called a data structure server because it has outer key-value shell, but each value can contain a complex data structure, such as a string, a list, a hashes, or ordered data structures called sorted sets as well as probabilistic data structures like hyperloglog.

Install Redis on Redhat Linux

Run the below command:

sudo yum install redis

systemctl start redis

systemctl enable redis

systemctl status redis

If you want to Install and configure the Redis server from the source use the below commands:

wget https://download.redis.io/redis-stable.tar.gz

yum install wget -y

wget https://download.redis.io/redis-stable.tar.gz

tar -xzvf redis-stable.tar.gz

cd redis-stable

sudo make >> error

Used link >> Used google search and bing search.
Used link > https://stackoverflow.com/questions/30692708/redis-linux-error-when-installing-redis-on-linux-cc-command-not-found

sudo yum groupinstall "Development Tools"

sudo yum install make

sudo make

sudo make install

sudo make install

systemctl status redis

Starting Redis CLI You can start the Redis command line interface using the following command:

redis-cli

Data structures in Redis server

Data structures in Redis server are different ways of storing and organizing data in memory. Each data structure has its own commands and features that make it suitable for different use cases.

As a first example, we can use the command SET to store the value "vin" at key "server:name":

SET server:name "vin"

Redis will store our data permanently, so we can later ask "What is the value stored at key server:name?" and Redis will reply with "vin"

GET server:name

There is a command to test if a given key exists or not:

EXISTS server:name

If the output is 1 then the key exists and if 0 then the key does not exist.

set server:name "vin"

OK

get server:name

"vin"

exists server:name

(integer) 1

exists server:surname

(integer) 0

set server:surname "salunke"

OK

exists server:surname

(integer) 1

  1. DEL: The DEL command is used to delete a specified key along with its associated value. It allows you to remove data from Redis.

  2. INCR: INCR is used for atomically incrementing a numeric value stored at a given key. This is particularly useful for implementing counters, where you need to increase a value by 1 each time it's called.

  3. INCRBY: INCRBY is similar to INCR, but it allows you to increment the number contained inside a key by a specific amount, which you specify as an argument.

  4. DECR: DECR is the counterpart to INCR. It is used to decrement the value stored at a key by 1. This is useful for reducing counters or decreasing values.

  5. DECRBY: DECRBY is similar to DECR, but it allows you to decrease the value inside a key by a specified amount, which you specify as an argument.

set con 10

OK

incr con

(integer) 11

get con

"11"

incr con

(integer) 12

del con

(integer) 1

get con

(nil)

incr con

(integer) 1

get con

"1"

get surname

(nil)

get server:surname

"salunke"

del server:surname

(integer) 1

get server:surname

(nil)

del server:name

(integer) 1

set server:name

OK

get server:name

""

set server:name "vinayak"

OK

get server:name

"vinayak"

> get con
"1"
> incrby con 1000
(integer) 1001
> decr con
(integer) 1000
> decr con
(integer) 999
> decr con
(integer) 998
> decrby con 10
(integer) 988
> decrby con 100
(integer) 888

It is also possible to increment the number contained inside a key by a specific amount:


    INCRBY connections 100 => 101

And there are similar commands in order to decrement the value of the key.


    DECR connections => 100
    DECRBY connections 10 => 90

When you manipulate Redis strings with incrementing and decrementing commands, you are implementing counters. Counters are a very popular application for Redis.

There is something special about INCR. Why do we provide such an operation if we can do it ourself with a bit of code? After all it is as simple as:

x = GET count
x = x + 1
SET count x

The problem is that doing the increment in this way will only work as long as there is a single client using the key. See what happens if two clients are accessing this key at the same time:

  1. Client A reads count as 10.

  2. Client B reads count as 10.

  3. Client A increments 10 and sets count to 11.

  4. Client B increments 10 and sets count to 11.

We wanted the value to be 12, but instead it is 11! This is because incrementing the value in this way is not an atomic operation. Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation.

All the Redis operations implemented by single commands are atomic, including the ones operating on more complex data structures. So when you use a Redis command that modifies some value, you don't have to think about concurrent access.

Redis can be told that a key should only exist for a certain length of time. This is accomplished with the EXPIRE and TTL commands, and by the similar PEXPIRE and PTTL commands that operate using time in milliseconds instead of seconds.

  • The EXPIRE command sets a key to expire after a specified number of seconds.

  • The TTL command is used to check how many seconds remain until a key expires. If the key no longer exists, it returns -2, and if the key will never expire, it returns -1.

  • PERSIST command is used to cancel the expiration of a key, making it permanent. This can be useful when you want to retain a key indefinitely after initially setting it to expire.

I will add some more Redis complex commands in my next blog.

If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I'll be more than happy to help! ๐Ÿ˜Š๐Ÿ‘๐Ÿ‘ฅ

0
Subscribe to my newsletter

Read articles from Vinayak Salunkhe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinayak Salunkhe
Vinayak Salunkhe

DevOps Engineer with 8+ Years of Experience | AWS, Azure DevOps, Linux | CKA & RHCSA Certified | Docker+K8S Expert