当前位置:网站首页>Redis master-slave mechanism
Redis master-slave mechanism
2022-07-24 07:04:00 【Bustling.】
One 、 Cluster structure
A single node Redis There is an upper limit to the concurrency of , We should further improve Redis Concurrency capability , You need to build a master-slave cluster , Read and write separation .
The master-slave structure is as follows :

There are three nodes , A master node , Two slave nodes . Start in the same virtual machine 3 individual redis example , Simulate master-slave cluster :

Two 、 Steps to build
1、 Create directory :
Create three folders , The names are 7001、7002、7003:
# Get into /tmp Catalog
cd /tmp
# Create directory
mkdir 7001 7002 70032、 To write redis.conf The configuration file :
take redis-6.2.6/redis.conf Copy the files into three directories , Persistence mode is the default RDB Pattern . And modify the configuration file in each folder , Change the ports to 7001、7002、7003, take rdb Change the file storage location to your own directory :
# Turn on RDB
# save ""
save 3600 1
save 300 100
save 60 10000
# close AOF
appendonly nosed -i -e 's/6379/7001/g' -e 's/dir .\//dir \/tmp\/7001\//g' 7001/redis.conf
sed -i -e 's/6379/7002/g' -e 's/dir .\//dir \/tmp\/7002\//g' 7002/redis.conf
sed -i -e 's/6379/7003/g' -e 's/dir .\//dir \/tmp\/7003\//g' 7003/redis.conf3、 Modify the declaration in the configuration file IP:
The virtual machine itself has multiple IP, To avoid future chaos , We need to be in redis.conf Specify the binding of each instance in the file ip Information , The format is as follows :
# redis Instance declaration IP
replica-announce-ip 192.168.1.103# Do it one by one
sed -i '1a replica-announce-ip 192.168.150.101' 7001/redis.conf
sed -i '1a replica-announce-ip 192.168.150.101' 7002/redis.conf
sed -i '1a replica-announce-ip 192.168.150.101' 7003/redis.conf
# Or one click modification
printf '%s\n' 7001 7002 7003 | xargs -I{} -t sed -i '1a replica-announce-ip 192.168.150.101' {}/redis.conf3、 ... and 、 start-up
For the convenience of viewing the log , stay mobaxterm Open in 3 individual redis window , Respectively in tmp Start the command under the directory :
# The first 1 individual
redis-server 7001/redis.conf
# The first 2 individual
redis-server 7002/redis.conf
# The first 3 individual
redis-server 7003/redis.confAfter starting :

Four 、 Open the master-slave relationship
To configure the master-slave relationship, you can use replicaof perhaps slaveof(redis5.0 before ) command :
# slaveof <masterip> <masterport>
slaveof 192.168.1.103 7001temporary : Use redis-cli The client connects to redis service , perform slaveof command ( Failure after restart );
permanent : Add this command in redis.conf in .
In order to facilitate the demonstration, we use the temporary method .
adopt redis-cli Command connection 7002, Execute the following command :
# stay tmp Connection under directory 7002
redis-cli -p 7002
# perform slaveof
slaveof 192.168.1.103 7001adopt redis-cli Command connection 7003, Execute the following command :
# stay tmp Connection under directory 7003
redis-cli -p 7003
# perform slaveof
slaveof 192.168.1.103 7001Connect 7001, View the cluster status :
# Connect 7001
redis-cli -p 7001
# Check the status
info replicationresult :

5、 ... and 、 test

6、 ... and 、 principle
1、 Full amount of synchronization

master How to determine slave Is it the first time to synchronize data ? Two very important concepts will be used here :
Replication Id:
abbreviation replid, Is the tag of the dataset ,id Consistent means the same data set . every last master There's only one replid,slave Will inherit master Node replid;
offset:
Offset , As recorded in repl_baklog The data in increases gradually .slave When the synchronization is completed, the current synchronization will also be recorded offset. If slave Of offset Less than master Of offset, explain slave The data lag behind master, You need to update . therefore slave Do data synchronization , You have to go to master Declare your own replication id and offset,master To determine which data needs to be synchronized .
Full synchronous process :

2、 The incremental synchronization

among repl_baklog There is an upper limit on size , When full, the oldest data will be overwritten . If slave Disconnected for too long , The data that has not been backed up is overwritten , Cannot be based on log Do incremental synchronization , Only full synchronization can be performed again .
3、 Optimize
It can be optimized from the following aspects Redis Master-slave cluster :

4、 summary

边栏推荐
- Sparksql core usage, 220724,
- [lvgl layout] grid layout
- Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary
- Upload excel file
- Thinking of data analysis -- analyzing the retail industry as a whole -- an all-round and multifaceted detailed analysis
- Day (0~6) represents the starting position of the first day of each month, stop represents the number of days of each month, and there are two blank spaces between each day. Input different days and s
- vs2019配置运行open3d例子
- 2022-07-22 mysql/stonedb并行hashJoin内存占用分析
- Don't compare with anyone, just be yourself
- reflex
猜你喜欢

【方向盘】超爱的IDEA提效神器Save Actions,卸载了

重磅直播 | ORB-SLAM3系列代码讲解地图点(专题二)
![[C language] operator details (in-depth understanding + sorting and classification)](/img/0a/e726126f6a55590d5ae73c49757cdb.png)
[C language] operator details (in-depth understanding + sorting and classification)

Huawei experts' self statement: how to become an excellent engineer

Vs debugging

UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
![[lvgl] API functions for setting, changing and deleting styles of components](/img/55/f25a510cf04caff7ee15e72360c3a1.png)
[lvgl] API functions for setting, changing and deleting styles of components

metaRTC5.0实现君正的纯C的webrtc版IPC

reflex

STM32基于hal库的adc以DMA的多通道采样以及所遇问题解决
随机推荐
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
Redis special data type hyperloglog
OSS authorizes a single bucket permission
Don't compare with anyone, just be yourself
不去和谁比较,只需做好自己
永远不要迷失自我!
项目问题积累
在线问题反馈模块实战(十二):实现图片删除功能
Input some data and find the maximum output. (keyboard and file reading)
owasp top10 渗透测试
Redis 持久化
Design a function print to print the string. If only the string parameter s is passed, the string length is compared with 10, greater than 10, print the first 10 characters, less than 10, and output a
【学习笔记】从汇编看 a+++a与 a+a++的区别
Day (0~6) represents the starting position of the first day of each month, stop represents the number of days of each month, and there are two blank spaces between each day. Input different days and s
Vs debugging
Ge port: sgmii mode and SerDes mode
Penetration learning - SQL injection - shooting range - installation and bypass experiment of safety dog (it will be updated later)
【学习笔记】Web页面渲染的流程
STM32 external interrupt (register version)
Cmake notes