In this lesson · topic 6 of 7
Topic 6
One slow message holds up the line
7 min read
The line is strictly first come, first served
The queue in this lesson serves messages in arrival order. That is fair, and it is also the source of a quiet problem. A message that takes a long time holds its consumer, and everything behind it waits for a free consumer.
Take three consumers. Ordinary messages take 0.3 seconds. Once in a while a report-generation message arrives that takes 30 seconds. If three report messages arrive close together, all three consumers are busy for 30 seconds. Every ordinary message that arrives in that window waits, though each would have taken a third of a second. This is head-of-line blocking: the item at the front of the line, or occupying the servers, decides how long everyone else waits.
Average wait barely shows it. Suppose 1 message in 100 is a report. The mean service time is 0.99 x 0.3 + 0.01 x 30, which is about 0.6 seconds, twice the ordinary figure. But the ordinary messages that hit the window wait up to 30 seconds. The tail suffers, and the mean hides it. Dean and Barroso make the same point for request latency: rare slow items dominate the 99th percentile.
Predict first
Separate queues by cost
The simplest cure is one queue per class of work, each with its own consumers. Quick jobs get two consumers and slow jobs get one. A burst of reports now fills the report queue and delays only other reports. The cost is lower utilisation, since an idle report consumer cannot help with quick jobs, and more queues to watch.
Split by cost, or by who is waiting, but keep the number of classes small. Three queues are manageable. Thirty become a capacity planning problem.
Priorities inside one queue
Some brokers let one queue serve high-priority messages first. RabbitMQ has priority queues, with a small number of levels recommended. This helps urgent messages overtake others, and it creates starvation: while high-priority work keeps arriving, low-priority work may never run.
Two guards are common. Cap the share of capacity high priority may use, so low priority always gets some. Or raise a message’s priority as it ages, so anything waiting long enough eventually wins. Both give up a little speed for the urgent traffic to guarantee progress for the rest.
A priority does not preempt running work either. If all consumers are busy with 30 second jobs, a new urgent message waits until one finishes. Priorities order the waiting line, and they do not interrupt service.
Worked example: how much to reserve
Quick jobs arrive at 20 a second and take 0.3 seconds, so they need 20 x 0.3 = 6 consumers’ worth of time on average. Reports arrive at 0.2 a second at 30 seconds each, which is another 6. A shared pool of 12 consumers is exactly full, with no slack, and waiting will be severe by the lesson’s own rule. A pool of 16 split 9 and 7 leaves about 33 percent slack in the quick pool and 14 percent in the report pool. The quick pool handles bursts well, and reports wait a bit longer, which users tolerate.
Sources
- Jeffrey Dean and Luiz Andre Barroso, The Tail at Scale, Communications of the ACM, 2013.
- RabbitMQ documentation, Priority Queues.
- Mor Harchol-Balter, Performance Modeling and Design of Computer Systems, Cambridge University Press, 2013.