In this lesson · topic 2 of 7
Topic 2
Capacity, full-queue policy and Little's law
5 min read
A queue meets a burst
Step through a small example. The queue has six places, and one consumer takes one message at a time.
A queue meets a burst
Step 1 / 6- waiting
- 1 of 6
A steady stream
Messages arrive about as fast as the consumer finishes them. The queue holds one message or none. This is a toy with six places, to show the idea.
A burst arrives
Several messages come at once, far faster than one consumer can take them. Without a queue the producer would have to wait, or the messages would be refused. Here they simply line up.
The consumer works through it
The consumer keeps its own pace and takes the oldest message each time. Nobody had to speed up. The burst waits its turn.
The queue empties
Quiet time after the burst lets the consumer catch up. A queue can only do this if the average arrival rate is below the consumer's speed. The burst was loud, but short.
The queue fills up
Now the burst is longer than the buffer, or the average rate is above the consumer's speed. All six places are taken and one more message arrives. Something has to give.
Three answers to a full queue
Block the producer, so the wait moves upstream and nothing is lost. Drop the newest message, which keeps the old ones and refuses the new. Drop the oldest, which keeps the freshest data. None of them creates capacity.
- A steady stream. Messages arrive about as fast as the consumer finishes them. The queue holds one message or none. This is a toy with six places, to show the idea. waiting: 1 of 6.
- A burst arrives. Several messages come at once, far faster than one consumer can take them. Without a queue the producer would have to wait, or the messages would be refused. Here they simply line up. waiting: 5 of 6.
- The consumer works through it. The consumer keeps its own pace and takes the oldest message each time. Nobody had to speed up. The burst waits its turn. waiting: 3 of 6.
- The queue empties. Quiet time after the burst lets the consumer catch up. A queue can only do this if the average arrival rate is below the consumer's speed. The burst was loud, but short. waiting: 0 of 6.
- The queue fills up. Now the burst is longer than the buffer, or the average rate is above the consumer's speed. All six places are taken and one more message arrives. Something has to give. waiting: 6 of 6; next message: no room.
- Three answers to a full queue. Block the producer, so the wait moves upstream and nothing is lost. Drop the newest message, which keeps the old ones and refuses the new. Drop the oldest, which keeps the freshest data. None of them creates capacity. choices: 3.
The parts
A queue has a producer side, where messages come in, and a consumer side, where they leave in the order they arrived. In between it has a capacity, the number of messages it is allowed to hold. Without a limit a queue can eat all the memory or disk of the machine that hosts it, so real queues have one, even if it is large.
The consumers are the other half. How many there are, and how fast each one is, decide how quickly the queue drains. Adding a consumer helps only if there is work waiting for it to take.
What a full queue does
Once the queue is full, the next message has to go somewhere. There are three common answers.
Block the producer. The send call waits until there is room. Nothing is lost, but the delay moves to the producer, and the producer’s own callers may be waiting on it. If the producer cannot afford to wait, the overload spreads upstream.
Drop the newest. The arriving message is refused. The messages already in the queue are kept. This is simple, and it protects the work already accepted.
Drop the oldest. The message at the head is evicted to make room. The queue always holds the most recent data. That suits a stream where only the latest reading matters, such as a position update, and it is wrong for an order that must not disappear.
None of these creates capacity. They choose who pays: the producer, the newest message, or the oldest one.
Little’s law
Little’s law says that in a stable system, the average number of items inside equals the average rate at which they arrive times the average time each one spends inside.
In symbols, L = λ W. Here L is the average number waiting, λ is the rate at which messages enter, and W is the average time a message spends in the queue.
Put numbers on it with the lab’s default setup. Averaged over the whole run, 12.9measured messages were waiting. Messages entered the queue at 6.86 a secondmeasured, and each spent 1.88 smeasured in the queue on average. Multiply the last two and you get 12.9measured, the same as the first.
The lab measures the two sides separately. L comes from watching the queue depth over time. λ comes from counting messages as they enter. W comes from timing each message. They agree because of the law, and not because the code was written to make them agree.
The law holds whatever the arrival pattern is and whatever order messages are served in. It is about averages, so it says nothing about the slowest messages. The fourth topic comes back to that.