RabbitMQ,重新连接后的传递标签值和消息顺序

RabbitMQ, delivery tag value and message order after reconnect

我使用 pika python 与 RabbitMQ 通信。我有 6 个线程,它们使用并确认来自同一队列的消息。我为每个线程使用不同的连接(和通道)。所以我有几个非常接近的问题:

  1. 如果与 rabbit 的连接将在线程 1 中关闭,我将重新连接,交货标签值将重置,重新连接后它将从 0 开始?

  2. 重新连接后,我将以相同的顺序为每个线程接收相同的未确认消息,或者它将开始在所有线程之间再次分发它们,或者它将从重新连接点开始?

这在我的应用程序中很重要,因为消息接收和确认之间存在延迟,我想避免在接下来的流程步骤中出现重复。

  1. 交付标签是特定于渠道和服务器分配的。请参阅 AMQP 规范第 1.1 节中的详细信息。 AMQP 为 delivery-tag 定义了域 RabbitMQ's documentation delivery-tag 的 RabbitMQ 初始值为 1

    Zero is reserved for client use, meaning "all messages so far received".

  2. 单个队列中有多个消费者时,无法保证消费者将按照排队的相同顺序获取消息。详见 RabbitMQ 的 Broker Semantics, "Message ordering guarantees" paragraph

    Section 4.7 of the AMQP 0-9-1 core specification explains the conditions under which ordering is guaranteed: messages published in one channel, passing through one exchange and one queue and one outgoing channel will be received in the same order that they were sent. RabbitMQ offers stronger guarantees since release 2.7.0.

    Messages can be returned to the queue using AMQP methods that feature a requeue parameter (basic.recover, basic.reject and basic.nack), or due to a channel closing while holding unacknowledged messages. Any of these scenarios caused messages to be requeued at the back of the queue for RabbitMQ releases earlier than 2.7.0. From RabbitMQ release 2.7.0, messages are always held in the queue in publication order, even in the presence of requeueing or channel closure.

    With release 2.7.0 and later it is still possible for individual consumers to observe messages out of order if the queue has multiple subscribers. This is due to the actions of other subscribers who may requeue messages. From the perspective of the queue the messages are always held in the publication order.

    另外,参见 this answer for "RabbitMQ - Message order of delivery" question