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, picture a database as a service. To handle daily backups for their users, a priority queue is beneficial in this architecture. Imagine that thousands of "perform-database-backup" messages are added to RabbitMQ once a day, one for each database. Another service then subscribes to these messages and creates the backups.

However, backups can also be created on-demand via a customer API or through the web interface. These backups should be available immediately after the request is sent - they are a priority. In this case, the "perform-database-backup" message is added to the same queue, but with a higher message priority than all other requests. This prioritized message is 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