Redis learning-master-slave replication Master/slave

What is it

Jargon: This is what we call master-slave replication. After the host data is updated, it is automatically synchronized to the master/slaver mechanism of the standby machine according to the configuration and strategy. The master is mainly written, and the slave is mainly read.

What can you do

Read and write separation,
disaster recovery and recovery

How to play

• With slave (library) but not with master (library)
• Slave configuration command: slaveof master library IP master library port
  • Every time you disconnect from the master, you need to reconnect, unless you configure the redis.conf file (specific location: redis.conf search #### REPLICATION ####)
  • info replication
• Modify the configuration file details operation
  • Copy multiple redis.conf files, press'redis[port].conf' to rename
  • Open daemonize yes
  • pid file Name
  •Specify port
  •Log file name
  •Dump.rdb name

replication English [ˌreplɪ'keɪʃ(ə)n] US [ˌreplɪ'keɪʃ(ə)n] n.
(painting, etc.) replication; copy; repeat (experiment); (especially for the reply) answer

3 common tricks

One master and two slaves (one master, two slaves)

Init

Insert picture description here
Set the slave through slaveof 127.0.0.1 6379d.
At this time, one master
and two slaves are set. Set k4 v4 in the master, and get k4 in both slaves can be displayed.
1. If I set k1 and k2 before the slave, can the slave check the value? Yes
, the slave only receives it, and then it starts to finish. Some of the masters are all over again
. 2. At this time, set k6 v6 on the master. And two slaves also have set k6 v66. What happens at this time? First come first served, the latter covers, can the slave and the master execute the same command?
The master writes as the master, and the slave reads as the master.
At this time, the slave cannot write, and an exception is reported.
3. The host is dead? What are the circumstances from the opportunity. From the upper position? Stand by?
It's still a slave, stand by. Will not be in position, the leader is back, business as usual
4. Slave machine died? Can the slave function be recovered?
The slave machine dies and becomes the master when it recovers, because each time you disconnect from the master, you need to reconnect, unless you configure it into redis.conf. If you want to keep up with the big army, just use the slaveof command. Start from the beginning, and run the host computer from beginning to end.

Passing the torch

The core idea: decentralized the
previous slave can be the master of the next slave, and the slave can also receive connection and synchronization requests from other slaves. Then the slave acts as the next master in the chain, which can effectively reduce the writing pressure of the master (slave If you are a slave or a slave,
change the direction in the middle: it will clear the previous data and re-create the copy of the latest
slave of the new main library IP and the new main library port. For
example, 79 is the host of 80, and 80 is the host of 81. In 79, set k9 v9 can be queried between 80 and 81. Using info republication on the 80 host is a slave, but a slave is also connected.

Anti-customer

SLAVEOF no one
stops the current database from synchronizing with other databases and turns it into the main database. After the
master is hung up, the two slaves need to select a boss. At this time, use a command for 80, slave of no one, and let 80 be the leader. For 81, if slaveof 80 is used, 80-bit master is recognized, and the data of 80 is modified, 81 can also be queried. If the original host 79 returns, it becomes an independent entity at this time.

Copy principle

After the slave starts successfully and connects to the master, it will send a sync command. The
master receives the command to start the background save process, and collects all the received commands for modifying the data set. After the background process is executed, the master will transfer the entire data file to the slave , In order to complete a complete synchronization and
full replication: After the slave service receives the database file data, it saves it and loads it into the memory.
Incremental replication: The Master continues to pass all the new collected modification commands to the slave in turn to complete the synchronization.
However, as long as the master is reconnected, a complete synchronization (full replication) will be automatically executed. The
first full replication is followed by incremental copy.

Sentinel mode (sentinel)

A group of sentinels can monitor multiple masters at the same time (patrol guards)

What is it

Anti-customer-oriented automatic version, which can monitor whether the host is faulty in the background. If it fails, it will automatically switch from the library to the main library according to the number of votes

How to play (use steps)

 1. Adjust the structure, 6379 with 6380, 6381
 2. Create a new sentinel.conf file, the name must not be wrong
 3. Configure the sentinel, fill in the content
   1. The name of the monitored database of the sentinel monitor (name it yourself) 127.0.0.1 6379 1
   2. The last number 1 above means that after the host hangs up, Salve will vote to see who will take over as the host, and the number of votes will become the host (PS. is different from the description on the official website, there is an official document below)
 4. Start the sentry
   1. redis- sentinel /sentinel.conf (The above directories are configured according to their actual conditions, and the directories may be different)
 5. Normal master-slave demonstration
 6. The original master is down
 7. Vote for a new election
 8. Restart the master-slave and continue to work, check info replication

Question: If the previously hung master restarts, will there be a double-master conflict?
Answer: No, the original master becomes a slave

Disadvantages of replication

Replication delay

Since all write operations are performed on the Master first, and then synchronized to the slave, there is a certain delay in synchronizing from the Master to the Slave machine. When the system is very busy, the delay problem will be more serious, and the number of Slave machines will increase. It will also make this problem more serious.

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/111402970