One 、 Preface
Now most of them use MQ To make the system heterogeneous , To decouple the system , The module of the system is equivalent to the sender and receiver ,MQ It plays the role of post office . As a transit role , You need to make sure that the message 100% The delivery .
Today we're going to study how to make sure that the news 100% Delivery of . Put together a copy of Java Interview book full version PDF
Two 、 Let's talk about RabbitMQ Characteristics of
RabbitMQ The assurance is ： As long as you deliver the message to Broker in , Then I'll make sure that the message reaches the consumers .
Of course, there are preconditions , such as ：
You need to respond manually ,
Minimum Broker Don't hang up , And the message is persisted .
combination RabbitMQ To make an analysis , For the delivery end , We just need to make sure that the message is sent to Broker Then you can , So how to ensure reliability , In the following steps ：
The message was successfully sent out
Guarantee Broker Successfully received the news
The producer received Broker The confirmation response
Message compensation mechanism , All three steps are kneeling , Make a compensation retransmission mechanism
The last barrier , If there are too many retries in step 4 , So there's something wrong with the system , We need human intervention
Just do the five steps above , Basically we can guarantee , Message delivery 100% Send it out . in addition , Official account Java Technology stack , Reply in the background ： interview , I can get the RabbitMQ A series of interview questions and answers , Very complete .
3、 ... and 、 The guarantee of the reliability of the producer's delivery
3.1 Let's start with the diagram
3.2 As for the above illustration , Let's step by step analyze
step1： Data in the database , This step is necessary
step2： Put the message in the library , And the initialization state is 0 （ In sending ）
step3： Deliver the message to Broker in
step4：Broker Send successful response
step5： The producer gets a successful response , Modify message status to 1:（ Send successfully ） All of the above are normal processes , Let's talk about the abnormal solution mechanism ：
step6： Periodically check whether the status of the message is 1
step7： If step6 The status of the message for is still 0 , Then go to resend , Repeat the above step1 - step5
step8： If the message is retransmitted a certain number of times , Then manual access processing , Because this may be a message
The above scheme seems to be perfect , But think about it , If in step4 in Broker In the process of sending response , If the message fails to reach the producer, the whole process will enter the compensation process , here Broker There are two messages , That is to say, the delivery becomes the problem of repeated delivery , So next we have to deal with this issue at the consumer end .
Four 、 The idempotent of the consumer side ：
4.1 The reason why idempotent needs to be solved
Broker When sending a reply message , The message did not reach the producer
When the consumer sends the response , The consumer hangs up
4.2 As for the solution of the above mechanism
Because all my messages above have a unique logo , So we just need to find the corresponding ID of the corresponding message to judge its status .
4.3 Build a unique identity scheme
First of all, different scenarios are used for different schemes , Don't talk about the hundreds of thousands of concurrency as soon as you come up , Take your time , Step by step .
4.3.1 Using database auto increment id
1)： It can't be simpler , It is acceptable when the concurrency is not large
2)： It's helpful for paging and sorting
1)： It is not applicable in the case of separation of sub database, sub table and read-write, and multiple slaves
2)： When the performance fails to meet the requirements , Not conducive to expansion
3)： The syntax implementation of different databases is different
4.3.2 utilize redis To generate id
1)：redis Single thread can generate global uniqueness ID
2)： have access to redis The cluster gets higher throughput
1)： Introduce new components , Increase system complexity
2)： You need to pay attention to dealing with concurrency
4.3.3 utilize twitter Open source snowflake
snowflake yes twitter Open source, a distributed ID Generation algorithm of , The result is a long Of ID, Use 41bit As milliseconds ,10bit As a machine ID（5 individual bit It's the data center ,5 individual bit It's a machine ID） 12 bit As a serial number in milliseconds （ Each node can generate... Every millisecond 2^12 = 4096 individual ID） The last one is the sign bit .
Don't rely on databases and other middleware , And the performance is good
It depends on time , If the weight loss of each machine is not synchronized, the discomfort will increase globally . Put together a copy of Java Interview book full version PDF