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.
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.
Variants
| Design | Premium latency | Batch | Risk |
|---|---|---|---|
| Single FIFO | Waits behind every batch job | Fair | Urgent work is slow exactly when load is high |
| Priority lanes, shared consumers | Near zero | Gets the leftovers | Starvation if high-priority load ever fills capacity |
| Dedicated queues + consumers | Bounded by its own capacity | Guaranteed share | Idle capacity in one lane cannot help the other; size each lane |
Implementation
@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
| Benefits | Costs | |
|---|---|---|
| Responsiveness | Urgent work is unaffected by bulk floods. | Low-priority work absorbs all the delay. |
| Isolation | Dedicated lanes can never block each other. | Capacity is split and must be sized per lane. |
| Complexity | Simple 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).