当前位置:网站首页>Refresh your understanding of redis cluster
Refresh your understanding of redis cluster
2022-07-28 11:24:00 【51CTO】
Redis colony
Redis Cluster adoption Fragmentation To share data , It also provides replication and failover capabilities .
1. node
1.1 Node handshake
Redis Each node in the cluster uses CLUSTER MEET Command to connect .
To a node node send out CLUSTER MEET command , It can make node Node and ip and port At the specified node handshake . After a successful handshake ,node Node adds the target node to node The node in the cluster .
Repeat this operation , Multiple nodes can be in the same cluster .

1.2 Cluster data structure
- clusterNode structure Saves the current state of a node , Including node creation time 、 Node name 、 Node configuration era 、 node IP Address and port number and so on .
- clusterLink structure yes clusterNode A property of , Save the information needed to connect nodes , Like socket descriptors 、 Input buffer, output buffer, etc .
- clusterState structure Saved by each node , The status of the cluster from the perspective of the current node is recorded , For example, whether the cluster is online or offline , How many nodes does the cluster contain , The current configuration era of the cluster and so on .
1.3 CLUSTER MEET Command implementation
Node receiving command A Connect with the target node B Conduct handshake .

2. Slot assignment
2.1 Redis Slot in
Redis The cluster stores key value pairs in the database through sharding , The entire database of the cluster is divided into 16384 Slot , Every key in the database belongs here 16384 One of the slots , Each node in the cluster can be processed 0 Or at most 16384 Slot .
When in the database 16384 Each slot has a node processing , Cluster in the Online status . If there is no node processing in any slot , So the cluster is in Offline status .
By sending to the node CLUSTER ADDSLOTS command , We can assign one or more slots to the node . For example, the following command can change the slot 0~5000 Assign to node 7000 be responsible for :
2.2 Record the slot assignment information of the node
clusterNode Structural slots Properties and numslot Property records which slots the node is responsible for processing .
2.3 Propagate the slot assignment information of the node
Nodes will send their own slots The array is sent by message to the other nodes in the cluster , Tell them which slots they are currently dealing with .
When node A Slave node via message B There the node is received B Of slots Array time , It will be on its own clusterState.nodes Look up the node in the dictionary B Corresponding clusterNode structure , And update .
3. Execute commands in the cluster
The online cluster can execute commands .
When the client sends a command to the node , The node receiving the command will check which slot the database key to be processed by the command belongs to , And check that the slot is assigned to yourself .

3.1 Calculate which slot the key belongs to
redis Use the following algorithm to calculate the given key Which slot does it belong to
CRC16(key) Statement calculates key Of CRC-16 The checksum , and & 16383 Calculate a value between 0 and 16383 An integer between as key Slot number of .
CLUSTER KEYSLOT The command is implemented according to the slot allocation algorithm above .
3.2 Determine whether the slot is in the charge of the current node
Check your own slots The corresponding position in the array .
3.3 MOVED error
MOVED The wrong format is :
The client can be based on MOVED error , Turn to the right node .
3.4 The implementation of the node database
In addition to saving key value pairs in data , Nodes also use clusterState The structure of the slots_to_keys Jump table to save the relationship between slot and key .

The score of each node of the hop table is a slot number . The member of each node is a database key .
Use this jump table , It is convenient to batch operate all database keys belonging to some slots .
4. The shard
4.1 Re segmentation process
Redis The re fragmentation operation of the cluster can cause the node assigned to a slot to be reassigned to another node . Re sectioning can be done online , In the process, the cluster does not need to go offline , And can handle command requests normally .
Re slicing by Redis Cluster management software redis-trib Responsible for the execution of .

The re slicing process is shown in the figure above . If the slot to be sliced belongs to multiple nodes , You have to send commands to multiple nodes .
4.2 ASK error
A situation may occur during re segmentation : The key value pairs belonging to the migrated slot are saved in the source node , Another part of key value pairs is saved in the target node .
At this time, when the client sends a command to the source node , And the database key required to be processed happens to be being migrated :
- The source node now finds its own data , If you find it, send the command directly .
- If not found, return to the client ASK error , Direct the client to the target node to execute the command .

One ASK The error is shown in the figure below :

