Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


Redis provides a master-slave library mode to improve reliability. The master and slave libraries are usually separated from each other by reading and writing. The master library receives write operations and synchronizes data changes to the slave library. Both master and slave libraries can receive requests for degree operations. In a master-slave architecture, there is one master library and one or more slave libraries.

This paper mainly describes the principle of data synchronization between master and slave libraries.

Setting up a master-slave relationship is as simple as using replicaof on the instance that will serve as the slave and specifying the address and port number of the master.

Replicaof 192.168.1.1 6379Copy the code

After execution, a connection is established between the master and slave libraries. Will psync be sent from the library to? The command -1 indicates that the master database requests data synchronization. After receiving the command, the master database replicates data according to the parameters provided in the command.

This command takes two arguments:

  • The first parameter is the runID of the master library, which is an instance ID randomly generated with the start of the Redis instance. The runID of the master library is not known when the slave library first requests data synchronization from the master, so this value is?.
  • The second parameter is the replication progress offset, which is the value of the first request to replicate the data- 1.

When the master library receives the psync command, it will respond with the FULLRESYNC command, which represents the full copy of the current data. The runID of the master library and the current copy progress offset will be included in the response. The slave library will record the two messages when it receives them.

The master database fully copies the current data to the slave database in the form of RDB files. The RDB file is generated by executing the BGsave command and then sent to the slave library. Due to full synchronization, when the slave library receives the RDB file, it clears the current data and loads the received RDB file.

It takes some time for the RDB files to be generated from the primary database to complete the first data copy (full copy). During this time, the master database is also performing data operations that need to be synchronized to the slave database. During this time, all data operations performed by the master database are recorded in the replication Buffer, and all subsequent data operations by the master database are stored in the Replication Buffer.

After full replication is completed, a long connection is maintained between the master and slave libraries. Through this connection, the master library continuously synchronizes commands to the slave library, and data synchronization between the master and slave libraries is realized.