当前位置:网站首页>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

边栏推荐
猜你喜欢

华为专家自述:如何成为优秀的工程师

一个AI玩41个游戏,谷歌最新多游戏决策Transformer综合表现分是DQN的两倍

渗透学习-SQL注入篇-靶场篇-安全狗的安装与绕过实验(后续还会更新)
![[lvgl] [stage summary 1]](/img/39/c6e96ae7971a2350769ef5b5950f8f.png)
[lvgl] [stage summary 1]
![[lvgl layout] grid layout](/img/36/47f586f3dc1a114ed7775c4e190872.png)
[lvgl layout] grid layout

Introduction to pyqt5 - student management system

The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
![[lvgl (5)] label usage](/img/55/f25a510cf04caff7ee15e72360c3a1.png)
[lvgl (5)] label usage

Mac can't connect to local MySQL server through socket '/tmp/mysql Sock '(2) problem

Sparksql core usage, 220724,
随机推荐
渗透学习-SQL注入篇-靶场篇-安全狗的安装与绕过实验(后续还会更新)
在线问题反馈模块实战(十二):实现图片删除功能
Don't compare with anyone, just be yourself
[learning notes] see the difference between a+++a and a+a++ from the compilation
[learning notes] what happens when the URL is input into the page presentation?
[learning notes] Web page rendering process
(static, dynamic, file) three versions of address book
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
[learning notes] possible reasons and optimization methods for white screen on Web pages
Libevent and multithreading
5. Template cache. Drawing a square can only move within the triangle
Libevent multithreaded server + client source code
上传图片base64
Create WPF project
Mac can't connect to local MySQL server through socket '/tmp/mysql Sock '(2) problem
Redis 主从机制
2022-07-22 mysql/stonedb parallel hashjoin memory usage analysis
Geek planet ByteDance one stop data governance solution and platform architecture
Record the pits encountered in the deserialization of phpserializer tool class
UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结