# Redis ## Install ::::{tab-set} :::{tab-item} Ubuntu :sync: ubuntu Add the repository: ```console $ 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: ```console $ sudo apt update $ sudo apt install redis ``` ::: :::: Check the version: ```console $ redis-cli -v redis-cli 8.0.1 ``` ## Configure Edit file `/etc/redis/redis.conf` to enable cluster mode: :::{literalinclude} /_files/ubuntu/etc/redis/redis.conf :diff: /_files/ubuntu/etc/redis/redis.conf.orig ::: :::{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: ```console $ 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: ```console $ redis-cli cluster keyslot myKey (integer) 16281 ``` Set/Get data by key: ```console $ redis-cli -c set myKey myValue OK $ redis-cli -c get myKey "myValue" ``` If we do this in `redis-cli` prompts: ```console $ 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.