当前位置:网站首页>Redis Cluster - the bottom principle of building clusters
Redis Cluster - the bottom principle of building clusters
2022-06-13 07:33:00 【A hard-working dog】
Redis colony
Redis Clusters are Redis The distributed database solution provided , Clustering is done by fragmentation (sharding) To share data , It also provides replication and failover capabilities .
node
One Redis A cluster usually consists of multiple nodes ( Running in cluster mode Reids The server )
// The work of connecting various nodes can use CLUSTER MEET <IP><port>
Send... To a node CLUSTER MEET command , It can make node Node and ip and port Handshake with the specified node (handshake), If the handshake is successful , that node The node will ip and port The specified node is added to node In the cluster .
Data structure of cluster
clusterNode The structure holds the current state of a node : Such as the creation time of the node 、 Node name 、 The current configuration level element of the node 、 Node IP And port numbers, etc .
One for each node clusterNode Structure to keep track of your state , And for all other nodes in the cluster ( Including master node and slave node ) Create a corresponding clusterNode structure , To record the status of other nodes .
struct clusterNode{ // Time of node creation mstime_t ctime; // Node name , from 40 It's made up of 16 hexadecimal characters char name[REDIS_CLUSTER_NAMELEN]; // Node identification , Use different identity values to record the role of the node , And the current state of the node int flags; // The current configuration era for the node , For failover uint64_t configEpoch; // Node ip Address char Ip[Redis_ip_str_len]; // The port number of the node int port; // Save the information needed to connect nodes clusterLink *link; }
struct clusterLink{ // Time when the connection was created mstime_t ctime; //TCP socket descriptor int fd; // Output buffer , Save messages waiting to be sent to other nodes sds sndbuf; // Input buffer , Save messages received from other nodes sds rcvbuf; // The node associated with this connection , If not, for null struct clusternode * node; }
Each node holds a clusterState structure , This structure records the current status of the current node cluster
typedf struct clusterState{ // Pointer to the current node clusterNode *myself; // The current configuration era for the node , For failover uint64_t configEpoch; // The current state of the cluster : Online or offline int state; // The number of nodes in the cluster that handle at least one slot int size; // Cluster node list ( Include myself) // The key of the dictionary is the name of the node , The value of the dictionary is... Corresponding to the node clusterNode The pointer dict *nodes; }clusterState
CLUSTER MEET Implementation of commands
Client to node A send out CLUSTER MEET command , Let node A The nodes B Add to cluster , Node receiving command A Will be on node B A handshake , To confirm each other's existence .
CLUSTER MEET <ip><port>
1. node A Will be for the node B Create a clusterNode structure ,, And add the structure to your own clusterState.Nodes In the dictionary 2. after , node A Based on the CLUSTER MEET The command is given IP And port number , To the node B Send a MEET news 3. If all goes well , node B Will receive node A Sent MEET news , node B For the node A Create a ClusterNode structure , And add the structure to your own clusterState.Nodes In the dictionary 4. after , node B To the node A Return a PONG news 5. If all goes well , Then the node A At the receiving node B Back to PONG news , Through this article PONG Message node A You can know the node B I have successfully received the one I sent MEET news 6. after , node A To the node B Return a PING news 7. If all goes well , node B Will receive node A Back to PING news , Through this article PING Message node B You can know the node A You have successfully received your returned PONG news , Shake hands with success . 8. After the node A The node will be B Information through Gossip The protocol is propagated to other nodes in the cluster , Let other nodes also be associated with nodes B A handshake , Last node B Will be recognized by all nodes in the cluster .
边栏推荐
猜你喜欢
Redis cluster parsing docker building redis cluster
Oracle problem: the data in the field is separated by commas. Take the data on both sides of the comma
How idea breaks point debugging
Table access among Oracle database users
How worker threads in the thread pool are recycled
About database: pgadmin4 editing SQL window
Redis learning journey master-slave replication
Simple understanding of basic language of C language
A learning dog
关于#数据库#的问题:PGADMIN4 编辑sql窗口问题
随机推荐
理財產品連續幾天收益都是零是怎麼回事?
Lightning data import
Three handshakes and four waves of TCP protocol and why------ One two pandas
理财产品连续几天收益都是零是怎么回事?
Classification of databases
C # Advanced Programming - Feature Section
Logback log framework learning and problems
部署RDS服务
Wechat applet - positioning, map display, route planning and navigation
5. interrupts and exceptions
Station B crazy God notes
MySQL query timeout control
How to use clion to debug a project built by the make tool
QT读取SQLserver数据库
Questions about ETL: io trino. jdbc. TrinoDriver
The password does not take effect after redis is set
Database outline
P6154 wandering (memory search
How to solve the 404 problem
Redis learning journey master-slave replication