Consumer Priority

Sometimes you might want to have consumers with more computing resources do more work than their relatively less powerful contemporaries. This is where consumer priority comes in.

What is consumer priority?

When consumers have different priorities in LavinMQ, higher-priority consumers with capacity receive messages before lower-priority consumers. Lower-priority consumers receive messages only when higher-priority consumers no longer have capacity(are blocked).

This means that when a message is ready to be delivered, LavinMQ checks if there are any higher-priority consumers that can handle it. If there are, the message is sent to the highest-priority consumer that is ready to receive it.

If multiple consumers share the same priority, LavinMQ follows a round-robin approach to distribute messages among them.

Consumers with capacity and blocked consumers

In the preceeding section, we pointed out two key things: - High priority consumers can only receive messages when they have capacity. - Once the high priority consumers are blocked, LavinMQ will start sending messages to the lower priority consumers.

But what does a consumer having capacity really mean?

A consumer is said to have capacity if it is ready to receive a message immediately. On the other hand, a consumer becomes blocked when it is unable to receive messages. This can happen because its channel has reached the maximum limit of unacknowledged messages set by its prefetch configuration, for example.

At any given time, a consumer is either ready to receive messages(having capacity) or it is blocked.

When can I use consumer priority?

Here is one typical scenario where consumer priority can be beneficial:

  • Resource Management: Consumer priority can be utilized to manage limited resources effectively. For example, you can assign higher priorities to consumers with more computing power or available resources, allowing them to handle important tasks efficiently.

Using consumer priority in LavinMQ

To assign priority to a consumer, you can set the x-priority argument in the basic.consume method to an integer value. If a consumer doesn’t specify a value, it has a priority of 0. Higher numbers indicate higher priority, and you can use both positive and negative numbers.

For example the code snippet below sets the priority on a consumer to 10. We are using Pika, the Python client.

channel.basic_consume(
    "hello_world",
    callback,
    auto_ack=True,
    arguments={
      'x-priority':10
    }
)

Edge cases

Here are some common edge cases to keep in mind.

Multiple consumers with the same priority

If multiple consumers share the same priority, LavinMQ does not follow a round-robin approach to distribute messages among them ouf of the box. Instead, it will send messages to the first consumer bound to the queue.

If you do however, need a fair distribution of messages among consumers with the same priority, then you can set their prefetch value to 1.

Single active consumers and priority consumers

If the single active consumer feature has been enabled on a queue, then that queue cannot have priority consumers.

Wrap up

We’ve explored the concept of priority consumers in LavinMQ.

Ready to take the next steps? Here are some things you should keep in mind:

Managed LavinMQ instance on CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.