G/G/1 vs. G/G/c – Kingman and Multiple Servers
A digression accompanying the Scalability series.
Photo: Edwin Petrus @ Unsplash - Unsplash licence
The connection pool of the search service from Kingman’s formula: phantom traffic jams in the queue has fifty connections. Suppose it runs at 80% utilization, and a query holds its connection for 100 milliseconds on average. Kingman’s formula predicts a waiting time of four times the service time for this case, as the table in the main article shows. A request would queue for 400 milliseconds before it even gets a connection.
In reality, it waits less than a millisecond on average. The formula is off by a factor of more than 400. Kingman did not get his sums wrong, though – he simply described a different system. That is no license to run pools at high utilization, though.
One checkout or fifty counters
Kingman’s formula applies to a queue in front of a single server. In the shorthand of queueing theory, such a system is called G/G/1: arbitrarily distributed arrivals, arbitrarily distributed service times, one server. That is the supermarket with only one checkout open. If the customer in front of me takes a long time, I stand and wait, and there is no way past them.
A pool of fifty connections is a G/G/c system with c = 50. That is the check-in hall at the airport: one queue, fifty counters. Here, you only wait if you arrive while all fifty are busy. At 80% utilization, forty are busy on average, and fewer than one request in ten has to wait at all. A slow passenger blocks one counter out of fifty – the others carry on working.
The first person to work this out was the Danish mathematician Agner Krarup Erlang. He wanted to know how many lines a telephone exchange needs so that callers rarely have to wait for a free one. His formula from 1917, known today as Erlang C, gives the waiting time for multiple servers with random arrivals and exponentially distributed service times. In Kingman’s formula, that corresponds to a variability of V = 1.
The mean waiting time as a multiple of the service time, for V = 1 in each case:
| Utilization | 1 server | 10 servers | 50 servers |
|---|---|---|---|
| 70% | 2.3 | 0.07 | < 0.001 |
| 80% | 4 | 0.2 | 0.009 |
| 90% | 9 | 0.7 | 0.07 |
| 95% | 19 | 1.7 | 0.25 |
| 98% | 49 | 4.6 | 0.84 |
The left-hand column is Kingman. The right-hand column is the search service’s pool: at 80%, a request waits just under a hundredth of the service time on average – 0.9 milliseconds for a service time of 100 milliseconds.
What remains of Kingman
The shape of the curve remains. Even with fifty servers it stays flat for a long time and then turns steep; the hockey-stick curve has merely slid to the right. Between 90% and 98% utilization, the waiting time in the pool of fifty grows more than tenfold.
Variability keeps working, too. For multiple servers there are approximations that are built exactly like Kingman’s formula; Hopp & Spearman use them to calculate entire production lines. The only part they swap is the one that depends on utilization: it also takes the number of servers into account. Variability enters as a factor, unchanged. So less variability means less waiting in a pool as well. Close to the saturation point, halving the variability halves the waiting time; further below it, a large pool gains somewhat less.
The pool therefore buys a buffer and nothing more. And it has an unpleasant side effect: the curve bends later, but more sharply. A single server becomes noticeably slower at just 50% utilization and thus gives an early warning. A large pool shows nothing until shortly before the end, and then a few more percentage points of load are enough.
Why pools fill up anyway
Fifty connections is already a large pool. In practice pools tend to be smaller, because every connection costs something on the database side: PostgreSQL, for instance, starts a separate process for each one and by default allows only a hundred in total. All instances of all services attached to the same database share that budget. The widely used pool HikariCP ships with a default of ten connections. For many services, then, it is the middle column of the table that applies, and there four out of ten requests already have to wait at 80% utilization.
The table also assumes a fixed service time. A database under load responds more slowly, though, and every query holds its connection for longer. By Little’s law, the number of connections in use then rises without a single additional request arriving. If every query holds its connection for just under 130 milliseconds instead of 100, 70% utilization turns into 90%, and the pool slides into the steep part of the curve all by itself. A larger pool is therefore no way out either: the more queries the database handles at the same time, the longer each one takes.
And finally, the utilization on the dashboard is an average over minutes, whereas queues form within seconds. A pool that averages 70% can be fully occupied for several seconds during a burst or a cluster of expensive queries. It is in exactly those seconds that requests pile up. The average utilization hides this just as the average response time does.
The average is therefore a poor early-warning signal. The time requests spend waiting for a connection is a better one, as is the number of threads currently queuing; HikariCP, for one, exposes both as metrics. If these values rise, the buffer is used up, whatever the dashboard says. And anyone sizing a pool does better to work with the load peak and the slow query than with the average.
Where the single server really sits
Almost every system has places where Kingman’s formula applies without any caveats, because only one unit of work can really proceed there at a time. A lock on a frequently updated database row is such a place, as is a mutex around a shared data structure. A component that does its work in a single thread belongs here too.
In front of such a place, fifty connections are no help. All fifty requests then stand in the same queue in front of the same lock, and the left-hand column of the table applies. This is the serial fraction that Amdahl’s Law and the Limits to Growth is about.
This yields a useful diagnostic hint. If response times are already rising markedly although the pool still has plenty of free connections, then nobody is waiting for the pool. Somewhere behind it there is a single server, and looking for it pays off more than a larger pool.
Which formula for what
Little’s law sizes the pool. $L = \lambda \times W$ holds for every stable system, no matter how many servers it has: 500 requests per second times 70 milliseconds makes 35 connections in use at the same time. That is not an approximation. Little’s Law in Practice shows what this looks like in a real system.
How much headroom the pool needs beyond that is answered more precisely by Erlang C than by Kingman. The formula is a little unwieldy, though (ρ is the utilization, S the service time, c the number of servers):
\[\text{Waiting time} = \frac{\dfrac{a^c}{c!} \cdot \dfrac{1}{1-\rho}}{\displaystyle\sum_{k=0}^{c-1} \dfrac{a^k}{k!} + \dfrac{a^c}{c!} \cdot \dfrac{1}{1-\rho}} \cdot \frac{S}{c \cdot (1-\rho)} \qquad \text{with } a = c \cdot \rho\]Fortunately, nobody does this by hand: call centers have been planning their shifts with it for decades, and Erlang C calculators are correspondingly easy to find online. For a rough estimate, the table above will do, read at the utilization during the load peak. Erlang C gives no more than an estimate either, because the formula knows nothing of timeouts and retries.
Kingman remains the formula for understanding – and for every place where only one can really work at a time. It fits on a beer mat and shows which three quantities drive the waiting time. That is why it appears in the main article even though the pool there has fifty connections. Just don’t trust its milliseconds when it comes to a large pool. With ten connections, four out of ten requests already have to wait at 80%, and there Kingman’s warning about the 80% mark is justified again.
What should happen to the requests that then queue in front of the pool is the subject of Backpressure – When Systems Learn to Say No.
Sources
- Kingman (1961) – The single server queue in heavy traffic. Mathematical Proceedings of the Cambridge Philosophical Society, 57(4).
- Erlang (1917) – Solution of some problems in the theory of probabilities of significance in automatic telephone exchanges. Elektroteknikeren, 13.
- PostgreSQL: Connection Establishment – one server process per connection.
- HikariCP: About Pool Sizing – recommendations on pool size.
- Hopp & Spearman (2011) – Factory Physics. 3rd ed. Waveland Press. Queue time at stations with several parallel machines.
Comments