# Latency 2: Scenarios

Source: https://obsvra.com/learn/simulation-101/simulation-lesson-latency-2

**Requires Pro** (declare and Import)

### Problem statement

The team has a candidate deploy. Its requests are a little slower in the middle (a median of 33 ms against 30) and its tail is heavier (sigma 0.87 against 0.8). The objective is the one from the last lesson: **the 99th percentile of latency must stay under 250 ms**. The team will run a simulation of the current service and of the new one, and decide from the two p99 numbers.

The questions in this lesson:

- How do you put two versions of the service in one model, so they run side by side?
- Does the new deploy meet the SLO?
- How much should you trust a "pass" or "fail" that comes from one run?

The answer to the last one is the point of the lesson. The new deploy is built to sit 0.26 ms under the line, and a run of 100,000 requests cannot tell which side of the line it is on.

### The model

The library from lesson 4.1, `samples/pro/latency-lib.xml`, is imported twice under two names. Each import brings its own copy of the five library nodes, and each can override the declared `median` and `sigma`. `samples/pro/latency-scenarios.xml` (seed 402):

```xml
<dataset columns="baselineLatency, newLatency" seed="402">
  <root node="Both"/>
  <nodes>
    <Import name="Baseline" file="latency-lib.xml" sigma="0.8"/>
    <Import name="NewDeploy" file="latency-lib.xml" median="33" sigma="0.87"/>
    <Append name="Both">
      <input name="Baseline"/>
      <input name="NewDeploy"/>
    </Append>
  </nodes>
</dataset>
```

- `Baseline` is the current service: the library's default median of 30 ms, with sigma overridden to 0.8, the realistic heavy tail the last lesson ended on. Its exact p99 is 30 x exp(2.3263 x 0.8) = 192.92 ms.
- `NewDeploy` is the candidate: median 33, sigma 0.87. Its exact p99 is 33 x exp(2.3263 x 0.87) = 249.74 ms, which is 0.26 ms under the line.
- `Append` puts the two columns side by side. One row is one request to each version of the service: column 0 is the baseline's latency and column 1 the new deploy's, both in milliseconds.

The file has three nodes. Each import is the library's five nodes, with the import's root taking the import's name, so the validator counts 11 nodes in all. The library file is unchanged: it is the same file the 4.1 models read.

```
grind --count 10 latency-scenarios.xml
```

The first ten rows with the model's own seed, 402:

| Row | Baseline (ms) | NewDeploy (ms) | Row | Baseline (ms) | NewDeploy (ms) |
|---|---|---|---|---|---|
| 1 | 93.739 | 29.652 | 6 | 47.121 | 195.387 |
| 2 | 23.976 | 23.784 | 7 | 25.504 | 20.166 |
| 3 | 57.167 | 57.846 | 8 | 31.902 | 37.124 |
| 4 | 14.710 | 54.873 | 9 | 40.085 | 12.478 |
| 5 | 34.447 | 42.371 | 10 | 76.119 | 49.799 |

The two columns do not move together. In row 1 the baseline's request is slow (93.7 ms) and the new deploy's is quick (29.7); in row 6 it is the other way round. That is deliberate and worth knowing about. A node's random numbers come from the seed and the node's full name, and the full names here differ, `Baseline.Spread` and `NewDeploy.Spread`. So the two imports draw different random numbers even though they share the seed and the library file. A check with 100,000 rows of this model finds that the correlation between the two columns' underlying normal draws (Z in `median x exp(sigma x Z)`, recovered from the output) is 0.0045, which is zero within its standard error of about 0.003. The columns are independent. Lesson 4.3 uses that.

### The first answer: the p99 of each service

A run of 100,000 requests gives 100,000 values per column. The p99 of a column is the value that 99% of the rows are below. Run the model with `--seed S` for the seeds 1 to 50, 100,000 rows each, and read the p99 of each column from the sorted rows:

```
grind --count 100000 --seed 1 latency-scenarios.xml
```

The first eight seeds, to see what a single run says:

| Seed | Baseline p99 | NewDeploy p99 | Verdict on NewDeploy |
|---|---|---|---|
| 1 | 194.73 | 253.42 | fails |
| 2 | 193.92 | 249.31 | passes |
| 3 | 196.30 | 252.03 | fails |
| 4 | 194.30 | 251.08 | fails |
| 5 | 192.21 | 245.54 | passes |
| 6 | 189.83 | 248.78 | passes |
| 7 | 194.19 | 251.77 | fails |
| 8 | 190.24 | 250.44 | fails |

In these eight runs the baseline's p99 stays between 189.8 and 196.3, far from the 250 line. The new deploy's p99 sits on both sides of it, and the model has not changed between rows of this table. Over all 50 seeds:

