Message priority in RabbitMQ

Sometimes messages simply have a higher priority and need to be handled before other messages. Thankfully, RabbitMQ supports priority queues. A queue can be set to have a range of priorities. Depending on the priority of the message, the message is placed in the appropriate priority queue.

Priority range

A queue's priority range needs to be applied when the queue is created. A priority queue is declared via the x-max-priority queue argument, where this argument should be an integer between 1 and 255.

The priority of each message is specified when the message is published to the queue. Larger numbers indicate higher priority, and messages with higher priority will be delivered before messages with less priority. Publishers specify message priority using the priority field in message properties. Messages without a priority property are treated as if their priority were 0.

Example use case

As an example, ElephantSQL (a database as a service) is using priority queues in the architecture for daily backup handling. Once a day there are thousands of “perform-database-backup” messages added to RabbitMQ, one for each database. Another service is then subscribing to the messages and creating the backups.

Backups can also be created on-demand via a customer API or through the web interface. This backup should be available immediately after the request was sent - it is a priority, in other words. In this case, the “perform-database-backup” message is added to the same queue, but this message has a higher message priority than all other requests. The message sent will be placed at the head of the queue immediately.

Priority queues best practice

It would be best if you were careful with the use of priority queues. Here are two things to think about:

  • Do not set a too high priority range; Each priority level uses an internal queue on the Erlang VM, which takes up some resources. In most use cases it is sufficient to have no more than five priority levels.
  • Use priority queues with care if you have specified a Queue Length Limit: Make sure that you are not reaching the limit of messages for a priority queue. Messages are discarded from the head of the queue, and your most important message might be dropped when a low priority message enters the queue.

The RabbitMQ priority queue implementation was added into RabbitMQ version 3.5.0.

Feel free to contact us at contact@cloudamqp.com if you have any questions.

CloudAMQP - industry leading RabbitMQ as a service

Start your managed cluster today. CloudAMQP is 100% free to try.

13,000+ users including these smart companies