Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ Message Sequence Guarantee

Tags:

rabbitmq

I have a project that involves rabbitmq. The problem that I have is illustrated as follows:

Problem

So now, let me describe the scenario. I have n number of queues which subscribed to topic1. Now my question is if I publish 3 messages in sequence, which are shown as 1, 2 and 3 into broker called Exchange, will rabbitmq Guarantee the sequence of those messages in all queues?

The only thing that I found was in rabbitmq documentation Message ordering guarantees which was taking about

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.

So can anyone help me out and point me to the right doc or example that shows whether it is guaranteed or not?

Thanks

like image 744
Ali Avatar asked Mar 13 '14 18:03

Ali


People also ask

Does RabbitMQ guarantee message order?

Message Ordering The RabbitMQ documentation states the following regarding its ordering guarantees: 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.

Is RabbitMQ sequential?

A RabbitMQ Queue is a sequential data structure in which an item can be enqueued at the last or dequeued from the head. Publishers and Consumers communicate using a queue-like storage mechanism.

Is it possible that multiple consumers of a RabbitMQ queue get the same message?

RabbitMQ has a plugin for consistent hash exchange. Using that exchange, and one consumer per queue, we can achieve message order with multiple consumers. The hash exchange distributes routing keys among queues, instead of messages among queues. This means all messages with the same routing key will go the same queue.

Are RabbitMQ messages persistent?

The short answer is yes: RabbitMQ supports persistence of messages. For this to happen the message must be persistent (set in the client) and the queue(s) that it is routed to must be Durable.


2 Answers

As the other poster mentioned, your scenario should work fine assuming a simple/basic consumer setup. But here's some additional info that might explain why.

I wasn't sure quite what nuances might have been wrapped up in that section of the documentation either, until I looked up exactly what a Channel was. A connection to RabbitMQ can have multiple "mini-connections" within it called channels. Each of these channels are independent and thus you could send multiple messages to the broker via multiple channels.

So as long as the messages in your scenario are sent on a single channel (you'd have to explicitly try to use multiple channels), they'll arrive in the queue in the same order you sent them. As long as the messages are consumed via a single channel, they'd arrive on the consumer in the same order they arrived in the queue (also being the same order they were sent).

From: https://www.rabbitmq.com/tutorials/amqp-concepts.html

Some applications need multiple connections to an AMQP broker. However, it is undesirable to keep many TCP connections open at the same time because doing so consumes system resources and makes it more difficult to configure firewalls. AMQP 0-9-1 connections are multiplexed with channels that can be thought of as "lightweight connections that share a single TCP connection".

like image 61
jumand Avatar answered Sep 21 '22 20:09

jumand


What you have quoted answers your question perfectly. The only question is what your consumer set up looks like. If you have each queue connected to its own channel and that consumer is running in its own thread, that thread will see each message in order as they were published.

like image 42
robthewolf Avatar answered Sep 17 '22 20:09

robthewolf