Redis cluster and cluster construction study notes

Redis cluster

1 concept

Redis cluster realizes the horizontal expansion of Redis, that is, starts N redis nodes, distributes and stores the entire database in these N nodes, and each node stores 1N of the total data. Redis cluster provides a certain degree of availability (availability) through partition: Even if some nodes in the cluster fail or cannot communicate, the cluster can continue to process command requests.

2 Redis cluster construction

(1) Empty persistent files such as rdb

image-20211015154146210

image-20211015154212701

(2) Create six configuration files redis6379.conf, redis6380.conf, redis6381.conf, redis6389.conf, redis6390.conf, redis6391.conf, and modify the configuration

​ Take 6379 as an example, add several cluster configurations on top of the basic configuration

cluster-enabled yes Turn on the cluster mode

cluster-config-file nodes-6379.conf set the node configuration file name

cluster-node-timeout 15000 Set the node disconnection time, beyond this time (milliseconds), the cluster will automatically switch between master and slave

image-20211015160254798

Similarly, configure the other five files, just change 6379 to the corresponding id

image-20211015160323002

(3) Start 6 redis services

image-20211015160525697

check on status

image-20211015160628418

Check that the nodes-xxxx.conf file is correctly generated

image-20211015160835491

(4) Combine 6 nodes into a cluster

First enter the src directory in the redis installation directory, where my redis is installed under /opt

image-20211015161028810

使用redis-cli --cluster create --cluster-replicas 1 192.168.33.11:6379 192.168.33.11:6380 192.168.33.11:6381 192.168.33.11:6389 192.168.33.11:6390 192.168.33.11:6391

–replicas 1 means to configure the cluster in the simplest way, one master, one slave, exactly three groups

image-20211015161639220

After that, you will be asked whether to accept this distribution method, yes

image-20211015162405747

If there are some problems, you can use the redis-cli --cluster fix 192.168.33.11:6379 command to fix the problematic node

redis-cli --cluster check 192.168.33.11:6379 can check what's wrong with the node

(5) Connect to the cluster

Connect through redis-cli -c -p 6379, -c indicates that the cluster policy is used to connect, and the setting data will be automatically switched to the corresponding host, because the redis cluster uses a non-center connection, so any node can be used to connect, that is, the command 6379 in can be replaced with other port numbers, such as 6380

image-20211015162800239

(6) View node information

You can view the node information in the cluster through cluster nodes

image-20211015163854551

3 What are slots

A Redis cluster contains 16384 slots (hash slot), and each key in the database belongs to one of these 16384 slots.
​ The cluster uses the formula CRC16(key) % 16384 to calculate which slot the key belongs to, where the CRC16(key) statement is used to calculate the CRC16 checksum of the key. Each node in the cluster is responsible for handling a subset of slots

​ Insert k1 v1 into the cluster. After calculation, the data should be inserted into the slot 12706, but the node 6379 maintains the slot 0-5460, and 12706 is not in this range. It is found that it is within the range maintained by 6381 , so it is directly inserted into the maintenance slot of 6381, and it will automatically switch to the 6381 node at this time

image-20211015165649753

image-20211015165909574

You can use cluster keyslot [key] to calculate which slot the key is in

image-20211015170337828

Use cluster countkeysinslot [slot] to view the number of keys in the slot

image-20211015170534686

cluster getkeysinslot [slot] [count] returns the keys in the count slot slots

image-20211015170650837

4 Failure Recovery

​ If the host goes down, the slave will replace the original host as the host, and if the original host connects again, it will become the slave.

​ If the master and slave of a certain slot are down, and the cluster-require-full-coverage configuration in the configuration file is yes, then the entire cluster will hang up, and if it is no, then all the slot data cannot be used, and cannot store

Guess you like

Origin blog.csdn.net/weixin_42195126/article/details/120787271