Why do you use MQ
advantage
- decoupling <br/>
The relevant capabilities of the same business are required by many projects , It is also very likely to be needed by other projects in the future . Decouple this part of the capability , adopt MQ Unified message processing . If there are other projects or businesses that need relevant functions , Subscribe to this , Handle related business . Reduce direct project dependencies .<br/> - asynchronous <br/>
Large scale project related collaborators , More and more teams are involved , The relationship between projects is getting deeper and deeper . The whole link is very long , And troubleshooting is not easy .<br/> - Peak shaving <br/>
For some businesses , At some point in time , It may be because the instantaneous request is too large , This causes all requests to access the database directly and puts pressure on the database , Thus affecting the original business of the project . commonly MySQL The processing capacity of the database is 2k Time /s,MQ Peak processing capacity 1w+. Can effectively deal with <br/>
<!-- more -->
shortcoming
Reduced system availability
Because of the new addition MQ, because MQ The usability of 、 Reliability imposes an additional burden on system availability .<br/>
- How to solve the usability problem <br/>
Different MQ The solution to this is different .
ActiveMQZookeeperAnd multipleactiveMQ, Change persistence to better performanceLevelDBReplace the defaultKahaDB.<br/>
advantage : Simple implementation of availability .<br/>
shortcoming : Each time actually available MQ only one , Once there is one MQ Problems arise , Relevant messages will be lost .<br/>
RocketMQ
- single master Pattern , When something goes wrong , The entire service will be unavailable . Generally, it will not be directly produced and used , Suitable for personal study ;
- many master Pattern , Multiple master Nodes form a cluster . Single master Problems do not affect . advantage : The highest performance of all modes . shortcoming : Single master During node downtime , Messages that are not consumed are not available until the node is restored , The real-time performance of messages is affected . Be careful : Using synchronous disk brushing can ensure that messages are not lost , meanwhile topic Corresponding queue It should be distributed among the centralized powers master node , Not just in one place master Node ;
- many master many slave Asynchronous replication mode , In a multiple master In mode , Every master Each node has at least one corresponding slave;<br/>
master Nodes are readable and writable ,slave Read only Don't write . advantage : stay master outage , Consumers can directly from slave Read message , The real-time performance of messages will not be affected , Performance is almost as good as many master equally . shortcoming : Using asynchronous replication may cause message loss problems . - many master many slave Synchronous double write , Similar to the previous mode , The difference lies in master And slave Data synchronization between . advantage : Synchronous double write synchronization mode can ensure that data is not lost . shortcoming : It takes a long time to send a single message , The performance will be poor ;
Synchronous brush set , The difference between asynchronous disk brushing : Lie Duo master Write data to disk mode synchronously .
| Synchronous replication | Asynchronous replication | |
|---|---|---|
| Concept | Immediate wait Master and Slave Only after all of them are successfully written, can they be fed back to the client | as long as Master Write a successful , Feedback the client to write the success status |
| reliability | High reliability , if Master Something goes wrong ,Slave There's all the backup data on the computer , Easy to recover | if Master Something goes wrong , There may be some data that hasn't been written yet Slave, May lose |
| efficiency | Because it is synchronous replication , It will increase the data write delay , Reduce system throughput | Because just write Master that will do , Therefore, the data writing delay is low , High throughput |
Recommendations in practical applications Master and Slave Set to ASYNC_FLUSH Asynchronous disk brushing mode , The master and slave are configured as SYNC_MASTER Synchronous replication method , So even if one of the machines fails , It can still guarantee that the data will not be lost .
- How to solve the reliability problem <br/>
introduce MQ Resulting in data reliability problems , Or how to deal with message loss ? Whole MQ In the link of , There are only three possible locations where messages may be lost , One is the production side , One is MQ Oneself , The last one is downstream consumers .<br/>
Different MQ This solution is different , It needs to be analyzed separately .
ActiveMQ
Production end
Every time the program is sent to MQ After the news , Can receive MQ Of its Ack Respond to ,
The complexity of the system increases
Added MQ, It introduces whether the message will be consumed repeatedly , Message loss issues .<br/>
Data consistency
introduce MQ after , A single transaction can be split into multiple transactions . The problem of distributed transaction consistency is introduced .<br/>
MQ How to deal with the problem of repeated consumption
Idempotent check
- The upstream cannot issue repeatedly <br/>
The program guarantees not to create messages repeatedly , Use business transactions for control .<br/> - Downstream cannot repeat consumption <br/>
- Put the message information into storage , Using a database 、Redis Store messages . A message is stored using a unique primary key , Check whether there exists each time , There is no continue processing , Otherwise, ignore or update according to the business ;<br/>
- Put the message information into storage , The unique primary key constraint is performed according to the business , If there are duplicate data , A unique primary key conflict will be reported , Can't insert ;
- If only redis Of set operation , There is no need to deal with . Even repetition does not affect ;








![[font multi line display ellipsis] and [dialog box] implementation ----- case explanation, executable code](/img/f8/9cfaafd0c013823e801beb9d4a946a.jpg)