![One dot per seed for the p99 of the new deploy in 50 runs of 100,000 requests, against the 250 ms line and the exact p99 of 249.74 ms. Twenty-eight runs fall under 250 ms and pass, twenty-two over it and fail, scattered around the line without a pattern](https://obsvra.com/lesson-assets/figures/sim101-17-verdict.svg)

| 50 seeds of 100,000 rows | Measured: mean (sd over seeds) | Exact | Standard error of one run's p99 |
|---|---|---|---|
| Baseline p99 | 192.75 (1.79) | 192.92 | 1.82 |
| NewDeploy p99 | 249.53 (2.40) | 249.74 | 2.57 |

- The baseline passes in all 50 runs.
- The new deploy **passes in 28 runs and fails in 22**.
- The average over the 50 runs is on the right value (249.53 against 249.74, within one standard error of the average, which is 2.40 / sqrt(50) = 0.34): the runs are not biased, they are noisy.

The "standard error of one run's p99" column is the theory for how much a p99 from n rows varies. A sample percentile has standard error `sqrt(p(1-p)/n) / f`, where p = 0.99 and f is the density of the latency at the true p99. At this tail f is small: the new deploy's density at 249.74 ms is 0.000123 per ms (a lognormal's density at its p99 is the standard normal density 0.0267, divided by sigma x p99 = 0.87 x 249.74). So the standard error is sqrt(0.0099 / n) / 0.000123 = 811 / sqrt(n) ms: 2.57 ms for 100,000 rows. The measured spread over seeds is 2.40, about 6% under the theory; a standard deviation computed from 50 values has a standard error of about 10%, so that is ordinary.

A normal distribution with mean 249.74 and standard deviation 2.57 puts 46% of its weight above 250, because 250 is only 0.1 standard deviations above the mean. So a single run has about a 46% chance of failing a service that, in truth, passes. A check with 500 more seeds (1001 to 1500): 242 of 500 failed, 48.4% (standard error 2.2%), consistent with 46%.

### More rows does not settle it

The first thing to try is more rows. Run the same 50 seeds at 10,000 and at 1,000,000 rows as well:

| Rows per run | NewDeploy p99: mean, sd over 50 seeds | Theory sd | Passes / fails of 50 | Chance of a fail (theory) |
|---|---|---|---|---|
| 10,000 | 248.38, 7.55 | 8.11 | 32 / 18 | 49% |
| 100,000 | 249.53, 2.40 | 2.57 | 28 / 22 | 46% |
| 1,000,000 | 249.51, 0.83 | 0.81 | 38 / 12 | 37% |

The spread falls with the square root of the rows: 100 times the rows buys 10 times the precision. But the line is only 0.26 ms from the truth, and even at a million rows the standard error (0.81 ms) is three times that distance. The verdict still flips: 12 of 50 runs fail at a million rows. The coin gets less fair slowly: the chance of a fail goes from 49% to 37% while the work goes up by a factor of 100.

How many rows would settle it? Say "settled" means the line is two standard errors from the estimate, so a 95% interval for the p99 does not contain 250. The standard error has to be 0.26 / 2 = 0.13 ms. Solving 811 / sqrt(n) = 0.13 gives n = (811 / 0.13)^2, about 39 million rows (if the estimate were exactly on the truth). That is the number to remember: for a service this close to its limit, a verdict at 95% takes tens of millions of requests, 400 times the 100,000 above.

That is cheap enough to try here: 8 runs of 5,000,000 rows (seeds 9001 to 9008), the NewDeploy column only (`--node NewDeploy`), pooled into 40,000,000 rows. The p99 is **249.67 ms**, with a standard error of 0.128 ms (theory; the pooled run is one measurement). A 95% interval is 249.42 to 249.92. The line is 0.33 ms above the estimate, 2.6 standard errors: the new deploy passes, and the evidence is now clear enough to say so. The estimate is 0.07 ms under the exact value (0.5 standard errors). In the 40 million rows, 0.996% were above 250 ms (exact 0.997%); the SLO allows 1%.

So the report on the new deploy is not best expressed as "pass" or "fail". The bottom line really is: the p99 is about 249.7 ms, within a fraction of a millisecond of the limit, and no short run can say which side it is on. The deploy passes by 0.3 ms. That is a margin so narrow that nobody should ship on it, whatever the arithmetic says.

### What a difference does better

The team's decision is really a comparison: is the new deploy worse than the current service? The difference of the two p99s answers that, and it behaves differently. In each of the 50 runs, subtract the baseline's p99 from the new deploy's:

