For the same reason as MySQL master-slave replication, although Redis is very fast in reading and writing, it can also cause extremely high reading pressure. In order to share the reading pressure, Redis supports master-slave replication. The master-slave structure of Redis can use one master and multiple slaves or a cascade structure. The following figure shows the cascade structure. (Recommended learning: Redis Video Tutorial)
Redis master-slave replication can be divided into full synchronization and incremental synchronization depending on whether it is full or not.
1 Full synchronization
Redis full replication generally occurs during the Slave initialization phase. At this time, the Slave needs to transfer all the data on the Master. Make a copy of both. The specific steps are as follows:
1) The slave server connects to the main server and sends the SYNC command;
2) After the main server receives the SYNC naming, it starts executing the BGSAVE command to generate the RDB file and use the buffer record All write commands executed thereafter;
3) After the master server BGSAVE is executed, snapshot files are sent to all slave servers, and the executed write commands continue to be recorded during the sending period;
4) After receiving the snapshot file, the slave server discards all old data and loads the received snapshot;
5) After the master server snapshot is sent, it starts sending the write command in the buffer to the slave server;
6) The slave server completes loading the snapshot, starts receiving command requests, and executes write commands from the master server buffer;
After completing the above steps, the slave server data initialization is completed For all operations, the slave server can now receive read requests from users.
2 Incremental Synchronization
Redis incremental replication refers to the process of synchronizing the write operations that occur on the master server to the slave server when the Slave is initialized and starts working normally.
The process of incremental replication is mainly that 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.
3 Redis master-slave synchronization strategy
When the master-slave is just connected, full synchronization is performed; after full synchronization is completed, incremental synchronization is performed. Of course, if necessary, the slave can initiate full synchronization at any time. The redis strategy is that, no matter what, incremental synchronization will be attempted first, and if unsuccessful, the slave machine will be required to perform full synchronization.
For more Redis related technical articles, please visit the Redis Getting Started Tutorial column to learn!
The above is the detailed content of How to synchronize master-slave in redis. For more information, please follow other related articles on the PHP Chinese website!