Redis cluster of redis

Posted by chadowch on Fri, 14 Jan 2022 10:27:36 +0100

1. Defects of sentinel mode

In sentinel mode, there is still only one Master node. Sentinel mode does not relieve write pressure when concurrent write requests are large.

2. Redis cluster concept

(1) A distributed network service cluster composed of multiple Redis servers;

(2) There are multiple Master master nodes in the cluster, and each Master node is readable and writable;

(3) Nodes communicate with each other and are connected in pairs;

(4) Redis cluster has no central node.

3. Cluster node replication

In redis cluster, you can add a slave node to each master node. The master and slave nodes directly follow the characteristics of the master-slave model.

When users need to process more read requests, adding slave nodes can expand the read performance of the system.

4. Failover

The primary node of Redis cluster has built-in node fault detection and automatic failover functions similar to Redis Sentinel. When a primary node in the cluster goes offline, other online primary nodes in the cluster will notice this and failover the offline primary nodes.

The failover method of the cluster is basically the same as that of Redis Sentinel. The difference is that in the cluster, the failover is carried out by other online master nodes in the cluster, so the cluster does not need to use Redis Sentinel.

5. Cluster fragmentation strategy

Redis cluster fragmentation strategy is used to solve the problem of key storage location.

The cluster divides the entire database into 16384 slot slots, and all key value data is stored in one of these slots. A slot can store multiple data. The slot calculation formula of key is: slot_number=crc16(key)%16384, where crc16 is a 16 bit cyclic redundancy checksum function.

Each master node in the cluster can process 0 to 16383 slots. When a node is responsible for processing 16384 slots, the cluster enters the online state and starts to process the data command requests sent by the client.

6. Cluster redirect

Since the Redis cluster has no central node, the request will be sent to any primary node randomly;

The master node will only process the command request of its own slot. For the command request of other slots, the master node will return a steering error to the client;

The client sends a command request to the correct responsible master node again according to the address and port contained in the error.

7. Cluster construction

7.1 description

redis5. After version 0, you can directly use the redis cli command to create clusters without redis trib RB command.

7.2 preparation

  • Redis cluster requires at least 6 nodes, 3 master and 3 slave, which can be distributed on one or more hosts.

True cluster: there are 6 hosts. The redis service of each host uses different IP addresses and port numbers. The same and different can be used

In a fake cluster, a host uses the same IP and different port numbers for redis services

This example is to create a fake cluster on a host. Different ports represent different redis nodes, as follows:

Primary node: 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381

Slave node: 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384

Main nodes newly added in the later stage: 127.0.0.1:6385

Slave node of new home in later stage: 127.0.0.1:6386

  • Copy redis Conf is configured in / usr/local/myredis / directory and renamed

  • Modify profile
    port 6379
    
    daemonize yes
    
    pidfile /var/run/redis/redis-6379.pid
    logfile /usr/local/redis/var/redis-6379.log
    dir "./" # node.conf file save path
    dbfilename dump-6379.rdb
    appendonly yes
    appendfsync always
    cluster-enabled yes
    cluster-config-file nodes-6379.conf

  • Batch start close script

Start script start_cluster.sh

#!/bin/bash
cd /usr/local/myredis/
/usr/local/redis/bin/redis-server redis-6379.conf
/usr/local/redis/bin/redis-server redis-6380.conf
/usr/local/redis/bin/redis-server redis-6381.conf
/usr/local/redis/bin/redis-server redis-6382.conf
/usr/local/redis/bin/redis-server redis-6383.conf
/usr/local/redis/bin/redis-server redis-6384.conf

Close script stop_cluster.sh

pgrep redis-server | xargs -exec kill -9

Start 6 services

./start_cluster.sh 

  • Execute the create cluster command
# --Cluster replicas 1 indicates the master-slave configuration ratio. 1 indicates 1:1. The first three are master and the last three are slave

# If the password is set in the configuration file, you also need to add - a passwod
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1

Error occurred!!!

Treatment method:

Delete the pid file, aof file, rdb file and log file of the cluster, and restart the cluster service

Connect each cluster client and execute the following command to create the cluster again

CLUSTER RESET

7.3 cluster management

  • Create cluster
redis-cli --cluster create host1:port1 ... hostN:portN --cluster-replicas <arg>
#example
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
# Example description

host1:port1 ... hostN:portN Represents the node of the cluster to be added IP And ports,
--cluster-replicas <arg>Indicates the proportion of master-slave nodes. Parameter 1 indicates that the first three are master nodes and the last three are slave nodes
 That is 6379,6380,6381 The node corresponding to the port is the primary node, 6382,6383,6384 The corresponding node is the slave node
  • Query cluster node information
redis-cli -c -h 127.0.0.1 -p 6379 cluster nodes

  • Add a new master node
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-master-id node_id
# example
redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6381 --cluster-master-id 18783e7b499b0c07227bc2b08bb9dd4f9c574528
# Example description

new_host:new_port For the new master node to add IP And port, 127 here.0.0.1:6385

existing_host:existing_port Represents the name of the last master node that already exists IP And port, which can be viewed from the above node information slots Number of slots. The number of node slots corresponding to port 6381 is 10923-16383
,16383 Indicates the last number of slots

--cluster-master-id Represents the node of the last master node id,Indicates that the newly added master node should be behind this node

  • hash slot reallocation

After adding a new node, the newly added primary node needs to be reassigned with hash slots so that the primary node can store data. redis has 16384 slots in total.

redis-cli --cluster reshard host:port --cluster-from node_id --cluster-to node_id --cluster-slots <args> --cluster-yes
# example

redis-cli --cluster reshard 127.0.0.1:6385 --cluster-from e406b1ea8e1ce5b8c012ae7acef20ea13981a6a0 --cluster-to ce04b81c35b890a332b005014ea66fc9bf945ccc --cluster-slots 500 --cluster-yes 

# Example description

host:port Represents the newly added master node IP And port, 127 here.0.0.1:6385

--cluster-from node_id Represents the node of the first primary node of the cluster id,This can be an existing cluster slots According to the slot number allocation, the node corresponding to port 6379 is represented here

--cluster-to node_id Represents the node of the last master node of the cluster id,That is, the newly added master node id,The node corresponding to port 6385 is shown here

--cluster-slots 500 Indicates how much is allocated to the new master node. Here, 500 indicates that the allocation is from 0-499 individual slots Number of slots. If this is not added, it will be entered manually

--cluster-yes Indicates that the automatic response is yes,If you don't add this, you'll have to enter it manually yes,Agree to this assignment

It will be found that the primary node corresponding to port 6385 already has slots, starting from 0

  • Add new slave node
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id node_id
# example

redis-cli --cluster add-node 127.0.0.1:8386 127.0.0.1:8385 --cluster-slave --cluster-master-id ce04b81c35b890a332b005014ea66fc9bf945ccc

# Example description

new_host:new_port Represents the slave node to be added IP And port, 127 here.0.0.1:8386

existing_host:existing_port It indicates which master node to add a slave node to. Here, it indicates 127.0.0.1:8385

--cluster-slave It means to add a slave node, otherwise it means to add a master node
--cluster-master-id node_id Indicates which master node to add the slave node to id
  • Delete node
redis-cli --cluster  del-node host:port node_id
# example

redis-cli --cluster del-node 127.0.0.1:7008 415db07121ba946b202bca98e15cbdffc60bc18a

# Example description
host:port Indicates the name of the node to be deleted IP And port, 127 here.0.0.1:7008

node_id Represents the node of the deleted node id

Topics: Database Redis nosql