What is master-slave replication

The master/slaver mechanism automatically synchronizes data updates from the master library to the slave library (the backup machine) based on the configuration and policies. Master (the master library) writes data and slave (the slave library) reads data.

Master/slave replication

  • Read/write separation: The master is mainly written, and the slave is mainly read.
  • Disaster recovery: as the data of slaves is a copy of Master, whether master or Slaves break down, the data can be effectively recovered.

Environment to prepare

  1. Copies of the threeredis.confConfig files and rename them respectively toredis6379.conf.redis6380.confandredis6381.conf

2. Configure all three configuration filesdaemonize yes3. Set the ports toport 6379.port 6380andport 63814. Set pid file paths topidfile /var/run/redis_6379.pid.pidfile /var/run/redis_6380.pidandpidfile /var/run/redis_6381.pid5. Set log names tologfile "6379.log".logfile "6380.log"andlogfile "6381.log"6. Set db file names todbfilename dump6379.rdb.dbfilename dump6380.rdbanddbfilename dump6381.rdb

Of course, none of the above configuration is necessary, just to demonstrate better differentiation on the same virtual machine

usingredis6379.conf.redis6380.confandredis6381.confConfiguration file to start three different Redis services and passps -ef|grep redisCommand to view service status.Connect to the Redis service using three command line Windows.For example, run the redis service on port 79info replicationView primary and secondary replication information.

# Replication
#master indicates the master library, slave indicates the slave library
role:master
# Number of slave libraries connected to the master library
connected_slaves:0
master_replid:07e5ec5474ccf8c60e1471309b47e2592f38aeb1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

Copy the code

Master/slave replication mode

  1. One Master and two Slaves: one master and two or more slaves

Assume that port 79 serves as the master library and the others are secondary libraries.79 The service creates several pieces of data respectively, as follows:The commandslaveof <host> <port>The redis service with the host address port as the master library and itself as the slave library. Assume that both 80 and 81 have 79 as the main library, as follows:

After 80 and 81 use 79 as the master database, the data of master database 79 is copied to the slave database. Since the data of master database 79 is added first, and the data of master database 79 is added to the master database after 80 and 81, it can be seen that the replication is full when the connection between the slave database and the master database (the data of the master database is added later, and the slave database is incremental replication).

For example, run info replication to view the master/slave replication information of secondary library 80. You can run this command to view the master/slave replication information of the master database.

# Replication
The current state is slave
role:slave
Primary library IP address
Master_host: 127.0.0.1
Primary library port
master_port:6379
# Indicates the connection status with the main library. Up indicates the connection, and Down indicates the connection
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:882
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:02c33749f6cd663e01bf7442a4a80e03d25a0cda
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:882
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:393
repl_backlog_histlen:490

Copy the code

The master library can write, the slave library can only read, namely: master write from read, read and write separation

If throughshutdownMaster library 79 is down. Question: Are slave libraries 80 and 81 standing by waiting to reconnect with the master library or are slave libraries turned from slave to master? After the primary library is reconnected.

Conclusion: After the master library is down, the slave library is waiting for reconnection with the master library, but the connection status between the slave library and the master library changes from up to Down.

Take slave library 80 as an example. After the slave library is down and reconnected, the question is as follows: Is slave library 80 still the slave library of master library 79, or is it disconnected from master library 79 and becomes the master library?

Conclusion: The secondary library is disconnected from the original master library and reconnected after the breakdown, but the data of the original master library is saved.

Note: the slaveof command determines that the master/slave relationship is only temporary. The slave library is disconnected upon restart.

Suppose that 80 is the master, after 80 becomes the master of 79, the original data of the master library 80 is merged with the master library 79, or the master library 79 completely overwrites the data of the master library 80?

Answer: The data from the slave library is completely overwritten by the master library. Students can try it on their own.

  1. New generation

80 is the slave of 79 and has a data copy of 79. At the same time, 80 is the master of 81, and 81 has a data copy of 80 (also a copy of 79). Data is transmitted from 79 to 80, and from 80 to 81. 80 is both master and slave.The master of 80 is 79, the master of 81 is 80, and 80 is both master and slaveinfo replicationThe following is the information about 80:Because roles can only be displayedmasterorslaveSo 80 ismaster&slaveIn this case, an error occurs when 80 runs the set command.

Run the commands in the sequence shown in the figure. The data is synchronized from 79 to 80, and then from 80 to 81. The theoretical analysis of several cases when the master or slave library is down is the same as the case of one master and two slave, which will not be described here.

  1. Going to

As can be known from one master and two slaves, when master breaks down, slaves are on standby for master recovery, but not without a master for a day. When the master is down, one of the slave libraries is selected as the master, and the other slave libraries take it as the new master.

Slaveof no one: Makes the current database subordinate to other databases, making it the primary database.

– Slaveof no one disconnects 80 from 79 when 79 goes down, slaveof 127.0.0.1 6380 makes 81’s new master 80.

After 79 is reconnected, view the master/slave replication information of 79 and 80 as follows:

It can be seen that even after 79 reconnected, jiang Shan had already changed hands.

  1. Sentinel mode (Sentinel)

When the master library is down, you need to manually select a new master and modify the master library direction of the slave. The sentry mode can monitor the master library in the background if there is a failure, then automatically select the new master library from the slave library according to the vote, and other slave libraries take the new master library as master.

The difference is that when the old master reconnects, the old master also becomes the slave of the new master.

Sentinel Monitor Monitored database Name (custom) Monitored database IP address Monitored database port Number Number of votes, which indicates that when the monitored database fails, the database automatically votes from the slave database. When the slave database reaches the specified number of votes, the slave database becomes the new master database of other slave databases.

newsentinel.confFile.The editorsentinel.confFile, writeSentinel Monitor HOST6479 127.0.0.1 6379 1.

Start database 79, 80 and 81 respectively, 79 is 80 and 81 respectively (one master and two slave)

New Windowsredis-sentinel /myredis/sentinel.conf Start the Redis monitoring service in sentinel mode.

The logs show that the slave libraries of 79 are 80 and 81 respectively.

shutdownAfter demo 79 goes down, the log shows that the background is voting to select a new master library from the slave library. After the vote the new master is 81.

After 79 reconnects to the service, the owner changes and the rights change. 79 becomes the slave library of 81.

Principle of replication

  1. After connecting to the master successfully, the slave sends a sync command.

  2. The master collects all the commands it receives to modify the data set. After the background process is complete, the master sends the entire data file to the slave for a full synchronization (full replication).

  3. Full replication: After receiving database file data, the slave service saves it to the disk and loads it to the memory.

  4. Incremental replication: The master continues to send all the new collected changes to the slaves in turn to complete the synchronization, but a full synchronization (full replication) is performed automatically whenever the master is reconnected.

Disadvantages of copying

As all the operations are operated on the master now and then synchronously updated to Slaves, there is a certain delay in synchronizing the machine from the Master to slaves. The problem of delay becomes more serious when the system is busy and the increase in the number of slaves also makes the problem more serious. SlaveB, slaveB, slaveC, slaveC… . The next slave can be synchronized only after the previous slave is synchronized, which further extends the synchronization time.