Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ and message priority

Tags:

rabbitmq

Does RabbitMQ have any concept of message priority? I have an issue where some more important messages are being slowed down due to less important messages sitting before them in the queue. I want the high-priority ones to take precedence and move to the front of the queue.

I know I can approximate this using two queues, a "fast" queue and a "slow" queue, but that seems like a hack.

Does anyone know of a better solution using RabbitMQ?

like image 205
Kelly Avatar asked May 24 '12 20:05

Kelly


People also ask

Is RabbitMQ First In First Out?

Queues in RabbitMQ are FIFO ("first in, first out"). Some queue features, namely priorities and requeueing by consumers, can affect the ordering as observed by consumers.

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.

What is a message priority?

A category of precedence reserved for messages that require expeditious action by the addressee(s) and/or furnish essential information for the conduct of operations in progress when routine precedence will not suffice.


3 Answers

The answers on this question are out-of-date. As of RabbitMQ 3.5.0, there is now in-core support for AMQP standard per-message priorities. The documentation has all the gory details, but in short:

  • You need to define the queue's priority range at the time the queue is created;
  • Messages without a priority set get a priority of 0;
  • Messages with a numeric priority higher than the maximum set on the queue get the highest priority the queue supports.

More interesting caveats are in the docs. It's well worth reading them.

like image 184
womble Avatar answered Oct 05 '22 06:10

womble


Rabbit has no concept of priority other than, as Brian succinctly puts it, the one in front gets there first. ;-)

I would suggest implementing a set of queues that serve to service your particular messaging need and have these queues model your prioritisation need by, say, calling them 'MyQueueP1', 'MyQueueP2' and so on and then have our consumer(s) check P1 before P2 (etc.) and service messages from there first.

If you then have a message that is high priority you would publish it to the appropriate priority queue by way of a suitable routing key and voila.

[update] Check this question: In a FIFO Qeueing system, what's the best way the to implement priority messaging

[update] As per recent RabbitMQ release 3.5.0 this answer is now outdated and should be considered valid for only versions prior to this release. https://stackoverflow.com/a/29068288/489888

like image 26
Steve Martin Avatar answered Oct 05 '22 06:10

Steve Martin


IIRC RabbitMQ still uses the AMQP protocol version 0.9.1 (get the spec here). The spec definitely mentions message priority:

Messages may have a priority level. A high priority message is sent ahead of lower     priority messages
waiting in the same message queue. When messages must be discarded in order to maintain a specific
service quality level the server will first discard low-priority messages.

And:

Note that in the presence of multiple readers from a queue, or client transactions, or use of priority fields,
or use of message selectors, or implementation-specific delivery optimisations the queue MAY NOT
exhibit true FIFO characteristics.

The spec says priority is a MUST, so I guess RabbitMQ should implement it, but you may want to consult its documentation.

like image 10
vanza Avatar answered Oct 05 '22 04:10

vanza