Home  >  Article  >  Database  >  How to implement clustering in redis

How to implement clustering in redis

步履不停
步履不停Original
2019-06-22 16:07:562793browse

How to implement clustering in redis

1. Master-slave replication

Master-slave replication principle:

  • The slave server connects to the main server and sends the SYNC command;

  • After the main server receives the SYNC naming, it starts executing the BGSAVE command to generate the RDB file and uses the buffer to record all write commands executed thereafter. ;

  • After the master server BGSAVE is executed, it sends snapshot files to all slave servers and continues to record the executed write commands during the sending period;

  • After receiving the snapshot file from the server, discard all old data and load the received snapshot;

  • After the master server snapshot is sent, it starts sending the write command in the buffer to the slave server ;

  • The slave server completes loading the snapshot, starts receiving command requests, and executes write commands from the master server buffer; (Slave server initialization completed)

  • Every time the master server executes a write command, it will send the same write command to the slave server, and the slave server receives and executes the received write command (After the slave server is initialized Operation)

Advantages and disadvantages of master-slave replication:

Advantages:

  • Supports master-slave replication, the host will automatically synchronize data to the slave, and read and write can be separated

  • In order to offload the read operation pressure of the Master, the Slave server can The client provides read-only operation services, and the write service must still be completed by the Master

  • Slave can also accept connection and synchronization requests from other Slaves, which can effectively offload the Master's synchronization pressure.

  • Master Server provides services to Slaves in a non-blocking manner. So during Master-Slave synchronization, the client can still submit queries or modification requests.

  • Slave Server also completes data synchronization in a non-blocking manner. During synchronization, if a client submits a query request, Redis will return the data before synchronization

Disadvantages:

  • Redis does not have automatic fault tolerance and recovery functions. The downtime of the host and slave machines will cause some front-end read and write requests to fail. You need to wait for the machine to restart or manually switch the front-end IP to recover.

  • The host machine went down. Some data could not be synchronized to the slave machine in time before the machine went down. After switching the IP, data inconsistency would be introduced, which reduced the availability of the system.

  • Redis is difficult to support online expansion. When the cluster capacity reaches the upper limit, online expansion will become very complicated.

2.Sentinel Mode

When the master server interrupts service, a slave server can be upgraded to the master server to continue to provide services, but this process requires manual operation. To this end, Redis 2.8 provides the sentinel tool to implement automated system monitoring and fault recovery functions.

The role of the sentinel is to monitor the running status of the Redis system. Its functions include the following two.

(1) Monitor whether the master server and slave server are running normally.
(2) When the main server fails, it will automatically convert from the slave server to the main server.

How the sentinel works:

  • Each Sentinel process sends a message to the Master in the entire cluster once per second. Server, Slave sends a PING command from the server and other Sentinel processes.

  • If the time since the last valid reply to the PING command exceeds the value specified by the down-after-milliseconds option, the instance will be marked by the Sentinel process. Subjectively offline (SDOWN)

  • If a Master server is marked as subjectively offline (SDOWN), all Sentinel processes that are monitoring the Master server must be Confirm once per second that the Master server has indeed entered the subjective offline state

  • When there are a sufficient number of Sentinel processes (greater than or equal to the value specified in the configuration file) in the specified If it is confirmed that the Master server has entered the subjective offline state (SDOWN) within the time range, the Master server will be marked as objectively offline (ODOWN)

  • Under normal circumstances, each The Sentinel process will send INFO commands to all Master servers and Slave servers in the cluster once every 10 seconds.

  • When the Master server is marked as objectively offline (ODOWN) by the Sentinel process, the Sentinel process sends a message to all Slave slave servers of the offline Master server. The frequency of the INFO command will change from once every 10 seconds to once every second.

  • If there are not a sufficient number of Sentinel processes to agree to the Master server being offline, the objective offline status of the Master server will be removed. If the Master server sends the PING command to the Sentinel process again and returns a valid reply, the subjective offline status of the Master server will be removed.

Advantages and disadvantages of Sentinel Mode

Advantages:

  • ## Sentinel The mode is based on the master-slave mode, and the sentinel mode has all the advantages of the master-slave mode.

  • Master and slave can be automatically switched, making the system more robust and highly available.

Disadvantages:

  • Redis is difficult to support online expansion. When the cluster capacity reaches the upper limit, online expansion will become Very complicated.

3.Redis-ClusterCluster

The sentinel mode of redis can basically achieve high availability and read-write separation, but in In this mode, each redis server stores the same data, which is a waste of memory. Therefore, the cluster mode is added to redis3.0 to realize distributed storage of redis, which means that each redis node stores different content.

Redis-Cluster adopts a centerless structure. Its characteristics are as follows:

  • All redis nodes are interconnected with each other (PING-PONG mechanism), and the binary protocol is used internally to optimize transmission. Speed ​​and bandwidth.

  • The fail of a node takes effect only when more than half of the nodes in the cluster detect failures.

  • The client is directly connected to the redis node, without the need for an intermediate proxy layer. The client does not need to connect to all nodes in the cluster, just connect to any available node in the cluster.

Working method:

On each node of redis, there are two things, one is the slot, Its value range is: 0-16383. Another one is cluster, which can be understood as a cluster management plug-in. When our access key arrives, redis will obtain a result based on the crc16 algorithm, and then calculate the remainder of the result to 16384, so that each key will correspond to a hash slot numbered between 0-16383, through This value is used to find the node corresponding to the corresponding slot, and then automatically jumps directly to the corresponding node for access operations.

In order to ensure high availability, the redis-cluster cluster introduces the master-slave mode. One master node corresponds to one or more slave nodes. When the master node goes down, the slave nodes will be enabled. When other master nodes ping a master node A, if the communication between more than half of the master nodes and A times out, then the master node A is considered to be down. If both master node A and its slave node A1 are down, the cluster will no longer be able to provide services.

For more Redis-related technical articles, please visit the Redis Tutorial column to learn!

The above is the detailed content of How to implement clustering in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn