0
0
RabbitMQdevops~5 mins

Priority queues in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, messages need to be handled in order of importance, not just in the order they arrive. Priority queues let RabbitMQ deliver higher priority messages first, so urgent tasks get done faster.
When you want urgent notifications to be processed before regular ones in a messaging system.
When your app handles tasks with different importance levels and you want to speed up critical jobs.
When you run a support ticket system and want high priority tickets to be addressed first.
When processing orders where express shipping orders should be handled before standard ones.
When you want to avoid delays in processing important messages during high traffic.
Config File - priority_queue_setup.sh
priority_queue_setup.sh
#!/bin/bash

# Declare a priority queue named 'task_queue' with max priority 10
rabbitmqadmin declare queue name=task_queue durable=true arguments='{"x-max-priority":10}'

# Bind the queue to the default exchange with routing key 'task'
rabbitmqadmin declare binding source=amq.direct destination=task_queue routing_key=task

This script creates a durable queue named task_queue that supports message priorities from 0 to 10. The x-max-priority argument enables priority handling. Then it binds the queue to the default direct exchange with routing key task so messages sent with this key go to the priority queue.

Commands
This command creates a durable queue named 'task_queue' that supports priorities from 0 to 10. Durable means it survives server restarts.
Terminal
rabbitmqadmin declare queue name=task_queue durable=true arguments='{"x-max-priority":10}'
Expected OutputExpected
{"queue":"task_queue","message_count":0,"consumer_count":0}
durable=true - Makes the queue survive RabbitMQ restarts
arguments='{"x-max-priority":10}' - Enables priority support with max priority 10
This binds the 'task_queue' to the default direct exchange with routing key 'task', so messages sent with this key go to the priority queue.
Terminal
rabbitmqadmin declare binding source=amq.direct destination=task_queue routing_key=task
Expected OutputExpected
{"source":"amq.direct","destination":"task_queue","routing_key":"task"}
Sends a message with low priority (1) to the 'task_queue'.
Terminal
rabbitmqadmin publish exchange=amq.direct routing_key=task payload='{"task":"low priority"}' properties='{"priority":1}'
Expected OutputExpected
{"routed":true}
properties='{"priority":1}' - Sets message priority to 1
Sends a message with high priority (9) to the 'task_queue'.
Terminal
rabbitmqadmin publish exchange=amq.direct routing_key=task payload='{"task":"high priority"}' properties='{"priority":9}'
Expected OutputExpected
{"routed":true}
properties='{"priority":9}' - Sets message priority to 9
Retrieves two messages from the 'task_queue' without requeuing them, showing that higher priority messages come first.
Terminal
rabbitmqadmin get queue=task_queue requeue=false count=2
Expected OutputExpected
Message 1: { "payload": "{\"task\":\"high priority\"}", "properties": {"priority":9} } Message 2: { "payload": "{\"task\":\"low priority\"}", "properties": {"priority":1} }
requeue=false - Prevents messages from going back to the queue after retrieval
count=2 - Fetches two messages
Key Concept

If you remember nothing else from this pattern, remember: setting the x-max-priority argument on a queue enables RabbitMQ to deliver higher priority messages before lower priority ones.

Common Mistakes
Not setting the x-max-priority argument when declaring the queue
Without this argument, the queue ignores message priorities and delivers messages in arrival order.
Always declare the queue with the x-max-priority argument set to the maximum priority value you want to support.
Sending messages without the priority property
Messages without a priority property default to zero priority, so they may be delivered after higher priority messages.
Include the priority property in message properties to control their delivery order.
Using a priority value higher than the queue's max priority
RabbitMQ caps message priority at the queue's max priority, so values above it have no extra effect.
Use priority values within the range 0 to x-max-priority.
Summary
Declare a queue with the x-max-priority argument to enable priority handling.
Bind the queue to an exchange with a routing key to route messages properly.
Publish messages with a priority property to control their delivery order.
Retrieve messages to see that higher priority messages are delivered first.