当前位置:网站首页>2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
2022-07-04 00:39:00 【Xianghan collection】
Sentinel mode

In master-slave mode , When the primary node goes down , The slave node can continue to provide services as the master node , But you need to change the address of the primary node of the application side , You also need to command all slaves to copy the new master , The whole process needs human intervention .
therefore , stay Redis 2.8 Version start , The sentry was introduced (Sentinel) The concept , stay The basis of master-slave replication On , The Sentinels achieved Automated fault recovery . As shown in the figure above , Sentinel mode consists of two parts , Sentinel nodes and data nodes :
- The sentinel node : Sentinel nodes are special Redis node , Don't store data ;
- Data nodes : Both master and slave nodes are data nodes .
Redis Sentinel It's monitoring in a distributed system Redis Master slave server , It also provides the mode of automatic fail over when the primary server is offline . Three of them are :
- monitor (Monitoring):Sentinel Constantly check whether your master and slave servers are working properly ;
- remind (Notification): When someone is being monitored Redis When there's a problem with the server , Sentinel Can pass API Send notifications to administrators or other applications ;
- Automatic failover (Automatic failover): When a primary server doesn't work , Sentinel An automatic failover operation will start .
Timing task
Sentinel Internal 3 Scheduled tasks , Namely :
- Every time 1 Every second Sentinel For others Sentinel and Redis Node execution
PINGoperation ( monitor ), This is a The heartbeat detection , Is the basis of failure judgment . - Every time 2 Every second Sentinel adopt Master Node channel Exchange information (Publish/Subscribe);
- Every time 10 Every second Sentinel Would be right Master and Slave perform INFO command , This task mainly achieves two purposes :
- Find out Slave node ;
- Confirm the master-slave relationship .
Subjective offline
Subjective offline (Subjectively Down, abbreviation SDOWN) single Sentinel Instance of the server to make a referral judgment , That a single Sentinel Consider a service offline ( It is possible that the subscription could not be received , Network failure and other reasons ).
Subjective offline means that if the server is within a given number of milliseconds , No return Sentinel Sent PING A reply to an order , Or return an error , that Sentinel This server will be marked as subjective offline (SDOWN).
Objective offline
Objective offline (Objectively Down, abbreviation ODOWN) multiple Sentinel The instance is made to the same server SDOWN Judge , And after communicating with each other by command , Get the server offline judgment , Then open failover.
Only in a sufficient number of Sentinel After marking a server as subjective offline , The server will be marked as offline (ODOWN). Only when Master When being identified as objective offline , Will fail over .
arbitration
Arbitration refers to... In the configuration file quorum Options . Some Sentinel First the Master The node is marked as subjective offline , And then the decision is passed sentinel is-master-down-by-addr Order to ask for other Sentinel Does the node also think it should addr Of Master Nodes should be subjective offline . Finally, when this consensus is reached Sentinel The number has reached the previous point quorum When setting the value of , The Master Nodes are identified as objective offline and fail over .
quorum The value of is generally set to Sentinel The number of Half plus 1, for example 3 individual Sentinel Set it to 2.
How sentinel mode works
- Every Sentinel At a rate of one second to what it knows Master,Slave And other things Sentinel The node sends a
PINGcommand ; - If an example (instance) Distance from the last valid reply PING The command took longer than the configuration file
own-after-millisecondsThe value specified by the option , Then this instance will be Sentinel Marked as Subjective offline ; - If one Master Marked as subjective offline , So we're monitoring this Master All of the Sentinel Confirm... At a rate of once per second Master Whether you really enter the subjective offline state ;
- When there is Enough Sentinel( Greater than or equal to the value specified in the configuration file ) stay Confirm within a specified time frame Master It has entered the subjective offline state , be Master Will be marked as Objective offline ;
- If Master be in ODOWN state , Then vote to automatically select a new master node . Point the remaining slave node to the new master node to continue data replication ;
- Under normal circumstances , Every Sentinel With every 10 Second frequency to all that it knows Master,Slave send out
INFOcommand ; When Master By Sentinel When marked as objective offline ,Sentinel To the offline Master All of the Slave send out INFO The frequency of the command will be from 10 Once per second to once per second ; - If there is not enough Sentinel agree! Master It's offline ,Master The objective offline status of will be removed . if Master Reorientation Sentinel Of PING Command returns a valid reply ,Master Will be removed .
advantage
- Sentinel mode is based on the master-slave model , All the advantages of master-slave , Sentinel mode has ;
- Master and slave can switch automatically , More robust system , Higher availability ;
- Sentinel Constantly check whether your master and slave servers are working properly . When someone is being monitored Redis When there's a problem with the server , Sentinel Can pass API Send notifications to administrators or other applications .
shortcoming
- It takes time for master-slave switching , Data will be lost ;
- It still doesn't solve the write pressure of the master node ;
- The write ability of the master node , Storage capacity is limited by a single machine ;
- Dynamic expansion is difficult and complicated , For clusters , When the capacity reaches the upper limit, online expansion becomes very complicated .
Cluster pattern
Suppose there are tens of millions of 、 Hundreds of millions of users visit at the same time Redis,QPS achieve 10 ten thousand +. These requests come here , stand-alone Redis Just hang up . The bottleneck of the system is Redis On the single machine problem , At this time, you can use the Master slave copy Solve the problem , To achieve high concurrency of the system .
In master-slave mode , When the primary node goes down , The slave node can continue to provide services as the master node , But you need to change the address of the primary node of the application side , You also need to command all slaves to copy the new master , The whole process needs human intervention . therefore , stay Redis 2.8 Version start , Introduced sentry (Sentinel) The concept , stay The basis of master-slave replication On , The Sentinels achieved Automated fault recovery .
In sentinel mode , The write ability of a single node , Storage capacity is limited by a single machine , Dynamic expansion is difficult and complicated . therefore ,Redis 3.0 Version officially launched Redis Cluster colony Pattern , Effectively solved Redis Distributed requirements .Redis Cluster Cluster mode has High availability 、 Extensibility 、 Distributed 、 Fault tolerance Other characteristics .

