首页 > 解决方案 > Why message can be lost when rabbitmq basic.publish return ok

问题描述

From the Rabbitmq doc

Networks can fail in less-than-obvious ways and detecting some failures takes time. Therefore a client that's written a protocol frame or a set of frames (e.g. a published message) to its socket cannot assume that the message has reached the server and was successfully processed. It could have been lost along the way or its delivery can be significantly delayed.

Using standard AMQP 0-9-1, the only way to guarantee that a message isn't lost is by using transactions -- make the channel transactional then for each message or set of messages publish, commit.

I wonder why the publish-confirm or transaction is required to prevent the message lost for producer publish.

If the amqp "basic.publish" is called successful, the api return ok, why the message still possible lost?

标签: rabbitmq

解决方案


该片段正在谈论 AMQP 0-9-1。RabbitMQ 提供了对该协议的扩展,其中之一是 Publisher Confirms。发布者确认不是 AMQP 0-9-1 标准本身的一部分。

如果您收到“OK”或 basic.ack,那么这就是发布者确认。一旦你收到了那个 bacic.ack 就意味着代理肯定收到了消息。但是此时您仍然可能“丢失”消息。如果没有绑定到该交换的队列或没有具有与消息匹配的绑定的队列,则代理将丢弃该消息。因此,尽管您收到了 basic.ack,但该消息现在已丢失。

因此,如果您真的想要一些保证,您应该使用 Publisher Confirms 并在消息上设置 Mandatory 标志。如果您使用此标志,那么如果代理收到消息但无法将其路由到交换,您将收到来自代理的 basic.return(后跟 basic.ack)响应。然后,您的应用程序可以采取适当的措施。


推荐阅读