RabbitMQ – 非持久队列的使用案例

huangapple go评论51阅读模式
英文:

RabbitMQ - Use case for non-durable queues

问题

我不太理解非持久队列的用例。所以我有一个应该持续运行并消费RabbitMQ消息的服务。我不在乎一些消息是否丢失。据我了解,如果RabbitMQ服务器重新启动,非持久队列将不会被重新创建。所以例如,如果我的服务有一个非持久队列,RabbitMQ崩溃并重新启动,我的服务将只会抛出异常,因为队列不再可用,对吗?

所以我唯一想象得到非持久队列的用例是用于测试服务,因为在这种情况下,你不在乎删除的队列或消息。

英文:

I don't really get the use case of non-durable queues. So I have a service that should run permanently and that consumes messages of RabbitMQ. I don't care if some messages are getting lost. As far as I understood, non-durable queues won't be recreated if the RabbitMQ server is restarted. So for example if my service has a non-durable queue and RabbitMQ crashes and is getting restarted, my service would only throw exceptions since the queue is not available anymore, right?

So the only use case I can imagine for non-durable queues is for testing services because in that case you don't care about deleted queues or messages.

答案1

得分: 2

我理解,以下是翻译好的内容:

正如你正确指出的:“如果RabbitMQ服务器重新启动,非持久性队列将不会被重新创建”,这也意味着在这种情况下会丢失消息。

从本质上讲,正如@vvv444所指出的,持久性队列也被称为瞬态队列——这暗示了它的用途。

瞬态数据:非持久性队列非常适合不需要长期存储的瞬态数据。这可能包括实时分析或指标,在当下很重要,但在某个时间点后可能就不再需要了。

高吞吐量、低价值的数据:在处理数据量巨大,使持久性变得不切实际或不可取的情况下,非持久性队列可能是一个不错的选择。如果你每分钟处理数百万条消息,而每条消息的价值较低,那么确保每条消息都是持久性的可能会带来更多资源和复杂性方面的成本,这可能超过了好处。

测试:正如你提到的,非持久性队列在测试中非常有用,因为它们允许轻松清理设置,并且由于无需将消息写入磁盘,可能会提高性能。

关于“如果我的服务使用非持久性队列,而RabbitMQ崩溃并重新启动,我的服务将只会抛出异常”的问题,你是正确的,如果RabbitMQ崩溃并重新启动,非持久性队列和其中的消息将会丢失。然而,如果生产和消费这些队列的服务是针对这种情况设计的,它们可以优雅地处理这种情况。RabbitMQ集成的一种常见模式是构建服务,使其完全控制队列的整个生命周期,包括创建、绑定和删除。

英文:

As you stated correctly "non-durable queues won't be recreated if the RabbitMQ server is restarted" which also means that there will be loss of messages in this case.

In essence as @vvv444 notes, durable queues are also known as Transient queues -- this hints to it's usage.

Transient Data: Non-durable queues are a good fit for transient data that does not need to be stored long-term. This could include real-time analytics or metrics, which are important in the moment but may not be needed after a certain point.

High-throughput, low-value data: In cases where the sheer volume of data being processed makes persistence impractical or undesirable, non-durable queues can be a good fit. If you're processing millions of messages per minute and the individual value of each message is low, then the cost (both in terms of resources and complexity) of ensuring that every single one of those messages is durable might outweigh the benefits.

Testing: As you mentioned, non-durable queues can be useful for testing, as they allow for easily cleanable setups and may improve performance due to not having to write messages to disk.

Re "if my service has a non-durable queue and RabbitMQ crashes and is getting restarted, my service would only throw exceptions" You're correct that if RabbitMQ crashes and is restarted, non-durable queues and the messages within them will be lost. However, if the services that produce and consume from these queues are designed with this in mind, they can handle this scenario gracefully. A common pattern for RabbitMQ integration is to build services in a way that they control the entire lifecycle of a queue. This includes creation, binding, and deletion.

答案2

得分: 1

短暂(非持久)队列在队列寿命非常短暂的情况下提供一些延迟优势。例如,当针对每个用户连接到 Web 服务器创建临时队列时。这来自于RabbitMQ文档

在大多数情况下,队列是持久的还是非持久的并不会影响队列的吞吐量和延迟。只有在队列或绑定的变化非常频繁的环境中,即队列被删除并且重新声明了数百次或更多次每秒钟,才会在某些操作上看到延迟的改善,特别是在绑定方面。因此,持久队列和短暂队列之间的选择取决于用例的语义。

临时队列可以是对于具有短暂客户端的工作负载的一个合理选择,例如用户界面中的临时 WebSocket 连接,以及预计会下线或切换身份的移动应用程序和设备。这些客户端通常具有固有的短暂状态,在客户端重新连接时应该进行替换。

英文:

Transient (non-durable) queues provide some latency benefits in scenarios when queue lifetime is very short. For example, when a temporary queue is created per user connection to a web server. That's from the RabbitMQ documentation:

> Throughput and latency of a queue is not affected by whether a queue is durable or not in most cases. Only environments with very high queue or binding churn — that is, where queues are deleted and re-declared hundreds or more times a second — will see latency improvements for some operations, namely on bindings. The choice between durable and transient queues therefore comes down to the semantics of the use case.
>
> Temporary queues can be a reasonable choice for workloads with transient clients, for example, temporary WebSocket connections in user interfaces, mobile applications and devices that are expected to go offline or use switch identities. Such clients usually have inherently transient state that should be replaced when the client reconnects

huangapple
  • 本文由 发表于 2023年5月10日 16:57:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定