Redis Cluster Use a no center structure , Each node can save data And the entire cluster state , Each node is connected to all other nodes .Cluster It is generally composed of multiple nodes , The number of nodes shall be at least 6 To ensure a complete and highly available cluster , Three of them are the main nodes , Three are slave nodes . Three master nodes will allocate slots , Handle command requests from clients , The slave node can be used after the master node fails , Replace the primary node .
As shown in the figure above , The cluster contains 6 individual Redis node ,3 Lord 3 from , Respectively M1,M2,M3,S1,S2,S3. Except the master and the slave Redis Data replication between nodes , all Redis Between nodes Gossip Protocol to communicate , Exchange metadata information of maintenance node .
In summary, it is : Read requests are assigned to Slave node , Write requests are assigned to Master, Data synchronization from Master To Slave node .
Fragmentation
stand-alone 、 Master-slave 、 The Sentinel's mode data is stored in one node , Other nodes replicate data . There is an upper limit to the storage of a single node , Cluster mode is to carry out data processing Fragmentation Storage , When a piece of data reaches the upper limit , It can also be divided into multiple pieces .
Redis Cluster use Virtual hash slot partition , All keys map to by hash function 0 ~ 16383 Integer slot , Calculation formula :HASH_SLOT = CRC16(key) % 16384. Each node is responsible for maintaining a part of the slot and the key value data mapped by the slot .

