You Probably Don’t Need a Message Queue

原文地址

I’m a minimalist, and I don’t like to complicate software too early and unnecessarily. And adding components to a software system is one of the things that adds a significant amount of complexity. So let’s talk about message queues.

Message Queues are systems that let you have fault-tolerant, distributed, decoupled, etc, etc. architecture. That sounds good on paper.

Message queues may fit in several use-cases in your application. You can check this nice article about the benefits of MQs of what some use-cases might be. But don’t be hasty in picking an MQ because “decoupling is good”, for example. Let’s use an example – you want your email sending to be decoupled from your order processing. So you post a message to a message queue, then the email processing system picks it up and sends the emails. How would you do that in a monolithic, single classpath application? Just make your order processing service depend on an email service, and call sendEmail(..) rather than sendToMQ(emailMessage). If you use MQ, you define a message format to be recognized by the two systems; if you don’t use an MQ you define a method signature. What is the practical difference? Not much, if any.

But then you probably want to be able to add another consumer that does additional thing with a given message? And that might happen indeed, it’s just not for the regular project out there. And even if it is, it’s not worth it, compared to adding just another method call. Coupled – yes. But not inconveniently coupled.

What if you want to handle spikes? Message queues give you the ability to put requests in a persistent queue and process all of them. And that is a very useful feature, but again it’s limited based on several factors – are your requests processed in the UI background, or require immediate response? The servlet container thread pool can be used as sort-of queue – response will be served eventually, but the user will have to wait (if the thread acquisition timeout is too small, requests will be dropped, though). Or you can use an in-memory queue for the heavier requests (that are handled in the UI background). And note that by default your MQ might not be highly-availably. E.g. if an MQ node dies, you lose messages. So that’s not a benefit over an in-memory queue in your application node.

Which leads us to asynchronous processing – this is indeed a useful feature. You don’t want to do some heavy computation while the user is waiting. But you can use an in-memory queue, or simply start a new thread (a-la spring’s @Async annotation). Here comes another aspect – does it matter if a message is lost? If you application node, processing the request, dies, can you recover? You’ll be surprised how often it doesn’t actually matter, and you can function properly without guaranteeing all messages are processed. So, just asynchronously handling heavier invocations might work well.

Even if you can’t afford to lose messages, the use-case when a message is put into a queue in order for another component to process it, there’s still a simple solution – the database. You put a row with a processed=false flag in the database. A scheduled job runs, picks all unprocessed ones and processes them asynchronously. Then, when processing is finished, set the flag to true. I’ve used this approach a number of times, including large production systems, and it works pretty well.

And you can still scale your application nodes endlessly, as long as you don’t have any persistent state in them. Regardless of whether you are using an MQ or not. (Temporary in-memory processing queues are not persistent state).

Why I’m trying to give alternatives to common usages of message queues? Because if chosen for the wrong reason, an MQ can be a burden. They are not as easy to use as it sounds. First, there’s a learning curve. Generally, the more separate integrated components you have, the more problems may arise. Then there’s setup and configuration. E.g. when the MQ has to run in a cluster, in multiple data centers (for HA), that becomes complex. High availability itself is not trivial – it’s not normally turned on by default. And how does your application node connect to the MQ? Via a refreshing connection pool, using a short-lived DNS record, via a load balancer? Then your queues have tons of configurations – what’s their size, what’s their behaviour (should consumers explicitly acknowledge receipt, should they explicitly acknowledge failure to process messages, should multiple consumers get the same message or not, should messages have TTL, etc.). Then there’s the network and message transfer overhead – especially given that people often choose JSON or XML for transferring messages. If you overuse your MQ, then it adds latency to your system. And last, but not least – it’s harder to track the program flow when analyzing problems. You can’t just see the “call hierarchy” in your IDE, because once you send a message to the MQ, you need to go and find where it is handled. And that’s not always as trivial as it sounds. You see, it adds a lot of complexity and things to take care of.

Certainly MQs are very useful in some contexts. I’ve been using them in projects where they were really a good fit – e.g. we couldn’t afford to lose messages and we needed fast processing (so pinging the database wasn’t an option). I’ve also seen it being used in non-trivial scenarios, where we are using to for consuming messages on a single application node, regardless which node posts the message (pub/sub). And you can also check this stackoverflow question. And maybe you really need to have multiple languages communicate (but don’t want an ESB), or maybe your flow is getting so complex, that adding a new method call instead of a new message consumer is an overkill.

So all I’m trying to say here is the trite truism “you should use the right tool for the job”. Don’t pick a message queue if you haven’t identified a real use for it that can’t be easily handled in a different, easier to setup and maintain manner. And don’t start with an MQ “just in case” – add it whenever you realize the actual need for it. Because probably, in the regular project out there, a message queue is not needed.

You Probably Don’t Need a Message Queue,布布扣,bubuko.com

时间: 2024-10-09 04:29:22

You Probably Don’t Need a Message Queue的相关文章

HDU 1509 Windows Message Queue

题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the qu

HDU 1509:Windows Message Queue【优先队列】

Windows Message Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4505    Accepted Submission(s): 1794 Problem Description Message queue is the basic fundamental of windows system. For each

Message Queue基本使用说明

一.安装Message Queue: 在Win7之前,控制面板,添加删除组件(Windows Message Queue). Win7~Win8:控制面板,程序和功能,启用或关闭Windows功能(找到Windows Message Queue服务器)选项,连同所有子类一并勾上即可,自动安装. 二.使用Message Queue: 1)用于各类服务器.计算机之间的通讯: 本地,自己给自己发(直接是.\\Private$\\Queue的私有名字). 远程计算机: FormatName:Direct

在单线程模型中 Message、Handler、Message Queue、Looper 之间的关系

Message,信息的载体,用来传递数据给Handler. Handler (Handler处理者,是 Message 的主要处理者,负责 Message 的发送,Message 内容的执行处理)发送和处理Message和Runable对象,这些对象和一个线程的MessageQueue相关联.每一个线程实例和一个单独的线程以及该线程的 MessageQueue 相关联.Handler和创建它的线程绑定在一起,把 Message和Runable 对象传递给 MessageQueue,这些对象离开

ZOJ2724_Windows Message Queue(STL/优先队列)

解题报告 题意: 看输入输出就很明白. 思路: 优先队列. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; struct node

hdu1509(Windows Message Queue) 优先队列

点击打开链接 Problem Description Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the

【排序,先入先出】Windows Message Queue

Windows Message Queue Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 73  Solved: 41 [Submit][Status][Discuss] Description Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something h

Windows Message Queue

D - Windows Message Queue Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 2724 Description Message queue is the basic fundamental of windows system. For each process, the system maintains a messag

Message、Handler、Message Queue、Looper 之间的关系

单线程模型中Message.Handler.Message Queue.Looper之间的关系 1.Message Message即为消息,可以理解为线程间交流的信息.处理数据后台线程需要更新UI,你可以发送Message内含一些数据给UI线程. 2.Handler Handler 即为处理者,是Message的主要处理者,负责Message的发送,Message内容的执行处理. 后台线程就是通过传进来的Handler对象引用来sendMessage(Message).而使用Handler,需要