当前位置:网站首页>Redis - understand the master-slave replication mechanism
Redis - understand the master-slave replication mechanism
2022-07-01 00:05:00 【Salted fish_ Turn over】
List of articles
brief introduction
Redis Master-slave replication of , In fact, it is a Redis Server data , Copy to other Redis Server for . The former is called the master node of the server (master/leader), The latter is the slave node (slave/follower), Data can only be copied from master node to slave node ,Master Write as master ,slave Mainly reading . In fact, each server is a master node , A master node can have zero or more slave nodes , And each slave node can only have one master node . Master slave copy , Read / write separation , In most cases , Many people browse the web more for reading , This is a pattern often used in architecture , To relieve reading pressure :
The role of master-slave replication
1、 data redundancy : Master-slave replication realizes hot backup of data , It's a way of data redundancy other than persistence .
2、 Fault recovery : When the primary node fails , The slave node can temporarily replace the master node to provide services , It's a way of service redundancy
3、 Load balancing : On the basis of master-slave replication , Cooperate with the separation of reading and writing , Write by the master node , Read from node , Share the load on the server ; Especially in the context of more reading and less writing , Load sharing through multiple slave nodes , Increase concurrency .
4、 High availability cornerstone : Master slave replication is also the foundation for sentinels and clusters to implement .
Be careful :
take Redis Apply to the project , It will not be built with only one server , Because if it's a single Redis, Then a single server will complete the read and write operations from the client alone , Larger load , And there is only one Redis The server , It's too easy to hang up , It's troublesome if it goes down accidentally
Single server , There is an upper limit to the memory capacity , No matter this one Redis No matter how large the memory of the server , It is also impossible to completely use it to store memory , And single Redis Generally, the memory consumption of the server will not exceed 20 individual G.
Why cluster
1、 It is difficult for a single server to load a large number of requests .
2、 Single server failure rate is high , The probability of system collapse is high .
3、 The memory capacity of a single server is limited .
Cluster environment construction
View the current library information :info replication
Build configuration , Copy out three configuration files , modify 4 Messages
1、 Build master-slave replication , Different servers need different port numbers
2、pidfile File name for
3、logfile Log file name
4、Redis Default RDB Persistence , therefore dump.rdb The name of also needs to be modified 
edit vim redis79.conf The configuration file , Use as a host :

edit vim redis80.conf The configuration file , Used as a slave :



edit vim redis81.conf The configuration file , Used as a slave :


Start after modification , If the process exists , It means that the construction is completed .
One master and two subordinates
be-all Redis The server is a master node by default , When configuring , Generally, you only need to configure the slave .
To put it bluntly, it means to recognize the boss ! A master (79) Two from (80,81)
Use SLAVEOF host port You can configure the host for the slave .

Operate in the same way 6381 Also recognize the boss , Then the host will display :
Be careful :
actually , So the master-slave configuration in the command line , It's just temporary , If you really want to achieve permanent master-slave configuration , You need to configure... In the configuration file , The location of the configuration file is as follows , Remove the comment , Then change to the corresponding configuration :
Usage rule
The slave can only read , Can't write , The host is readable and writable, but mostly used for writing .
The setting value of the host , It will also be obtained by two slaves .

But as a slave, it cannot be written by default .
1、 If one master and two slaves are realized , The host accidentally hung up , Then the remaining slaves stand by before the master arrives , At this time, the main engine is down , It means that the write operation is lost . And after the host goes online again , Still maintain the original identity , The slave can still get the information written by the host . How can we create a new host when the host is down ? Mode one 、 The slave executes the command manually slaveof no one, After this execution, it will be independent from the opportunity and become a host . Mode two 、 Use sentry mode ( Automatic election )
2、 At this time, if you hang up from the computer , It means that the slave has been disconnected , When waiting to go online again , Will not connect to the original host , It becomes the host itself , But the original data still exists , If it reconnects , The newly added data in the host will also be shared to .
The principle of replication
If one Slave Specified a master Be the boss , that Master The node will send a message called sync The synchronization command of , In the use of the SLAVEOF After the command ,Master received , Will start the background save process , Start collecting commands for modifying datasets , After the execution of the save process , Data files will be sent to Slave, Achieve a full synchronization .
There are two ways to copy :
1、 Copy in full ,Slave The document is receiving Master After the data file sent , It will be saved and loaded into memory
2、 Incremental replication ,Master and Slave When synchronization has been achieved , If Master Continue modifying dataset , Will collect commands again , Send it to Slave
Be careful :
As long as it's reconnecting Master Server for , Full replication will be performed automatically after connection . We can see all the data of the host from the slave !
Link model
In fact, there are two models for this kind of replication , The above is a model ( Master slave copy model ), There is also a link model similar to , Linear relation :
Link model :
In the actual application scenario, there can be multiple nodes , Such nodes have Master and Slave The double identity of ( But in fact, it is still a Slave node , It's just a write operation Slave node ), Can share Redis The pressure of writing , And in real Master After the node hangs up , These people with dual identities can work on top , It effectively solves the unwritten problem of one master and two slaves .
边栏推荐
- Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
- leetcode 474. Ones and zeroes (medium)
- [designmode] factory pattern
- The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
- VR panorama adds contrast function to make the display of differentiation effect more intuitive!
- Error when starting PHP: [pool www] cannot get uid for user '@php_ fpm_ [email protected]’
- Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
- PS2 handle-1 "recommended collection"
- HP notebook disable touchpad after mouse is inserted
- Arthas debugging problem determination Toolkit
猜你喜欢

Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.

206页上海BIM技术应用与发展报告2021

VR panorama adds contrast function to make the display of differentiation effect more intuitive!

一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图

The programmer's girlfriend gave me a fatigue driving test

8253A寄存器浅析

shell 同时执行多任务下载视频

The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results

Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance
随机推荐
1175. Disposition des nombres premiers / échange de doigts II 104. Nombre de permutations
What is SRM system and how to standardize the internal procurement process of the company
Is it safe to open a stock account of Huatai Securities online?
Ctfshow framework reproduction
Schéma de transition asynchrone - générateur
Ctfshow permission maintenance
未来十年世界数字化与机器智能展望
Warmup preheating learning rate "suggestions collection"
ABAQUS 2022 software installation package and installation tutorial
Red hat will apply container load server on project atomic
What value should testers play in requirements review? Two minutes will stop you from being stupid
The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
基金管理人公司治理和风险管理
1175. 质数排列 / 剑指 Offer II 104. 排列的数目
Advanced mathematical modeling
让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
Red Hat将在Project Atomic上运用容器负载服务器
Solve arm_ release_ ver of this libmali is ‘g2p0-01eac0‘,rk_ so_ Ver is' 4 ', libgl1 mesa dev will not be installed, and there are unsatisfied dependencies
MySQL index test
New trends of China's national tide development in 2022