Video in progress

Strong Middle 🏗️ System design

In one queue, VIP customers' orders are stuck behind regular orders. How do you organize priority?

The most reliable way is separate queues by priority, with dedicated or more workers for them. Priority inside a single queue (a RabbitMQ priority queue) works at small volume, but you also have to account for low-priority work "starving".

📖 Answer

The problem usually shows up on a campaign day: 200 thousand regular orders are in the queue, and a VIP customer's order waits 40 minutes. FIFO is fair, but it isn't right for the business.

Option 1 — separate queues.

orders.high   -> 10 workers (this queue only)
orders.normal -> 20 workers

It's simple and predictable: high priority has its own workers, and regular traffic doesn't touch them. It's the same in Kafka — separate topics and separate consumer groups, because Kafka doesn't know priority within a partition.

Option 2 — a worker reads both queues, but high first: it takes from normal only when high is empty. It saves resources, but if high is always full, normal is never processed (starvation). So you set a ratio: for example, 1 normal after every 4 high.

Option 3 — a RabbitMQ priority queue (x-max-priority). Within one queue, higher-priority messages jump ahead. But this only works when messages pile up in the queue, and if prefetch is large, the consumer will still process the low-priority messages it has already taken first — keep prefetch small.

Other things to think about:

  • who sets the priority — the server, not the client (otherwise everyone makes themselves VIP);
  • monitoring per queue: "age of the oldest message in the high queue" is the most important alert;
  • priority has to be preserved not only in the queue but in later stages too (payment, inventory), otherwise the jam just moves there.