问题描述
我需要为我的新项目选择一个新的队列代理.
这次,我需要一个支持pub/sub的可伸缩队列,并且必须保持消息的顺序.
我读了亚历克西斯的评论:他写道:
我阅读了Rabbitmq文档中的消息排序部分:
如果我需要按照消息的顺序处理消息,那么我只能对每个使用者使用RabbitMQ,并且将其排他队列吗?
RabbitMQ仍然被认为是有序消息队列的好解决方案吗?
好吧,让我们仔细看看上面描述的场景.我认为将文档紧贴在您的问题的片段之前以提供上下文很重要:
因此,很明显,从2.7.0版开始,RabbitMQ在消息排序方面比原始AMQP规范进行了相当大的改进.
使用多个(并行)使用者,不能保证处理顺序.
第三段(粘贴在问题中)继续给出免责声明,我将其解释为:如果队列中有多个处理器,则不再保证将按顺序处理消息."他们在这里所说的只是RabbitMQ不能违抗数学定律.
请考虑银行的客户数量.这家特殊的银行以帮助客户按顺序进入银行而感到自豪.客户排队等候,并由3个可用的出纳员中的下一个服务.
今天早晨,碰巧所有三个出纳员都可以出门,接下来的三位顾客也来了.突然,三个出纳员中的第一个变得病重,无法为排队的第一个客户提供服务.到发生这种情况时,柜员2已经与客户2结束,柜员3已经开始为客户3服务.
现在,可能会发生以下两种情况之一. (1)排队的第一个客户可以返回到该行的开头,或者(2)第一个客户可以抢占第三个客户,从而使该柜员停止在第三个客户上工作,并开始在第一个客户上工作. RabbitMQ以及我所知的任何其他消息代理都不支持这种类型的抢占逻辑.在这两种情况下,第一个客户实际上都不会首先得到帮助-第二个客户却很幸运,可以幸运地找到一个好的,快速的出纳员.保证为客户提供帮助的唯一方法是一次由一名出纳员帮助一名客户,这将给银行造成重大的客户服务问题.
我希望这有助于说明您所问的问题.假设您有多个使用者,则无法确保在每种可能的情况下都按顺序处理消息.拥有多个队列,多个排他使用者,不同的代理人等等都没关系-无法保证一个先验消息会被多个使用者依次应答.但是RabbitMQ会尽力而为.
I need to choose a new Queue broker for my new project.
This time I need a scalable queue that supports pub/sub, and keeping message ordering is a must.
I read Alexis comment: He writes:
I read the message ordering section in rabbitmq docs:
If I need to handle messages by their order, I can only use rabbitMQ with an exclusive queue to each consumer?
Is RabbitMQ still considered a good solution for ordered message queuing?
Well, let's take a closer look at the scenario you are describing above. I think it's important to paste the documentation immediately prior to the snippet in your question to provide context:
So, it is clear that RabbitMQ, from 2.7.0 onward, is making a rather drastic improvement over the original AMQP specification with regard to message ordering.
With multiple (parallel) consumers, order of processing cannot be guaranteed.
The third paragraph (pasted in the question) goes on to give a disclaimer, which I will paraphrase: "if you have multiple processors in the queue, there is no longer a guarantee that messages will be processed in order." All they are saying here is that RabbitMQ cannot defy the laws of mathematics.
Consider a line of customers at a bank. This particular bank prides itself on helping customers in the order they came into the bank. Customers line up in a queue, and are served by the next of 3 available tellers.
This morning, it so happened that all three tellers became available at the same time, and the next 3 customers approached. Suddenly, the first of the three tellers became violently ill, and could not finish serving the first customer in the line. By the time this happened, teller 2 had finished with customer 2 and teller 3 had already begun to serve customer 3.
Now, one of two things can happen. (1) The first customer in line can go back to the head of the line or (2) the first customer can pre-empt the third customer, causing that teller to stop working on the third customer and start working on the first. This type of pre-emption logic is not supported by RabbitMQ, nor any other message broker that I'm aware of. In either case, the first customer actually does not end up getting helped first - the second customer does, being lucky enough to get a good, fast teller off the bat. The only way to guarantee customers are helped in order is to have one teller helping customers one at a time, which will cause major customer service issues for the bank.
I hope this helps to illustrate the problem you are asking about. It is not possible to ensure that messages get handled in order in every possible case, given that you have multiple consumers. It doesn't matter if you have multiple queues, multiple exclusive consumers, different brokers, etc. - there is no way to guarantee a priori that messages are answered in order with multiple consumers. But RabbitMQ will make a best-effort.
这篇关于RabbitMQ-消息传递顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!