Message Queuing - What is MQ? When to use MQ? How to choose MQ?

What is MQ?

MessageQueue: It is message + queue, task + queue, command + queue.
Function: How to communicate between applications (producer and consumer).

scenes to be used

从下面这个场景来感受MQ 的诞生
If we have a lot of tasks to process, the tasks will be sent continuously, and we need to process them. Without MQ, if we develop this function normally, we may have the following ideas:

  1. single thread
  2. Single thread + single queue
  3. Multi-threading + single queue
  4. Multi-threading + multi-queue
    Insert image description here
    From the above example, we can actually have the following thoughts:
    1) If we use queue queuing, is it true that the real-time performance of task processing is not that high? Is it actually asynchronous processing? So for example, there is an example of user registration. Automatically log in after registration, but this system is relatively complex. For example, it needs to send text messages to users to welcome them as members, or it may have a points system, or some coupons or other privileges for new users. If the system is large enough, the points system and coupon system will generally be used as an independent module. Do users need to care about these when registering this action? In fact, it is not necessary. First, only the core registration logic is completed, and these additional functions can be processed asynchronously. For example, we perform asynchronous processing on tasks such as initializing points and giving coupons, and send a message to the points system and coupon system respectively. Currently, We respond to the user immediately after completing the initialization of the user system's information. This provides a good experience for the user and can also improve the system's QPS.
    2) Based on the thinking of 1, we split it into different systems and send messages to different systems. At this time, MQ actually plays a communication role. So are these different systems decoupled? At the same time, is it possible to distribute data to different systems through the main system?

In fact, our above thinking is far from enough. For example, if the message is not persisted, will it disappear after the service is restarted? What should I do if an exception occurs during task processing? How to route to different queues? What if we have point-to-point processing or broadcasting? To deal with this demand. Then MQ is a solution to these problems. MQ considers more than us and provides solutions to different problems.

基于上面的一些简单的思考我们可以总结出MQ的应用场景:

  • Asynchronous: focuses on the processing process, transforming some previous synchronous logic into an asynchronous logical process.
  • Decoupling: Focusing on functional design, when doing some business architecture analysis, you can distinguish the main process and branch process in a strong and focused manner.
  • Peak clipping and current limiting: Focusing on the issue of orders of magnitude, it can withstand several times, even dozens, or hundreds of times more traffic than when it is not connected to MQ.
  • Delayed call (quasi-real-time, certain delay): Focusing on customized requirements, a choice was made between db and MQ.

MQ's choice

Rocketmq official website
Why choose RocketMQ has a comparison of various MQs.
Then we need to consider different dimensions when selecting. For these dimensions, you can refer to the picture below.
Picture source: https://time.geekbang.org/column/article/540810
Insert image description here

Guess you like

Origin blog.csdn.net/qq_43259860/article/details/135436420