---
title: Queues and waiting times
heading: Why a router at 90% load is so much slower than at 80%
eyebrow: A11 · Lesson 51 · Queues
xp: 40
---

::: hook
Packets arrive at a router at random and wait their turn to be sent. Interrupts queue for a processor, print jobs for a printer, calls for an operator. When arrivals are random, a server that is busy less than 100% of the time still builds up queues, and those queues grow explosively as the load approaches 100%. The simplest model, the M/M/1 queue, with Poisson arrivals and random service times, captures this exactly, and **Little's law**, which holds for almost any queue, ties together how many are waiting and how long they wait.
:::

::: play
- The curve is the mean number in an M/M/1 system, $\frac{\rho}{1 - \rho}$, against the utilization $\rho$, the fraction of time the server is busy.
- At 50% load there's about one in the system on average. At 90%, nine; at 99%, ninety-nine.
- The readouts give the mean time in the system, for a server that handles 1000 per second, and the chance the buffer holds more than 20.
:::

```sim plot
view: graph
x: rho = 0..0.98
y: L = 0..50
slider: u = 0.1..0.98 step 0.01 = 0.8
plot: rho/(1 - rho) | label: mean number in the system | samples: 400
marker: rho = u | label: load
readout: mean number at this load = u/(1 - u) | digits: 4
readout: mean time in system at 1000 per second, ms = 1/(1 - u) | digits: 4
readout: chance of more than 20 in the system = u^21 | digits: 4
```

::: predict
## A link serves 1000 packets per second. Traffic rises from 800 to 900 per second. What happens to the average delay?

- [ ] It rises about 12%, in proportion to the traffic
- [x] It doubles, from 5 ms to 10 ms
- [ ] It barely changes until the load reaches 100%
- [ ] It falls, since the server stays busier

::: reveal
It doubles. The mean time in an M/M/1 system is $\frac{1}{\mu - \lambda}$: $\frac{1}{200}$ s = 5 ms at 800 per second and $\frac{1}{100}$ s = 10 ms at 900. The spare capacity $\mu - \lambda$ is what matters, and it halved. That's why networks are engineered to run well below full load.
:::
:::

::: nameit
## The M/M/1 queue

Arrivals are Poisson at rate $\lambda$, service times are exponential with rate $\mu$, and there's one server. With utilization $\rho = \frac\lambda\mu < 1$:
- the number in the system is geometric: $P(n) = (1 - \rho)\rho^n$, so $P(\text{more than } n) = \rho^{n+1}$;
- the mean number in the system is $L = \frac{\rho}{1 - \rho}$;
- the mean time in the system is $W = \frac{1}{\mu - \lambda}$;
- the mean wait before service starts is $W_q = \frac{\rho}{\mu - \lambda}$.

At $\rho \ge 1$ the queue grows without bound.

## Little's law

For any stable system, the average number inside equals the arrival rate times the average time spent inside:

$$L = \lambda W$$

It needs no assumptions about the distributions. A router holding 4 packets on average while 800 per second pass through keeps each one for 5 ms.

## Buffers

A finite buffer drops arrivals when full. For a buffer of $N$ packets in an M/M/1 system, the overflow probability is about $\rho^{N+1}$ when it's small: each extra packet of buffer cuts losses by a factor $\rho$, which buys a lot at 50% load and very little near 100%.
:::

::: example
## Packets arrive at 800 per second at a link that serves 1000 per second. Find the utilization, the mean number in the system and the mean delay.

::: solution
1. **Utilization.**
   $$\rho = 0.8$$
2. **Mean number in the system.**
   $$\begin{aligned} L &= \frac{0.8}{0.2} \\ &= 4\ \text{packets} \end{aligned}$$
3. **Mean delay.**
   $$\begin{aligned} W &= \frac{1}{1000 - 800}\ \text{s} \\ &= 5\ \text{ms} \end{aligned}$$
4. **Check with Little's law.**
   $$\begin{aligned} \lambda W &= 800 \times 0.005 \\ &= 4 \end{aligned}$$

**Answer:** the utilization is 0.8, the mean number in the system 4 packets, and the mean delay 5 ms.
:::
:::

::: example
## At the same link, traffic rises to 900 per second. What happens?

::: solution
1. **New figures.** $\rho = 0.9$ and $L = 9$, and
   $$\begin{aligned} W &= \frac{1}{100}\ \text{s} \\ &= 10\ \text{ms} \end{aligned}$$
2. **Compare.** A 12.5% increase in traffic more than doubled the number waiting and doubled the delay.
3. **What it means.** The last 10% of capacity is the most expensive to use.

**Answer:** the utilization rises to 0.9, the mean number in the system to 9 and the mean delay to 10 ms.
:::
:::

::: example
## At 80% load, how often does the system hold more than 20 packets?

::: solution
1. **More than 20.**
   $$\begin{aligned} P(\text{more than } 20) &= \rho^{21} \\ &= 0.8^{21} \\ &= 0.0092 \end{aligned}$$
   about 1% of the time.
2. **A 20-packet buffer.** A buffer of 20 packets would drop about 1% of arrivals.
3. **At 50% load.** The same buffer would overflow with probability $0.5^{21} = 5 \times 10^{-7}$.

**Answer:** it holds more than 20 packets 0.92% of the time, about 1%.
:::
:::

