Redis

https://redis.io/

Install

Add the repository:

$ curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
$ echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb jammy main

Install:

$ sudo apt update
$ sudo apt install redis

Check the version:

$ redis-cli -v
redis-cli 8.0.1

Configure

Edit file /etc/redis/redis.conf to enable cluster mode:

 # You will also need to set a password unless you explicitly disable protected
 # mode.
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-bind 127.0.0.1 -::1
+bind * -::*
 
 # By default, outgoing connections (from replica to master, from Sentinel to
 # instances, cluster bus, etc.) are not bound to a specific local address. In
 # By default protected mode is enabled. You should disable it only if
 # you are sure you want clients from other hosts to connect to Redis
 # even if no authentication is configured.
-protected-mode yes
+protected-mode no
 
 # Redis uses default hardened security configuration directives to reduce the
 # attack surface on innocent users. Therefore, several sensitive configuration
 # started as cluster nodes can. In order to start a Redis instance as a
 # cluster node enable the cluster support uncommenting the following:
 #
-# cluster-enabled yes
+cluster-enabled yes
 
 # Every cluster node has a cluster configuration file. This file is not
 # intended to be edited by hand. It is created and updated by Redis nodes.

Caution

The protected mode is off and the service is opened to outside world, which is dangerous. The configurations is for learning and testing in internal network only.

Start all nodes, then create the cluster:

$ redis-cli --cluster create las0:6379 las1:6379 las2:6379
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: b099b5bbc985bd735380104d85b56577c7bb5e54 las0:6379
   slots:[0-5460] (5461 slots) master
M: 9b0c93352947c54494582c66e9afb1491f612325 las1:6379
   slots:[5461-10922] (5462 slots) master
M: 9baccecd0d7e6ae2730ef18f2f885843cfb2058d las2:6379
   slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join

>>> Performing Cluster Check (using node las0:6379)
M: b099b5bbc985bd735380104d85b56577c7bb5e54 las0:6379
   slots:[0-5460] (5461 slots) master
M: 9baccecd0d7e6ae2730ef18f2f885843cfb2058d 10.225.4.53:6379
   slots:[10923-16383] (5461 slots) master
M: 9b0c93352947c54494582c66e9afb1491f612325 10.225.4.52:6379
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Usage

In a Redis cluster, the key is hashed and distributed to so-called “slots” on each master node. To find the slot of a key:

$ redis-cli cluster keyslot myKey
(integer) 16281

Set/Get data by key:

$ redis-cli -c set myKey myValue
OK
$ redis-cli -c get myKey
"myValue"

If we do this in redis-cli prompts:

$ redis-cli -c
127.0.0.1:6379> set myKey myNewValue
-> Redirected to slot [16281] located at 10.225.4.53:6379
OK
10.225.4.53:6379> get myKey
"myNewValue"
10.225.4.53:6379> exit

We can see additional messages of “Redirect”. Note the -c parameter to invoke redis-cli, which is required to handle redirecting in cluster mode.