Redis introduction of master-slave replication (5)

Master-slave replication (Master/Slave)

1. What is it?

After the host data is updated, according to the configuration and strategy, it is automatically synchronized to the master/slaver mechanism of the standby machine.
Master is mainly writing, and slave is mainly reading.

2. What can you do?

​ Read and write separation, disaster recovery (reducing the pressure on reading and writing)

3. Commonly used master-slave method

1) One master and two servants

Meaning: One Master and two Slave
Insert picture description here
info replication: View master-slave information The
information is as follows:

role:master
connected_slaves:0
master_replid:f6baff9abfda12ca58048cfce4b0e2c1f4683da1
master_replid2:e8fe596d47d9d1d923d56d884b28128b78d2c1e0
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

Use:
configure the slave (library) but not the master (library). By default, everyone is the master of the master library. Set the following master library in the slave library
(as long as the master library is configured, everything in the master library will be copied, and Not just copy the operations after the main library configuration)

slaveof 主库ip 主库端口:配置从库
info replication:查看主从信息

Note:
The first entry point for slave1 and slave2 is full replication, followed by incremental replication (no copy will be copied before) the
master can write, but the slave cannot write, and the slave can only read the
Insert picture description here
master shutdow Then, the slave machine is in the standby state. After the master machine comes back, the new record of the master machine can be copied smoothly.

After the slave shutdow, you need to reconnect every time you disconnect from the master, unless you configure it into the redis.conf file

The data copied from the machine will be persisted by the machine. Even if shutdown is disconnected, there will still be data.

Reconnect or change the master, the previous data will be cleared, and the latest data will be recreated and copied

2) Passing on

Meaning: 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.
Insert picture description here
The precautions are similar to that of one master and two servants, but note that although the slave in the middle is the relative master, it is still the identity of the slave

3), anti-customer-oriented

slaveof no one:使当前数据库停止与其他数据库的同步,转成主数据库

Copy principle

Slave will send a sync command after successfully connecting to the master after starting;

After the master receives the command to start the save process, it also collects all the received commands for modifying the data set. After the background process is executed, the master will transmit the entire data file to the slave to complete a complete synchronization;

Full copy: The slave service saves the database file data and loads it into the memory;

Incremental replication: The Master continues to transmit all the newly collected modification commands to the slave in turn to complete the synchronization;

But as long as the master is reconnected, a complete synchronization (full replication) will be executed automatically.

Disadvantages of replication

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

Java

Jedis jedis_master = new Jedis("主机地址1",端口号1);
Jedis jedis_slave = new Jedis("主机地址1",端口号2);
jedis_salve.slaveof("主机地址1",端口号1); //认人为主
jedis_master.set("key","value"); //主机写完,从机即可读到
jedis_slave.get("key"); //从机就可读到

Guess you like

Origin blog.csdn.net/weixin_42272869/article/details/112795029