::: check
given: l = 500 | 800 | 950; m = 1000 | 2000
answer: 1000/(m - l)
unit: ms
tolerance: 0.5%

## Packets arrive at {l} per second at a server that handles {m} per second. What is the mean time in the system?

::: right
Right. $W = \frac{1}{\mu - \lambda}$ = {answer}.
:::

::: misses
- [1000/m] That's the time for service alone, without waiting.
- [any] Not yet. $W = \frac{1}{\mu - \lambda}$.
:::
:::

::: check
given: rho = 0.5 | 0.8 | 0.95
answer: rho/(1 - rho)
tolerance: 0.5%

## An M/M/1 server is busy a fraction {rho} of the time. How many are in the system on average?

::: right
Right. $\frac{\rho}{1 - \rho}$ = {answer}.
:::

::: misses
- [rho] That's the mean number in service, not in the whole system.
- [any] Not yet. $L = \frac{\rho}{1 - \rho}$.
:::
:::

::: check
given: L = 3 | 12; l = 100 | 400
answer: L/l*1000
unit: ms
tolerance: 0.5%

## A system holds {L} jobs on average while {l} per second pass through. How long does each spend inside, on average?

::: right
Right. Little's law: $W = L/\lambda$ = {answer}.
:::

::: misses
- [L*l] Divide, don't multiply.
- [any] Not yet. $L = \lambda W$.
:::
:::

::: check
given: rho = 0.5 | 0.7 | 0.9; N = 10 | 30
answer: rho^(N + 1)
tolerance: 0.5%

## At utilization {rho}, what is the chance an M/M/1 system holds more than {N}?

::: right
Right. $\rho^{N+1}$ = {answer}.
:::

::: misses
- [rho^N] More than $N$ means at least $N + 1$.
- [any] Not yet. The number is geometric: $P(\text{more than } n) = \rho^{n+1}$.
:::
:::

::: check
kind: parts
given: l = 600 | 900; m = 1000 | 1200

## Arrivals at {l} per second, service at {m} per second.

::: part
answer: l/m
tolerance: 0.5%

### What is the utilization?

::: misses
- [m/l] Arrival rate over service rate.
- [any] $\rho = \lambda/\mu$.
:::
:::

::: part
answer: l/m/(m - l)*1000
unit: ms
tolerance: 0.5%

### What is the mean wait before service starts?

::: misses
- [1000/(m - l)] That's the total time in the system; subtract the service time $1/\mu$.
- [any] $W_q = \frac{\rho}{\mu - \lambda}$.
:::
:::
:::

::: check
kind: derivation
vars: r = 0.05..0.95

## Derive the M/M/1 mean from $P(n) = (1 - r)r^n$.

::: step
answer: r/(1 - r)

### What is $\sum n(1 - r)r^n$?
:::

::: step
answer: r^3

### What is the chance of more than two in the system?
:::

::: right
Right. A geometric distribution: mean $\frac{r}{1 - r}$, tail $r^{n+1}$.
:::
:::

::: check
kind: python

## Simulate an M/M/1 queue with arrivals at 800 per second and service at 1000 per second for 200 000 packets (`np.random.default_rng(3)`): each packet starts service at the later of its arrival and the previous packet's departure. Set `W` to the mean time in the system, in ms.

```python starter
import numpy as np

W = 0.0
print(W)
```

```python solution
import numpy as np

rng = np.random.default_rng(3)
n = 200000
arrive = np.cumsum(rng.exponential(1 / 800, n))
service = rng.exponential(1 / 1000, n)
depart = np.empty(n)
prev = 0.0
for i in range(n):
    prev = max(arrive[i], prev) + service[i]
    depart[i] = prev
W = np.mean(depart - arrive) * 1000
print(W)
```

```python tests
assert abs(W - 5) < 0.3, f"The M/M/1 formula gives 1/(1000 - 800) = 5 ms; got {W}."
```

::: right
Right. About 5 ms. The simulation needs no assumptions beyond the arrival and service times, so it also handles cases with no formula.
:::
:::

::: explain
## Why does delay grow so steeply as a queue's utilization approaches 100%, even when arrivals are below the service rate on average?

::: model
With random arrivals, there are stretches when jobs arrive faster than the server can handle them and stretches when it sits idle. Idle time is lost forever, since the server can't work ahead, but a burst leaves a backlog that has to be worked off. The closer the load is to capacity, the less spare time there is to work off backlogs, so they persist and pile up. In the M/M/1 model the mean delay is $\frac{1}{\mu - \lambda}$, inversely proportional to the spare capacity: halving the spare capacity doubles the delay. Perfectly regular arrivals wouldn't queue at all; randomness is what makes waiting.
:::
:::

::: practice

::: check
given: l = 10..990; m = 1000..2000
answer: 1000/(m - l)
tolerance: 0.5%

## Mean time in system, ms, for {l}/s arriving at a {m}/s server?

::: misses
- [any] $\frac{1}{\mu - \lambda}$.
:::
:::

::: check
given: rho = 0.05..0.99
answer: rho/(1 - rho)
tolerance: 0.5%

## Mean number in an M/M/1 system at load {rho}?

::: misses
- [any] $\frac{\rho}{1 - \rho}$.
:::
:::
:::
