Priority Queue: Serve Urgent Requests First, Without Starving the Rest
A priority queue lets urgent messages skip ahead of bulk work. Learn the two ways to build one, and the starvation risk you must watch.
In short
A priority queue lets urgent messages skip ahead of bulk work, but low-priority work can starve if it is never given a turn.
11 min readFree
What You Will Learn
Some work matters more than other work. A payment that a customer is waiting for is not the same as an email that can arrive an hour late. A priority queue is a messaging pattern that handles this difference. Requests still go into a queue, but the queue does not always serve them in arrival order. The most important requests are handled first, so they finish sooner.
By the end of this lesson, you will be able to explain why a plain queue can fail urgent work, describe the two main ways to build a priority queue, follow a worked example, and name the trade-offs. The biggest trade-off is starvation, where low-priority work waits so long that it is effectively never done.
Key words
- Message
- A small package of data sent from one part of a system to another, such as 'charge this card'.
- Queue
- A line where messages wait until a worker is ready to handle them.
- FIFO (first in, first out)
- A queue rule where the oldest message is handed out first.
- Priority value
- A number or label attached to a message that says how urgent it is.
- Producer
- The part of a system that creates a message and sends it into a queue.
- Consumer or worker
- A program that reads a message from a queue and does the work it describes.
- Message broker
- The software that stores messages and delivers them to consumers.
- Starvation
- A state where some messages wait so long that they are never handled.
- SLA (service-level agreement)
- A written promise about how fast or how reliable a service will be.
Why Is One Line Not Always Fair?
Most queues start as first-in, first-out. That feels fair, because nobody cuts in line. But a FIFO queue ignores how much each message matters. Picture a shop with one checkout line, where a person buying a single coffee waits behind someone returning a pallet of goods. The coffee buyer is treated the same, even though their job takes seconds.
In software, this happens when a large batch of background jobs fills the queue. A user who taps Pay then waits behind thousands of unrelated tasks. Their request is not broken, but it is too slow to accept. Microsoft's pattern guide says that the default FIFO design does not account for how important each task is, and that slow handling of urgent tasks can hurt the user experience and break service promises.
Tip
Before you add priorities, write down which requests must finish fast and which can wait. If you cannot name the urgent ones, you do not need a priority queue yet.
What Are the Two Ways to Build It?
Both designs start the same way. The producer attaches a priority value to each message before sending it. The designs differ in how the broker stores the messages and how consumers read them. Microsoft's guidance names two main approaches.
Approach one: a single sorted queue
All messages go into one queue. The queue keeps them in priority order, so the next consumer always gets the most urgent message.
Approach two: one queue per level
Each priority level gets its own queue. The producer picks the right queue, and consumers check the high-priority queue before the others.
Pick by how different the work is
A single queue is simpler to run. Separate queues make sense when urgent work needs its own workers and its own monitoring.
The next two sub-topics explain each approach in more detail.
Single Sorted Queue
In a single sorted queue, the broker does the ordering. Each new message is placed according to its priority value, and consumers read from the front. The design stays small, with one queue to watch. The cost is that the broker must keep messages in priority order, and not every broker can do this well. We come back to that under broker support.
Separate Queues With Their Own Workers
With separate queues, each level has a lane of its own. Consumers in the high lane can be given more workers, or faster ones, so urgent work does not compete with bulk work for the same capacity.
- 1
Create one queue per level
For example, make a high queue, a normal queue, and a low queue.
- 2
Tag each message
The producer sets the priority value and sends the message to the matching queue.
- 3
Assign workers by level
Give the high queue more consumers or faster ones. Give the low queue fewer.
- 4
Read the high queue first
Each worker checks the high queue first, and takes from a lower queue only when the higher ones are empty.
- 5
Track each lane
Watch how many messages wait in each queue and how long they wait. If the high queue keeps growing, add workers there.
Worked Example: A Payment App
Imagine a payment app with two kinds of background work. The first is confirm card payment. A customer waits for it on the checkout screen. The second is email the monthly statement. It can arrive later. If the statement emails share one first-in, first-out queue, a big batch could sit in front of a payment confirmation. The team decides to use separate queues.
- 1
Name the two queues
Create a high-priority queue for card confirmations and a low-priority queue for statement emails.
- 2
Set priority at the source
When a customer taps Pay, the checkout code sends a confirm card payment message with the high value. When the month closes, the statement job sends its messages with the low value.
- 3
Route each message
The broker places each message in the queue that matches its priority value.
- 4
Give the high queue more workers
A larger worker pool reads the high queue. A smaller pool reads the statement queue, so a flood of emails cannot take all the workers.
- 5
Check both paths
The team watches the checkout queue for growth. It also confirms that statement emails still go out every day.
Quick check
A photo-printing site sends every order into one first-in, first-out queue. During a big sale, customers who paid for express delivery complain that their orders wait behind thousands of standard orders. Which change fits best, and which risk should you watch afterward?
Seeing the Order in Code
The sample below uses only Python's standard library. Its heap is a binary heap that keeps only the smallest item at the top. A smaller number means more urgent, so 0 is the high level. A counter in each tuple keeps messages of equal priority in arrival order.
import heapq
queue = []
heapq.heappush(queue, (2, 1, 'email monthly statement'))
heapq.heappush(queue, (0, 2, 'confirm card payment'))
heapq.heappush(queue, (2, 3, 'email monthly statement'))
heapq.heappush(queue, (0, 4, 'confirm card payment'))
while queue:
priority, order, job = heapq.heappop(queue)
print(priority, order, job)
Output
0 2 confirm card payment 0 4 confirm card payment 2 1 email monthly statement 2 3 email monthly statement
Both payment messages come out first, even though a statement arrived before them. This is the core idea in miniature. A real broker does the same kind of choosing, at much larger scale.
Trade-offs to Weigh
A priority queue is not free. Each choice below costs something, and a poor setup can hurt the work it was meant to help. Four trade-offs matter most for a beginner: starvation, who sets the priority, extra cost and complexity, and broker support.
What Is Starvation of Low-priority Work?
Starvation happens when low-priority messages wait so long that they are effectively never handled. If high-priority messages keep arriving, a single pool of workers may never reach the low queue.
Tip
Starvation is quiet. Nothing crashes, and the low queue simply grows. Alert on the age of the oldest waiting message, not only on errors.
How Do You Keep Low-priority Work Moving?
Teams use a few simple rules to stop starvation. None of them removes the trade-off, but each one keeps low-priority work from being forgotten.
Age-based promotion
Track how long each message has waited. After a set time, move it up a level. One guide gives the example of raising low messages older than ten minutes to medium.
Weighted polling
Take several high messages for every low message, instead of always emptying the high queue first.
A dedicated low pool
Give the low queue its own workers. Microsoft's guidance says that separate consumer pools suit strict performance and fault-isolation needs.
Who Sets the Priority, and How Many Levels Are Needed?
The producer sets the priority, so the producer's rules decide what counts as urgent. If every team can mark its work as urgent, the label loses meaning. Define a few levels with clear meanings, such as work that must be processed within a stated time. Microsoft's guidance gives the example of high-priority messages that need processing within 10 seconds. Keep the number of levels small. Each extra level adds another queue or another check to monitor.
Quick check
A support team's app lets staff mark any ticket as high priority. Within a week, almost every ticket is marked high, and the true low-priority tickets wait for days. What is the best first fix?
Extra Cost and Complexity
More queues can mean more fees
Some queue services charge for posting, retrieving, and checking messages, and those fees can grow with the number of queues.
More worker pools to manage
Each pool needs its own size, scaling rules, and monitoring. Scaling pools with queue length is a common way to keep them in step.
Harder to explain a delay
A message's wait now depends on its level and on the load at that level. Logs and dashboards must show both.
Broker Support
Not every message broker supports priority in the same way. RabbitMQ has native priority queues, set through a queue argument that defines the highest priority value. A comparison page lists Amazon SQS and Apache Kafka as brokers without native priority support. Another summary says few brokers support native priority, and that separate queues per priority are the most common approach. Read your broker's documentation before you design around a single sorted queue.
Quick check
A team uses a message broker that has no built-in priority setting. They still need urgent messages handled first. What is the most common approach to try?
When Not to Use It
Microsoft's guidance says a priority queue may not suit a system where all work has similar importance and strict first-in, first-out order matters most. It is also a poor fit when tasks depend on each other in order, because moving a later task ahead can produce wrong results. Two related ideas have their own lessons: smoothing sudden bursts with a buffer queue, and keeping related messages in their correct order.
Tip
If a fair line is what users expect, such as a ticket queue where everyone waits their turn, a plain first-in, first-out queue may be the better design.
Summary
A priority queue lets urgent messages be handled before less urgent ones. You can build it as one sorted queue or as separate queues with their own workers. The main risk is starvation, so use clear priority levels, give low work a guaranteed share, and watch how long the oldest messages wait. Check whether your broker supports priority before you choose the design.
Sources
- Priority Queue pattern, Azure Architecture Center (Microsoft Learn)
- Cloud design patterns catalog, Azure Architecture Center (Microsoft Learn)
- Priority Support in Queues, RabbitMQ documentation 4.2
- 8 Message Queue Types Every Backend Engineer Should Know (Design Gurus newsletter)
- Priority Queue Pattern, Reliability and Resilience Patterns (sysdesai.com)
Last updated on 18 November 2026
Related lessons
- System designQueue-Based Load Leveling: Smooth Sudden Traffic Bursts With a Queue
- System designTask Queues: Doing Slow Work in the Background, Reliably
- System designMessage Queues: Let Services Hand Off Work and Keep Running
- System designPull CDNs: How Edge Servers Fetch Content on First Request and Keep It
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.