In order to make the cluster available even if some nodes fail or most nodes fail to communicate, the cluster uses a master-slave replication model, with n-1 replicas per node.
Redis clusters use a master-slave replication model to achieve data replication and high availability. The design of the master-slave replication model is mainly divided into two roles: master node and slave node.
Master node: The master node is a server in the cluster, and the master node is responsible for receiving write requests from clients and writing them to the local database, and at the same time sending these write operations to the slave node to implement data replication. Slave node: The slave node is a server in the cluster, and the slave node is responsible for receiving write commands sent by the master node and executing these commands, so as to achieve data synchronization, and when the master node goes down, the slave node can automatically take over the work of the master node to achieve high availability. The main process of master-slave replication model is as follows:
The master node receives the write request from the client, performs write operations and writes them to the local database, the master node sends these write operations to the slave node at the same time, the slave node performs these operations, and returns the execution results to the master node, the master node detects whether the slave node is down or delayed through the heartbeat mechanism, when the slave node is down or delayed, the master node marks the slave node as unavailable When the master node is down, the slave nodes in the cluster will elect a new master node, and the slave nodes will automatically switch to the new master node, so that high availability is achieved。 In summary, the master-slave replication model of a Redis cluster synchronizes writes to the slave node through the master node, and implements heartbeat monitoring and automatic switchover to achieve data replication and high availability.