![Dot plots of the difference of the two p99s over 50 runs of 100,000 requests, for the main change (all 50 differences between 48 and 63 ms, centered on 56.8, exact 56.82) and for a small change, median 31 sigma 0.8 (the differences spread around 6.4, exact 6.43, with one of the 50 below zero)](https://obsvra.com/lesson-assets/figures/sim101-17-difference.svg)

| 50 seeds, 100,000 rows | Difference: mean (sd) | Exact | Theory sd | Wrong sign |
|---|---|---|---|---|
| Main change (33 ms, 0.87) | 56.78 (3.05) | 56.82 | 3.15 | 0 of 50 (500 more seeds: 0 of 500) |
| Small change (31 ms, 0.8) | 6.44 (2.56) | 6.43 | 2.62 | 1 of 50 (500 more seeds: 3 of 500) |

For the main change, the difference is 56.8 ms in every run (the smallest is 48.5, and the spread is 3.05). It is 18 standard deviations from zero, so "the new deploy has a worse tail than the baseline" is a safe statement from a single run. The question "is the deploy over 250?" is a hard one, but "is it worse than the baseline?" is not.

The small change (median 31 ms, sigma 0.8, so the same tail shape as the baseline with the median 3% higher) is a harder comparison. Its p99 is 199.35 against the baseline's 192.92, a difference of 6.43 ms, and both pass the SLO in all 50 runs (the new deploy's p99 ranges from 195.5 to 202.7). But the difference has a standard deviation of 2.56 (theory 2.62), which is 40% of the difference itself, and one run in 50 has the wrong sign: seed-level noise can make a service that is 3% slower look 0.9 ms faster in the tail. The normal approximation gives the chance as 0.7% per run (6.43 / 2.62 = 2.45 standard deviations below the mean); the measurement over 550 seeds is 4 of 550, 0.7%. At 10,000 rows per run the same comparison has the wrong sign in 14 of 50 runs.

The noise in the difference has two parts, one from each column. The standard errors of the two p99s, 1.82 and 2.57 (main case), combine as the root of the sum of the squares, 3.15, and that is the difference's spread. Both columns contribute because the columns are independent. A comparison can be fairer if the two columns share their noise, and lesson 4.3 shows how to do it.

### Check: do the numbers match the math?

The exact p99 of a lognormal is `median x exp(2.3263 x sigma)`: 30 x exp(2.3263 x 0.8) = 192.92 for the baseline, 33 x exp(2.3263 x 0.87) = 249.74 for the new deploy, 31 x exp(2.3263 x 0.8) = 199.35 for the small change. The measured means over the 50 seeds at 100,000 rows are 192.75, 249.53 and 199.19, each within one standard error of the mean of its exact value (the standard errors are 0.25, 0.34 and 0.25). The baseline's means at 10,000 and 1,000,000 rows (192.39 and 192.83) are also within a standard error of exact. The new deploy's are 248.38 and 249.51, which are 1.3 and 2.0 standard errors of the mean below 249.74; over the 500 further seeds at 100,000 rows the mean is 249.82 (standard error 0.11, 0.7 above exact), so the two low ones are best read as chance, not bias. The spreads over seeds, 1.79, 2.40 and 1.76 at 100,000 rows, are 98%, 93% and 94% of the theory for a sample p99, the last two off by an amount that a spread from 50 values often is. At 1,000,000 rows the measured and theoretical spreads for the new deploy are 0.83 and 0.81. The two columns of the model, drawn from the seed and two different node names, are uncorrelated (0.0045 over 100,000 rows, standard error 0.003). The 40,000,000-row pooled p99, 249.67, is 0.5 standard errors from the exact 249.74.

**What this gives you:** a comparison of two versions of a service in one model: two imports of the same library under different names, with different overrides, put side by side by `Append`, three nodes in the file. You have seen that the baseline's p99 is 192.9 ms (measured 192.75 over 50 seeds) and the new deploy's is 249.74 (measured 249.53), that the new deploy passes the SLO in 28 of 50 runs of 100,000 rows and fails in 22, that a run of a million rows still fails 12 of 50, and that settling a 0.26 ms gap takes about 39 million rows (a measured p99 of 249.67 +/- 0.25 at 40 million). A single pass/fail from one run is a coin flip when the truth is that close to the line, so report an interval and say "just under the line". A difference between two versions is more robust than a verdict against a fixed line: the main change is 56.8 ms worse in every run, while a 6.4 ms change still has the wrong sign in 1 run in 50 at 100,000 rows. The [companion page](https://obsvra.com/lesson-assets/companions/sim101-17.html) lets you pick the change (main or small) and the rows per run (10,000, 100,000 or 1,000,000), and shows the p99 of the 50 seeds against the line, the pass/fail count and the spread of the difference.

The noise in the difference came from two independent samples. The baseline and the new deploy were tested on different random requests, so some of the difference between their p99s is luck about which requests each one got. The next lesson gives both versions the same requests (the same random numbers, scaled by each version's median and sigma) and measures how much of the noise disappears. This technique is called common random numbers.
