Home  >  Article  >  Database  >  How to synchronize redis cluster data

How to synchronize redis cluster data

(*-*)浩
(*-*)浩Original
2019-06-17 13:14:299187browse

Redis does not have the concept of copying locations like mysql, so when the Slave and the Master are disconnected and reconnected, a full snapshot of the Master will be taken, all the data of the Slave will be cleared, and the entire memory table will be re-established, which will cause the Salve to restore the data. It is extremely slow, and it also puts pressure on the Master.

How to synchronize redis cluster data

The master-slave replication strategy of Redis is implemented through its persistent rdb file. The process is to first dump the rdb file and then transfer the entire rdb file to the slave. Then synchronize the dumped operations to the slave in real time. Make the slave server an exact replica of the master server. The following features are mentioned in the official document ReplicationHowto: (Recommended learning: Redis Video Tutorial)

A master supports multiple slaves, and the slave can accept connections from other slaves and serve as the master of other slaves. The multi-level structure replication function thus forming a master-slave will not block the master server: even if one or more slave servers are undergoing initial synchronization, the master server can continue to process command requests. The replication function does not block the slave server: as long as the corresponding settings are made in the redis.conf file, the server can use the old version of the data set to process command queries even if the slave server is undergoing an initial synchronization. However, connection requests are blocked while the old version of the data set is removed from the server and the new version of the data set is loaded. Replication is used to provide scalability. For example, the slave can be used for data redundancy. Time-consuming commands (such as sort) can also be sent to certain slaves to avoid master blocking. In addition, slaves can also be used for persistence. , the slave server performs the persistence operation, which only requires commenting out the save directive in the master's configuration file.

Redis uses asynchronous replication.

The master-slave replication of Redis is divided into two stages:

1) Synchronization operation: update the database status of the slave server to the current database status of the master server .

2) Command propagation: When the database status of the master server is modified, causing the database status of the master and slave servers to be inconsistent, return the master and slave servers to a consistent state.

Synchronization

When the client sends the SLAVEOF command to the slave server and requires the slave server to copy the master server, the slave server first needs to perform a synchronization operation, that is, The database status of the slave server is updated to the current database status of the master server.

The synchronization operation of the slave server to the master server needs to be completed by sending the SYNC command to the master server. The following are the execution steps of the SYNC command:

Slave server Send a SYNC command to the master server. The master server that receives the SYNC command executes the BGSAVE command, generates an RDB file in the background, and uses a buffer to record all write commands executed from now on. When the master server's BGSAVE command is executed, the master server will send the RDB file generated by the BGSAVE command to the slave server. The slave server will receive and load the RDB file, and update its own database status to the database status when the master server executed the BGSAVE command. . The master server sends all the write commands recorded in the buffer to the slave server. The slave server executes these write commands and updates its own database status to the current status of the master server database.

For more Redis-related technical articles, please visit the Redis database usage tutorial column to learn!

The above is the detailed content of How to synchronize redis cluster data. 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