received ASK The wrong client will provide according to the error IP Address and port number , Turn to the target node , Send a message to the target node first ASKING command , Then resend the original command .

ASKING Command can open the of the client sending the command REDIS_ASKING identification . This is useful when the server receives the following commands .

The server that is in the process of re fragmentation judges the status of the client sending the request ASKING Identify whether to open
- If opened , The client's request is a redirection request during slot dispatch , Normal execution .
- If it doesn't open , The client's request is an ordinary command request with addressing error , return MOVED error .
5. Replication and failover
Redis The primary node in the cluster is used for processing slots , The slave node is used to replicate a master node , And take over the copied master node when it goes offline .
Suppose the status between servers in a certain scenario is as follows :

If the main server 7000 Offline , Then the remaining primary servers are 7001、7002、7003 From 7000 Two slave servers 7004、7005 Select a takeover 7000 Responsible tank . The other slave server will become the slave server of the new master server .

If the follow-up 7000 Back online , It will become 7004 The new master node of .

5.1 Fault detection
Each node in the cluster will regularly send messages to other nodes in the cluster PING news , To check whether the other party is offline . If you receive PING The node of did not return within the specified time PONG news , Will be marked as Suspected offline .
Nodes in the cluster have three states from the perspective of other nodes : On-line 、 Suspected offline 、 Offline .
When a master node A The master node is informed by the message B Think of the master node C When entering the suspected offline , Master node A It will be on its own clusterState.nodes Find the master node in the dictionary C Corresponding clusterNode structure , And the master node B Of offline reports added to clusterNode Structural fail_reports In the list .
If you're in a cluster , A master node finds that more than half of the master nodes will a master node x Mark as meaning offline , Then he will mark the master node as offline , And broadcast to all other nodes FAIL news .

5.2 Fail over
When a slave node finds that the master node it is copying is offline , The slave node will start to fail over the offline master node :
- The primary node that finds the problem first holds an election , Call yourself the new master node . Election rules and Sentinel Middle election leader Sentinel Very similar , They're all based on Raft The leading election method of algorithm .
- Success is called the new master node
- If it fails, wait for the next slave node to propose an election .
- The selected slave node performs SLAVEOF no one command , Becomes the new master node .
- The new master node takes over all slot assignments of the offline master node .
- The new master node broadcasts PONG news , Let other nodes know that they have become the master node .
- The new master node starts to perform the duties of the master node
边栏推荐
- 融云 IM & RTC 能力上新盘点
- Office2013 input mathematical formula above
- Game theory 1. Introduction (basic concepts of combination games, confrontation search, bash games, Nim games)
- leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
- "Node learning notes" koa framework learning
- 精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”
- Two point, three point, 01 point plan [bullet I]
- The use of C language linked list
- Rongyun IM & RTC capabilities on new sites
- 本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
猜你喜欢
JWT login authentication + token automatic renewal scheme, well written!

Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems
![[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03](/img/50/754f7915ee4b2e04fdd21ae68130c6.png)
[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03

Office2013 input mathematical formula above

不用Swagger,那我用啥?

精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”

mysql还有哪些自带的函数呢?别到处找了,看这个就够了。

Sword finger offer 35. replication of complex linked list

什么是WordPress

什么样的知识付费系统功能,更有利于平台与讲师发展?
随机推荐
Cortex-M内核管理全局中断的三种方式
表格数据处理软件,除了Excel还有什么?
使用共用体union及指针测试大小端
Two point, three point, 01 point plan [bullet I]
「Node学习笔记」Koa框架学习
ZBrush 2022软件安装包下载及安装教程
A solution to the problem that ThinkPad fingerprint verification cannot be used in win7
2021-03-24
What is WordPress
使用c语言实现双向链表
Two point, three point, 01 point plan [bullet III]
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
Machine learning strong foundation plan 0-5: why is the essence of learning generalization ability?
构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
Use the statement object to execute DDL statements to create tables
What functions does MySQL have? Don't look everywhere. Just look at this.
什么样的知识付费系统功能,更有利于平台与讲师发展?
不用Swagger,那我用啥?
[MySQL] MySQL error "error 2006 (HY000): MySQL server has gone away"
[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03