Interviewer: Why use MQ (message queue)??

1. Why use MQ (message queue)

In the interview, MQ is the knowledge point that the interviewer often asks, so why do we use MQ?

  1. asynchronous processing
  2. flow clipping
  3. application decoupling

以上是标准问答,那么你是否真的了解MQ该如何使用?Below I will tell you how to use MQ from the actual application scenarios.

Registration scene

I will directly analyze the real scene of our company . I will not take the unpopular business here (I am afraid that you will not understand me-.-), and find a function that most systems will have: registration .

Information Verification->Create an account and put it into the warehouse->Create a fund account->Notify other systems->SMS notification

image-20200905162324130

question

Let me talk about the problems caused by this linear registration method. The problem is that it is 线性true. If there is a problem with any link in the middle and the operation fails, it will result in a return failure.

The second is 响应速度慢, and it will increase with the increase of the system volume 越来越慢.

Optimization direction

First of all, it is necessary to clarify which step does not need to be processed immediately.

Create a fund account: This step does not have a compensation mechanism during our business operations, and processing can be delayed

Notify other systems: Three-party systems are involved in obtaining user information, and the real-time requirements are not so high. If users do not exist, they will ask our system to obtain them. The more connected systems, the slower the notification, and the processing can be delayed

SMS notification: Here is mainly to let the message service send SMS notifications to the user's mobile phone/email. The service itself also has a compensation mechanism, which can delay processing

The above three steps are steps that can be delayed. The real-time requirements are not very high, and all failures are handled without affecting the normal execution of the system and business.

final optimization process

image-20200905161809171

asynchronous processing

Now we can see that the yellow position has changed from the original 5 steps to the current two steps, and the response speed will be greatly improved. This is the benefit of asynchronous processing.

flow clipping

If there are too many registered users at the same time, MQ will also play a role in peak cutting. Because of the characteristics of the queue, the next message will only be processed when one message is processed.

application decoupling

Every function in the blue area is decoupled, such as SMS notification, when any function of any system, if you want to send a text message to the user, you only need to tell MQ

2. Conclusion

Through the above cases, we really understand what is asynchronous processing , traffic peak clipping , decoupling and why we should use MQ and the benefits it brings us. The next article will bring you the principle of RabbitMQ and how to use it in the code, so that everyone can feel at ease in interviews and work.

Guess you like

Origin blog.csdn.net/qq_21046665/article/details/108421701