当前位置:网站首页>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 .
边栏推荐
- How to close an open DNS resolver
- Never use redis expired monitoring to implement scheduled tasks!
- Software engineering best practices - project requirements analysis
- Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions
- What does it mean to open an account online? Is it safe to open an account online?
- Is it safe to buy funds on the compass?
- Summer Challenge [FFH] harmonyos mobile phone remote control Dayu development board camera
- ABAQUS 2022 latest edition - perfect realistic simulation solution
- Why did kubernetes win? The changes in the container circle!
- LVM snapshot: preparation of backup based on LVM snapshot
猜你喜欢

Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance

When is it appropriate to replace a virtual machine with a virtual machine?

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

让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北

To tell you the truth, ThreadLocal is really not an advanced thing

One revolution, two forces and three links: the "carbon reduction" road map behind the industrial energy efficiency improvement action plan

New trend of embedded software development: Devops

HP notebook disable touchpad after mouse is inserted

New trends of China's national tide development in 2022

Maxpool2d explanation -- Application in arrays and images
随机推荐
网上开华泰证券的股票账户是否安全呢?
Makefile notes (Yiwen Institute makefile)
Arthas debugging problem determination Toolkit
[designmode] factory pattern
Shell multitasking to download video at the same time
Fund managers' corporate governance and risk management
LVM snapshot: backup based on LVM snapshot
Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
Ctfshow permission maintenance
ABAQUS 2022 software installation package and installation tutorial
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 database query optimization
Matlab saves triangulation results as STL files
五分钟搞懂探索式测试
Combining online and offline, VR panorama is a good way to transform furniture online!
Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
Never use redis expired monitoring to implement scheduled tasks!
Schéma de transition asynchrone - générateur
lvm-snapshot:基于LVM快照的备份之准备工作