preface

  • Redis is a cache database that is often used in projects. Knowing its configuration and design ideas helps us to use it more effectively and safely
  • This time, I will analyze the master-slave architecture mechanism of Redis.

Set up process

  • The best way to understand the master-slave architecture of Redis is to build your own master-slave architecture.
  • Let’s build a master-slave architecture of redis with one master and two slaves.

  • First download a stand-alone Redis server, then copy two copies of the redis-6380.conf and redis-6381.conf files, and change the following configuration from the slave node:
Port 6380 # replicaof 192.168.0.60 6379 # Configure the slave node to access the master node. Slave -read-only yes # Configure the slave node to read only.Copy the code
Port 6381 replicaof 192.168.0.60 6379 slave-read-only YesCopy the code
  • Conf, redis‐ Server Redis-6379.conf, redis‐ Server Redis-6380. conf, redis‐ Server Redis-6381.conf. Command to start the Redis primary/secondary service.
  • After the startup is successful, test whether adding data to the primary node synchronizes data from the secondary node.

  • Whether adding data to the secondary node succeeds.

  • See the figure above, after setting data to the master node, the slave node can get data. Set data to the secondary node will prompt read only.

Analyze the principle of master-slave consistency

  • When we configure a slave node for the master node, regardless of whether the slave is linked to the master for the first time, it will send a psync command to the master to request data replication.
  • After receiving the psync command, the master will persist the data in the background using bgSave to generate the latest RDB snapshot file. During this time, Redis will continue to receive requests from other clients. And cache these new data set requests in memory. After the persistence is complete, the master sends the RDB file to the slave. The slave persists the received data to generate an RDB file and loads it to the memory. The master then sends the latest data cached in the memory to the slave.
  • When the slave disconnects from the master again, the slave can automatically reconnect to the master. If the master receives multiple concurrent connection requests from the slaves, it will persist the file only once, instead of persisting the connection once and then sending the file to other slaves.

First full data replication for Redis master and slave:

Incremental copy to link again:

  • After the master and slave are disconnected and reconnected, data is generally replicated in full. After Redis 2.8, Redis supports incremental replication of data to slave.
  • The master creates a cache queue in memory to cache the latest data. The master and all its slaves maintain the offset and master process ID of the replicated data. So when the slave disconnects, the slave sends a command to the master to request incomplete replication, starting with the data subscript of the record. When the master’s process ID or slave data subscript is too old to be in the master’s cache queue, a full copy of the data is performed.

  • If there are many slaves, to alleviate the master/slave replication storm (multiple slaves copy master data at the same time, causing excessive pressure on the master), you can make the following architecture to synchronize data with some slaves

The last

  • Learn with an open mind and make progress together