Redis master-slave architecture construction

A master node, one or more slave nodes, the master node performs write operations, and the slave node performs data backup. You can also let the slave node provide write and read operations to reduce the pressure on the master node.

Redis master-slave architecture construction

1. Copy a new configuration file from src/redis.conf

cp redis.conf redis.conf_1

2. Modify the newly created file redis.conf_1 and configure the following information

port 6380                                  #修改端口号,代表本机另外一台机器
pidfile /var/run/redis_6380.pid            #把pid写入到该文件中
logfile "6380.log"                         #log的地址和文件
dir /home/allen/packages/redis-5.0.3/6380  #知道数据存放的目录

3. Modify the newly created file redis.conf_1 to configure master-slave replication information

replicaof 127.0.0.1 6380  #本机从6371那台机器上获取数据 5.0之后改成slaveof
replica-read-only yes     #配置从节点只读

4. Start the slave node and view

[allen@localhost redis-5.0.3]$ src/redis-server conf/redis.conf_1
[allen@localhost redis-5.0.3]$ ps -ef|grep redis
allen     27587      1  0 04:25 ?        00:00:09 src/redis-server 127.0.0.1:6379
allen     89254  29143  0 05:31 pts/5    00:00:00 src/redis-server 127.0.0.1:6380

5. Link the slave node -src/redis-cli -p 6380, and get the value set by the master node from the slave node

#从主节点设置值节点:
127.0.0.1:6379> set Allen "you are the best"
OK
127.0.0.1:6379> 

#从从节点获取值:
[allen@localhost redis-5.0.3]$ src/redis-cli -p 6380
127.0.0.1:6380> get Allen
"you are the best"
127.0.0.1:6380> 

The replication principle of master-slave architecture

These logics are implemented in C language on the server side of redis.

1. The slave node establishes a long socket connection with the master node and sends a psync command to the master node

2.1 Execute bgsave to generate the latest rdb snapshot file. (Do it on the server side, it has nothing to do with whether you open the rdb file locally)

2.2 Put the newly added data during the execution of 2.1 into the Repl buffer in the cache.

3. Send the rdb file to the slave node.

4. Clear the rdb file in the machine from the node, and load the new rdb file of the master node.

5. Send the command in 2.2 to the slave node.

6. The command received in replay5.

7. The master node continuously sends synchronization commands through the socket long connection.

Re-launch after the slave node is hung up-resuming upload

1. Found broken link

2. The master will cache the most recent data and the most recently executed command in a repl backlog buffer. The size of the following configuration file can be configured, the default is 1m.

repl-backlog-size 1m

3. Relink the master node

4. The slave node sends the brave psync command to the master node with the offset offset of the last disconnection

5. Find the offset through the offset and the buffer in 2 and send the command after the offset to the heavy node. If the slave node hangs for a long time, and the lost data is more than 1m, it will be directly copied in full.

 

There are too many slave nodes under one master node. When multiple slave nodes replicate data from the master node at the same time, it may cause too much pressure on the master node and a master-slave replication storm occurs. Therefore, the following architecture is generally adopted.

In the master-slave architecture, if the master node is down, it needs to be handled manually by the operation and maintenance personnel to rewrite the configuration of the master node.

Guess you like

Origin blog.csdn.net/pengweismile/article/details/112297814