Skip to content
System Design Lab
Microservice Patterns

Priority Queue

Let urgent work overtake routine work in a queue — with priority lanes or separate queues and consumers — so a batch flood never makes interactive users wait, without starving the batch forever.

Beginner 3 min read· Updated 2026-11-18

What you will learn

  • See how one FIFO queue makes urgent work wait behind bulk work.
  • Compare priority lanes with dedicated queues and consumers.
  • Recognise and prevent starvation of low-priority work.
  • Choose how to express priority with real brokers.

Before you start

Overview

A hospital emergency room does not treat patients strictly in arrival order: a heart attack goes before a sprained ankle. Some hospitals also keep a separate fast-track desk for minor cases, so they are not ignored all night.

A plain queue is FIFO: a burst of low-value work delays everything behind it. A priority queue lets consumers take higher-priority messages first. The common variants are priority lanes in one queue (or one consumer pool reading high first) and dedicated queues with their own consumers per priority, which isolates the lanes completely.

Run it

A nightly batch dumps 400 report jobs at t = 5 s while premium users keep submitting. Compare Wait for premium jobs across the three designs.

Loading simulation…

Variants

DesignPremium latencyBatchRisk
Single FIFOWaits behind every batch jobFairUrgent work is slow exactly when load is high
Priority lanes, shared consumersNear zeroGets the leftoversStarvation if high-priority load ever fills capacity
Dedicated queues + consumersBounded by its own capacityGuaranteed shareIdle capacity in one lane cannot help the other; size each lane

Implementation

Two queues, consumers read high first (with a fairness guard)
@Scheduled(fixedDelay = 50)
void poll() {
    // Take up to 9 premium jobs for every batch job, so batch is slower but never starves.
    for (int i = 0; i < 9; i++) {
        Job job = premiumQueue.poll();
        if (job == null) break;
        workers.submit(() -> run(job));
    }
    Job batch = batchQueue.poll();
    if (batch != null) workers.submit(() -> run(batch));
}

Trade-offs

BenefitsCosts
ResponsivenessUrgent work is unaffected by bulk floods.Low-priority work absorbs all the delay.
IsolationDedicated lanes can never block each other.Capacity is split and must be sized per lane.
ComplexitySimple to reason about with two or three levels.Many priority levels are hard to tune and to explain.

Production considerations

  • Keep to two or three priorities with clear rules about who sets them.
  • Alert on the age of the oldest message per lane, not only on depth.
  • Protect the high lane from abuse: if anyone can mark work urgent, everything becomes urgent.
  • Autoscale lanes independently (queue depth is a good signal).

Further reading

Related topics