Why are message queues so important?

Hello everyone, I'm [Architecture Ferryman], a ten-year programmer. This is the first article of the message queue. This series will share with you a lot of useful experience in practical work. If you have any gains, please share it with more friends.

I don't know if you have ever used Queue-related classes, such as ArrayBlockingQueue, DelayQueue and other queues. If you say that you have never used these when writing business code, it is actually normal, but you have actually used them indirectly.

For example, the thread pool, which everyone must have used, then imagine that if you keep dropping tasks into the thread pool, the rejection policy will be triggered when the task cannot be dropped. But the tasks in the early stage are all queued for execution, so where are these tasks temporarily stored? This temporary container is the Queue.

We can see whether Queue is used through the constructor of ThreadPoolExecutor. The code is as follows:

 public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
}
复制代码

It can be seen that the role of Queue is to store. But why people rarely use Queue in Java directly in business development, that is because business code involves data storage, and the data of Queue is all in memory and not persistent, so the scenarios that can be used are relatively limited .

Although these Queues are not used, we often use some open source message middleware, because these middleware support persistence and are more powerful, but in fact, they are all message queues and data temporary storage. container.

So why is message queue so important ? I think the main thing is that the system can be decoupled and processed asynchronously to improve throughput.

Decoupling between systems

Suppose you are doing an e-commerce business. After placing an order, you need to send a text message to the user, and you need to remove the data in the shopping cart. According to the normal logic, the interface of the shopping cart and the SMS service is called in the ordering logic, then it is equivalent to directly relying on the SMS and the shopping cart.

Suppose the shopping cart service hangs up, then calling the remove interface when placing an order will inevitably report an error. Should the error be handled or not handled? From a business point of view, this error must not affect the order, right, but this will cause the shopping cart data to still exist after the order is placed. Then it is necessary to process abnormal requests, such as recording a log, and then perform post-operation manually or program. This is troublesome and increases the complexity of the entire logic.

如果我们引入了消息队列,那这件事情就简单多了。直接将要处理的动作,通过消息的形式告诉消息队列,只要告诉就完了,然后就可以告诉用户下单成功了。短信服务和购物车服务会去消费消息队列的消息,然后去执行对应的业务逻辑。我在下图中对于投递消息用的是虚线,目的是告诉大家,这块就解耦了。

假设购物车服务有几分钟挂了,那么等它重启正常后就会继续消费消息,然后把对应的数据移除,这里是最终一致性。

异步处理,提升吞吐量

这里就不贴图了,大家一看上面那张图就知道了。当下单完成后,消息投递之后,就立马返回了。而投递消息会非常快,因为不涉及到业务逻辑的处理。所以我们通过消息的形式,将一些不需要立马获得返回值的业务场景,进行了异步处理。同时下单的响应时间变短了,吞吐量自然就上来了。

总结

消息队列之所以在面试过程中必问,也是因为它是我们工作中不可缺少的一款中间件。正因为它的重要性,我们才更要去学习,要了解它的功能特点和使用场景,这样才能在工作中去解决具体的问题。

原创:架构摆渡人(公众号ID:jiagoubaiduren),欢迎分享,转载请保留出处。

本文已收录至学习网站 cxytiandi.com/ ,里面有Spring Boot, Spring Cloud,分库分表,微服务,面试等相关内容。

Guess you like

Origin juejin.im/post/7087399340913721352