Latency 1: A Reusable Baseline
Requires Pro (declare and Import)
Problem statement
A team runs a web service and is about to ship a change. Before it ships they want to know two things: will the change push the service over its latency target, and would their alerting notice if it did. The target, the service level objective (SLO), is a statement about the slow end of the traffic: 99% of requests complete in under 250 ms. This module answers both questions, one lesson at a time, and every lesson needs the same thing underneath: a model of how long one request takes.
This first lesson builds that model once, as a file everything else can reuse. The questions here are modest:
- What does a latency distribution look like, and which numbers describe it?
- How do you write the service’s latency in one place and still use it with different numbers (a different median, a different tail), without copying the file?
- Is the baseline service anywhere near the 250 ms line?
The model
Request latency is skewed. Most requests are close to a typical time, a few are several times slower, and none is faster than zero. The usual model is a lognormal: the logarithm of the latency is normal. It has two numbers. The median is the typical request: half of all requests are faster. Sigma sets how heavy the slow tail is: the logarithm of the latency has standard deviation sigma, so each increase of sigma by 0.1 stretches the far tail by a factor of about 1.26 at the 99th percentile.
The service is described by those two numbers, and they are the ones a later lesson will change, so they go in a library file with two declared parameters. declare registers a named default; any value written exactly as $name in the file’s nodes is replaced by it, and whoever imports the file can override it. A declare replaces a whole attribute value and cannot compute, which matters here: a Lognormal node wants mu, the log of the median, and mu="ln($median)" is not allowed. So the library draws the standard shape, exp(sigma x Z) with Z a standard normal (a lognormal with mu = 0), and multiplies it by the median. The result, median x exp(sigma x Z), is exactly a lognormal with median median and log-scale spread sigma. Here is samples/pro/latency-lib.xml, five nodes:
<dataset columns="latency">
<root node="Latency"/>
<definitions>
<declare name="median" value="30"/>
<declare name="sigma" value="0.3"/>
</definitions>
<nodes>
<Constant name="Median" value="$median" type="double"/>
<Lognormal name="Spread" mu="0" sigma="$sigma"/>
<Append name="Pair">
<input name="Median"/>
<input name="Spread"/>
</Append>
<Calculate name="Scaled" expression="y[0] = x[0] * x[1]">
<input name="Pair"/>
</Calculate>
<Arrange name="Latency" columns="0">
<input name="Scaled"/>
</Arrange>
</nodes>
</dataset>
Median is a constant, Spread is the standard shape, Pair puts the two side by side, Scaled multiplies them, and Arrange keeps the product as the one output column. The defaults are a median of 30 ms and a sigma of 0.3. The library has no seed: it is a part, and the model that imports it decides the seed.
The model that uses it, samples/pro/latency-baseline.xml, is one Import, seed 401:
<dataset columns="latency" seed="401">
<root node="Sys"/>
<nodes>
<Import name="Sys" file="latency-lib.xml"/>
</nodes>
</dataset>
Import brings the library’s five nodes in under the name Sys (so they are Sys.Median, Sys.Spread and so on), and the import’s root, Latency, becomes the node Sys. With no overriding attributes, the defaults apply. One row is one request, and the value is its latency in milliseconds. A Free model has no parameters, so every variant of this service would have to be its own file; here there is one source of truth and the variants are one-line imports.
grind --count 10 latency-baseline.xml
The first ten rows with the model’s own seed, 401 (you get exactly these if you leave the seed alone):
| Row | Latency (ms) | Row | Latency (ms) |
|---|---|---|---|
| 1 | 36.349 | 6 | 35.237 |
| 2 | 20.184 | 7 | 26.298 |
| 3 | 18.294 | 8 | 40.527 |
| 4 | 37.180 | 9 | 28.604 |
| 5 | 31.260 | 10 | 29.445 |
A hand check of the mechanism. grind --count 2 --node Sys.Pair latency-baseline.xml prints what Pair hands to Scaled, the median and the standard shape: 30.0 and 1.21162 in row 1, 30.0 and 0.67280 in row 2. Then 30 x 1.21162 = 36.349 and 30 x 0.67280 = 20.184, the first two latencies above. (The standard normal draw behind row 1 is ln(1.21162) / 0.3 = 0.64.) For a different set of requests each time, leave the seed out: grind --no-seed --count 1000000 latency-baseline.xml.
What a latency distribution looks like
Running the model for 1,000,000 requests with its own seed (401) gives this histogram, on a linear axis and on a logarithmic axis:
On the linear axis the histogram has a steep rise to a peak near 27 ms and a tail that trails off to the right: some requests take twice the median, a few take 2.5 times as long. On the logarithmic axis the same data is a symmetric bell, which is what “the logarithm is normal” means. Latency data from real services nearly always has this shape, and it is why a table of its numbers needs more than one number.
| Measured (1,000,000 requests) | Exact | |
|---|---|---|
| Mean | 31.38 ms (SE 0.010) | 31.38 |
| p50 (median) | 29.99 ms (SE 0.011) | 30.00 |
| p95 | 49.13 ms (SE 0.031) | 49.14 |
| p99 | 60.35 ms (SE 0.068) | 60.29 |
| p99.9 | 75.64 ms (SE 0.213) | 75.81 |
The p99 (99th percentile) is the latency that 99% of requests beat. The slowest 1% take longer. The mean is a little above the median (31.4 against 30.0): the tail drags it. At this sigma the mean is still a fair summary. It stops being one as the tail grows. The same library with sigma 0.8, the heavy tail of the next lessons, has a mean of 41.3 ms (exact 41.31), a median of 30.0 and a p99 of 192.5 (exact 192.92); this is a run of 1,000,000 requests with seed 4108, not a shipped model. A report of “average latency 41 ms” hides that one request in a hundred takes about 190 ms or more: in a million requests, 10,000 of them. Below is that service against the baseline, with the same median:
This is why an SLO talks about percentiles and not means. Users feel the slow requests, a page that needs ten calls is slow if any one of them is, and the mean averages the slow ones away among the fast ones. The median says what a typical request feels like; the p99 says how bad the bad ones are; and in this lesson’s two services the median is the same and the p99 differs by a factor of three. Everything in this module is about that tail.
The SLO question for the baseline
The baseline has a p99 of 60.3 ms measured (exact 60.29) and a p99.9 of 75.6, far under the 250 ms line. At this sigma, the service passes with a wide margin: the 250 ms line is 4.1 times the p99. In the 1,000,000 requests, 30 were slower than 100 ms (0.003%, exact 0.003%) and none was slower than 250 ms. If a real service looked like this, nobody would worry about a deploy that made it 10% slower. A realistic service has a heavier tail than this, and lessons 4.2 and after use sigma 0.8, whose exact p99 is 192.9 ms: closer to the line, so that small changes decide the SLO. The baseline here is only the clean case.
Overriding without changing the library
The point of the library is that the same file serves a different service. samples/pro/latency-override.xml imports it with both parameters overridden, median 33 ms and sigma 0.87, seed 402:
<dataset columns="latency" seed="402">
<root node="Sys"/>
<nodes>
<Import name="Sys" file="latency-lib.xml" median="33" sigma="0.87"/>
</nodes>
</dataset>
Each attribute on the Import replaces the matching declare for this one import. An attribute with no matching declare (say medain="33") is a load error, so a typo cannot silently leave the default in place. The library file is the same file, not edited, and the baseline model above still reads it with the defaults. Running 1,000,000 requests with seed 402:
| Measured | Exact | |
|---|---|---|
| Mean | 48.19 ms (SE 0.051) | 48.18 |
| p50 | 33.04 ms (SE 0.036) | 33.00 |
| p95 | 138.05 ms (SE 0.254) | 138.04 |
| p99 | 249.61 ms (SE 0.811) | 249.74 |
| p99.9 | 479.0 ms (SE 3.96) | 485.4 |
A median of 33 and a sigma of 0.87 put the p99 at 249.74 ms (exact: 33 x exp(2.3263 x 0.87)), which is 0.26 ms under the 250 line. The measured p99 is 249.6, with a standard error of 0.8, so this run cannot tell which side of the line the truth is on, and 1.0% of the requests (0.993% measured) are over 250 ms, the share the SLO allows. This is a service that is right at its limit, and it is the one the next lesson compares with the heavy-tailed baseline. Whether it passes is a question about noise, and lesson 4.2 measures how much.
What sigma puts the p99 at 100 ms?
A common question runs the model backwards: given a target p99, how heavy a tail may the service have? The p99 of this lognormal is median x exp(sigma x 2.3263), so for a median of 30 ms and a p99 of 100 ms, sigma = ln(100 / 30) / 2.3263 = 0.5175. The library was imported with median="30" sigma="0.5175" (a temporary file, seed 403, not shipped) and run for 1,000,000 requests: the p99 came out at 100.44 ms (standard error 0.19), against an exact 99.99. That is 2.3 standard errors above the exact value. It is one of two comparisons beyond 2 standard errors among the 90 generated for this lesson (the other is -2.4, a p99.9 in the companion’s grid), and about 4 of 90 is what chance produces; the measured share over 100 ms is 1.025% against the exact 1%. The p99 as a function of sigma, with the measured values of these runs:
The curve climbs fast: a tail that is a little heavier (sigma 0.5 to 0.8) takes the p99 from 96 ms to 193. A median 10% higher lifts the whole curve by 10%; a change in sigma moves it much more.
Check: do the numbers match the math?
The exact percentiles of the lognormal are median x exp(sigma x z), with z = 0, 1.645, 2.326 and 3.090 for p50, p95, p99 and p99.9 (the table above gives the exact value beside each measured one). Over the baseline, the override and the 100 ms run, the fifteen percentile comparisons (five percentiles each, the p90 included) are within 1.7 standard errors of exact, apart from the 2.3 noted above. Across those three runs and the 15 runs of the median-by-sigma grid behind the companion page, the 90 comparisons have a largest |z| of 2.4 and a root-mean-square of 0.96 (it would be about 1 if the measurements and the formula agreed exactly). The logarithm of the baseline’s latencies has mean -0.0001 and standard deviation 0.2999 over the 1,000,000 rows (0 and 0.3 exactly), which shows that the library’s median x exp(sigma x Z) is a lognormal with the declared parameters. The mean agrees with median x exp(sigma^2 / 2) = 31.381 (measured 31.376, SE 0.010); the standard deviation of the latencies, 9.63 ms, with the exact 9.63. Standard errors for the percentiles are sqrt(p(1-p)/n) / f, with f the density of the distribution at that percentile.
What this gives you: a latency model as a reusable library, in five nodes: two declare parameters, median and sigma, a Constant and a standard-shape Lognormal multiplied together because declare cannot compute ln(median), and a model that is a one-line Import with the defaults (median 30 ms, sigma 0.3) or with overrides (median 33, sigma 0.87), the same file in both. You have seen what a latency distribution looks like (a right-leaning bell that is symmetric on a log axis), why SLOs speak in percentiles (the mean of the sigma 0.8 service is 41 ms and its p99 is 193), that the baseline passes the 250 ms SLO with p99 60.3 ms, that median 33 and sigma 0.87 put the p99 at 249.74 ms, and that sigma 0.5175 puts it at 100 ms (measured 100.4). The exact and the measured percentiles agree within 2.4 standard errors in all 90 comparisons. The companion page lets you pick the median (20, 30 or 50 ms) and sigma (0.2 to 1.0), and shows the percentiles beside their exact values and the histogram on a linear or a logarithmic axis, from 1,000,000 simulated requests for each choice.
The baseline is too gentle to be interesting; a real service has a tail like sigma 0.8, with a p99 of 192.9 ms, and a new deploy that shifts it slightly can push the p99 across 250 ms. The next lesson puts both in one model, the baseline and the deploy, imports the library twice with different parameters, and asks the question the team actually has: does the new deploy breach the SLO? It turns out that a single run of a hundred thousand requests can give either answer.