Redis Cluster It provides a flexible solution for node expansion and reduction . Without affecting the external services of the cluster , You can add nodes to the cluster to expand capacity, or you can downsize some nodes offline . so to speak , Slot is Redis Cluster The basic unit of data management , Cluster scaling is the movement of slots and data between nodes .
The simple understanding is : After expansion or reduction , Slots need to be reallocated , Data also needs to be re migrated , But the service doesn't need to be offline .
If , Here you are 3 The cluster environment of nodes is as follows :
- node A Hash slot range is 0 ~ 5500;
- node B Hash slot range is 5501 ~ 11000;
- node C Hash slot range is 11001 ~ 16383.
here , If we want to store data , according to Redis Cluster Hash slot algorithm , Suppose it turns out to be : CRC16(key) % 16384 = 6782. Then you'll take this key Is allocated to B node . Now connect A、B、C Any node gets key, It will be calculated in this way , Finally through B Nodes get data .
If we add a new node D,Redis Cluster We will take a part from each node Slot To D On , Like this :
- node A Hash slot range is 1266 ~ 5500;
- node B Hash slot range is 6827 ~ 11000;
- node C Hash slot range is 12288 ~ 16383;
- node D Hash slot range is 0 ~ 1265,5501 ~ 6826,11001 ~ 12287
This feature allows you to easily add and remove nodes from the cluster . Again, if I want to delete nodes D, Just put the node D The hash slot is moved to other nodes , When the node is empty , It can be completely removed from the cluster .
A master-slave mode
Redis Cluster In order to ensure the high availability of data , Added master-slave mode , A master node corresponds to one or more slave nodes , The master node provides data access , Copy the master data backup from the node , When the master node hangs up , It will select one of the slave nodes of the master node to act as the master node , So as to ensure the high availability of the cluster .
Back to the example , Cluster has A、B、C Three master nodes , If this 3 None of the nodes has a corresponding slave node , If B Hang up , Then the cluster will not be able to continue , Because we no longer have a way for 5501 ~ 11000 The hash slot in scope provides services .
So when creating a cluster , Be sure to add the corresponding slave node for each master node . such as , The cluster contains the master node A、B、C, And slave nodes A1、B1、C1, So even B Hang up the system can also continue to work correctly .
because B1 Node belongs to B Child of a node , therefore Redis The cluster will choose B1 Node as the new master node , The cluster will continue to provide services correctly . When B After re opening , It becomes B1 The slave node . But notice , If node B and B1 Hang up at the same time ,Redis Cluster You can't continue to serve correctly .
advantage
- No central Architecture ;
- Extensibility , Data according to Slot Storage is distributed over multiple nodes , Data sharing between nodes , Nodes can be added or removed dynamically , Data distribution can be adjusted dynamically ;
- High availability , Some nodes are not available , The cluster is still available . By increasing the Slave Make a backup copy of the data .
- Realize automatic fault diagnosis failover, Nodes pass through gossip Protocol exchanges status information , By voting Slave To Master The role of .
shortcoming
- Data is replicated asynchronously , No guarantee Strong data consistency ;
- The cluster environment is complex , But based on Docker It will be relatively simple .
边栏推荐
- What does redis do? Redis often practices grammar every day
- 不得不会的Oracle数据库知识点(三)
- 不得不会的Oracle数据库知识点(四)
- Development and application of fcitx functional plug-ins
- Pytest unit test framework: simple and easy to use parameterization and multiple operation modes
- What insurance products should be bought for the elderly?
- [NLP] text classification still stays at Bert? Duality is too strong than learning framework
- HR disgusted interview behavior
- Test the influence of influent swacth on the electromagnetic coil of quartz meter
- Advanced C language - pointer 2 - knowledge points sorting
猜你喜欢
![[PHP basics] cookie basics, application case code and attack and defense](/img/7c/551b79fd5dd8a411de85c800c3a034.jpg)
[PHP basics] cookie basics, application case code and attack and defense

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

Cloud dial test helps Weidong cloud education to comprehensively improve the global user experience

功能:求5行5列矩阵的主、副对角线上元素之和。注意, 两条对角线相交的元素只加一次。例如:主函数中给出的矩阵的两条对角线的和为45。

A Kuan food rushed to the Shenzhen Stock Exchange: with annual sales of 1.1 billion, Hillhouse and Maotai CCB are shareholders
![[CSDN Q & A] experience and suggestions](/img/db/dff3173dda24ca5740729b54a81153.jpg)
[CSDN Q & A] experience and suggestions

Future源码一观-JUC系列

MySQL is installed as a Windows Service

Release and visualization of related data

Is it really possible that the monthly salary is 3K and the monthly salary is 15K?
随机推荐
Global and Chinese market of glossometer 2022-2028: Research Report on technology, participants, trends, market size and share
[dynamic programming] leetcode 53: maximum subarray sum
What does redis do? Redis often practices grammar every day
Recommendation of knowledge base management system
Is the account opening of Guoyuan securities really safe and reliable
Joint examination of six provinces 2017
Generic
2022 Software Test Engineer skill list, please check
The first training of wechat applet
网上的低佣金链接安全吗?招商证券怎么开户?
[C language] break and continue in switch statement
国元证券开户是真的安全可靠吗
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
The FISCO bcos console calls the contract and reports an error does not exist
Entropy and full connection layer
Development and application of fcitx functional plug-ins
From functional testing to automated testing, how did I successfully transform my salary to 15K +?
MySQL 8.0.12 error: error 2013 (HY000): lost connection to MySQL server during query
1214 print diamond
[leetcode] interview question 17.08 Circus tower