Latency 5: Replaying History
Requires Pro (FlatFile, declare and Import)
Problem statement
Everything so far has been made-up traffic: a stream of requests drawn from a distribution. The team also has the real thing. Their monitoring system records how many requests the service handled in every minute, and one day of that record is a file with 1,440 numbers. The question they actually care about is concrete: would that day have breached the SLO under the new deploy? The SLO is the one from lesson 4.2, a p99 under 250 ms, which means that at most 1% of requests may be slower than 250 ms.
The questions in this lesson:
- How do you replay recorded traffic through a model, row by row, without copying production data into it?
- Would that recorded day have breached the SLO under the new deploy, and under the current service?
- How do you ask the growth question, “what if traffic were 20% higher?”, of the same recording?
This is trace-driven simulation. The trace carries the part of the world you have measured (when the traffic came), and the model carries the part you have to assume (how latency responds to it). The trace in this lesson is invented: it was written by a script (it is reproduced inside learn/figures/sim101-20-make.py), as a stand-in for an export from a monitoring system. Its shape is made up, and so every conclusion below is about this made-up day. What carries over to your own data is the method. Note what a trace of this kind holds: a count per minute and nothing else. No user, no request, no payload has to be copied into the model for the replay to work.
The model
Two files and a data file, all in samples/pro/:
latency-traffic-trace.csv: the recorded day. A header,requests, and then 1,440 lines, one per minute from 00:00: 303, 322, 315, 308, 296, and so on. It is an input, so unlike the recorded outputs next to the other samples it has no# seed=line.latency-slo-lib.xml: a library, like the one in lesson 4.1, that turns one minute of traffic into the share of requests that breach the SLO. It declaresmedian(30),sigma(0.8) andslo(250).latency-replay.xml: the model, which imports the library twice, once for each version of the service.
The library:
<dataset columns="requests, slowShare, slow">
<root node="Out"/>
<definitions>
<declare name="median" value="30"/>
<declare name="sigma" value="0.8"/>
<declare name="slo" value="250"/>
</definitions>
<nodes>
<FlatFile name="Trace" file="latency-traffic-trace.csv" skipHeader="true" mode="sequential" onExhaustion="repeat"/>
<Constant name="Median" value="$median" type="double"/>
<Constant name="Sigma" value="$sigma" type="double"/>
<Constant name="Slo" value="$slo" type="double"/>
<Append name="Params">
<input name="Trace"/>
<input name="Median"/>
<input name="Sigma"/>
<input name="Slo"/>
</Append>
<Calculate name="Z" dimension="8" expression="y[4] = (log(x[3]) - log(x[1] * (1 + 0.5 * pow(x[0] / 3000, 2)))) / x[2]">
<input name="Params"/>
</Calculate>
<Calculate name="T" expression="y[5] = 1 / (1 + 0.2316419 * abs(x[4]))">
<input name="Z"/>
</Calculate>
<Calculate name="Share" expression="y[6] = 0.5 - x[4] / (abs(x[4]) + 0.000000000001) * (0.5 - 0.3989422804014327 * exp(-0.5 * x[4] * x[4]) * (((((1.330274429 * x[5] - 1.821255978) * x[5] + 1.781477937) * x[5] - 0.356563782) * x[5] + 0.31938153) * x[5]))">
<input name="T"/>
</Calculate>
<Calculate name="Slow" expression="y[7] = x[0] * x[6]">
<input name="Share"/>
</Calculate>
<Arrange name="Out" columns="0,6,7">
<input name="Slow"/>
</Arrange>
</nodes>
</dataset>
The model, samples/pro/latency-replay.xml (seed 405):
<dataset columns="baselineRequests, baselineSlowShare, baselineSlow, newRequests, newSlowShare, newSlow" seed="405">
<root node="Both"/>
<nodes>
<Import name="Baseline" file="latency-slo-lib.xml"/>
<Import name="NewDeploy" file="latency-slo-lib.xml" median="33" sigma="0.87"/>
<Append name="Both">
<input name="Baseline"/>
<input name="NewDeploy"/>
</Append>
</nodes>
</dataset>
The replay. FlatFile reads the CSV into memory and hands out its rows. Three of its settings matter here:
mode="sequential"gives the rows in file order, one per output row. The other two modes sample rows at random, uniformly or by a weight column (unweightedis the default). A replay needs the order, because the order is the time of day.skipHeader="true"skips therequestsline. The file’s columns are numbered from 0 in the model whatever they are called; the names are only used byskipHeaderand by settings such astextColumns. This file has one column, so the node outputs one number, the minute’s request count, and cells that look like numbers are read as numbers.onExhaustionsays what happens when the file runs out.repeatstarts again from the first row, so a count of 2,880 replays two days.error, the default, stops the run after the last row.holdkeeps repeating the last row, andnullproduces empty rows. Running all four with a count of 1,443:repeatgives row 1,441 equal to row 1 (303 requests),errorprints the 1,440 rows and then stops with “FlatFile ‘Trace’: sequential data exhausted”,holdprints 291 requests, the last row, three more times,nullprints 1,440 rows and then stops, because theCalculateafter it cannot do arithmetic on an empty value.
One row is one minute. Each import reads the file on its own, from the first row, so both services see the same day.
The assumption. A trace gives request counts, not latencies, so the model has to say how latency depends on load. If it did not, every minute would be the same: with a fixed median, a minute’s share of slow requests is the same number at 03:00 and at 14:16 (0.402% for the baseline, 0.997% for the new deploy), and replaying a day would tell you nothing about the day. So the model assumes that the median latency rises with the number of requests in the minute:
median(load) = median x (1 + 0.5 x (requests per minute / 3000)^2)
The service is taken to have a capacity near 3,000 requests a minute, where the median is 1.5 times its idle value. At a quiet 300 a minute the median is 30.15 ms instead of 30; at 4,684 requests it is 2.2 times as large. This is an assumption of the lesson, not a measurement. The shape of the curve and the 3,000 are both made up, and the numbers below depend on them. With real monitoring data you would fit it from data (latency against load), or take it from a load test.
Computing the share. Given the minute’s median and sigma, the requests of that minute are lognormal, so the share slower than the SLO is exact and no random number is needed: it is the survival function of the lognormal, 1 - Phi(z), where z = (ln 250 - ln median(load)) / sigma and Phi is the standard normal distribution function. Here is how the nodes get there:
Params:Appendputs the minute’s request count and the three declared parameters in one row of four columns.Zcomputes z for the row, using the load-dependent median above. It also hasdimension="8", which makes its row eight columns wide: the four it received and four more, which are 0 until a calculation writes into them.Calculatewrites into columns that exist, and it cannot add one by itself; this is where the room forT,ShareandSlowis made. Without it the finalArrangefails with “column 6 is out of range”.TandSharecompute 1 - Phi(z).Calculatehas no normal distribution function, soShareuses a standard polynomial approximation (Abramowitz and Stegun 26.2.17): for z of 0 or more, 1 - Phi(z) is the normal density at z times a five-term polynomial in t = 1 / (1 + 0.2316419 z), and for a negative z it is one minus the value at |z|. The division byabs(z) + 0.000000000001is only a sign function that picks between the two cases. The approximation is good to 7.5e-8, and over the day the largest measured difference from the exact value is 6.7e-8 for the baseline and 7.0e-8 for the new deploy.Slowmultiplies the share by the request count, giving the number of slow requests in the minute.Outkeeps three columns: the minute’s requests, the share, and the slow requests.
The model has two imports, so its columns are: 0 to 2 for the baseline (requests, share slower than 250 ms, slow requests) and 3 to 5 for the new deploy. As in lesson 4.2, each import brings the library’s 10 nodes and its root takes the import’s name, so the validator counts 21 nodes with Append. Run a day with:
grind --count 1440 latency-replay.xml
The first five minutes (00:00 to 00:04):
| Minute | Requests | Baseline share | Baseline slow | NewDeploy share | NewDeploy slow |
|---|---|---|---|---|---|
| 00:00 | 303 | 0.004097 | 1.241 | 0.010125 | 3.068 |
| 00:01 | 322 | 0.004107 | 1.322 | 0.010145 | 3.267 |
| 00:02 | 315 | 0.004103 | 1.293 | 0.010138 | 3.193 |
| 00:03 | 308 | 0.004100 | 1.263 | 0.010130 | 3.120 |
| 00:04 | 296 | 0.004094 | 1.212 | 0.010118 | 2.995 |
Because nothing in the model is random, the seed changes nothing. When running the full day with the model’s own seed (405), with --seed 1 and with --seed 99, all three outputs are identical in every one of the 1,440 rows and six columns. The seed="405" in the file is there only because every sample ships with one. A replay is the opposite of the earlier lessons in one respect: there is no run-to-run variety to show, and no standard error to quote. The uncertainty has moved out of the sampling and into the assumptions and into the question of whether another day would look like this one.
The recorded day
The day has 997,041 requests, a quiet night of about 300 a minute (301 on average from 00:00 to 05:59), a daytime hump that peaks near 1,550, a smaller bump in the evening near 20:30, and a flash crowd from 14:10 to 14:29: 20 minutes holding 78,343 requests (7.9% of the day), 16 of them above 3,000 a minute, peaking at 4,684 in the minute starting at 14:16 (row 857). The quietest minute is 00:12 with 262 requests. All of this was written into the trace on purpose.
Would that day have breached the SLO?
A word on what a breach is, because there are two honest readings. The SLO is a p99 under 250 ms, which means at most 1% of requests slower than 250 ms.
- A minute breaches the SLO when more than 1% of that minute’s requests are slower than 250 ms. This is the column
sharecompared with 0.01. - The day breaches it when more than 1% of all the day’s requests are slower than 250 ms: the sum of the slow-request column divided by the sum of the request column. It is a weighted average of the minutes, weighted by traffic, and it is not the mean of the 1,440 shares: that mean is 0.495% for the baseline and 1.166% for the new deploy, because the quiet minutes would count as much as the busy ones.
Both can be read straight off the printed rows. For example, with awk on the output of the run above (columns 1, 2 and 3 are the baseline’s requests, share and slow requests, 4, 5 and 6 the new deploy’s):
grind --count 1440 latency-replay.xml | awk -F, '{r+=$1; s+=$3; r2+=$4; s2+=$6; if ($2>0.01) a++; if ($5>0.01) b++} END {print 100*s/r, 100*s2/r2, a, b}'
This prints 0.7486, 1.5769, 18 and 1,440.
| The recorded day, 997,041 requests | Baseline (30 ms, sigma 0.8) | NewDeploy (33 ms, sigma 0.87) |
|---|---|---|
| Share of the day’s requests slower than 250 ms | 0.749% (7,464 requests) | 1.577% (15,722 requests) |
| Minutes with a share above 1% | 18 (14:11 to 14:28) | 1,440 (all) |
| Minutes with a share above 5% | 0 | 14 (14:13 to 14:26) |
| Lowest minute (00:12, 262 requests) | 0.408% | 1.009% |
| Highest minute (14:16, 4,684 requests) | 4.906% (229.8 slow requests) | 7.906% (370.3 slow requests) |
Yes: that day would have breached the SLO under the new deploy, by either reading, and the flash crowd is not the whole story. The new deploy’s p99 with no load at all is 33 x exp(2.3263 x 0.87) = 249.74 ms, which is 0.26 ms under the line (the figure from lesson 4.2). Any load pushes the median up, and so the p99 over the line: even the quietest minute, with 262 requests, is at 1.009%. So every minute of the day is above 1%, the day as a whole has 1.58% of its requests slower than 250 ms against the 1% allowed, and in the 14 minutes from 14:13 to 14:26 the share is above 5%, peaking at 7.9%.
The current service gets through the day: 0.749% of the day’s requests are slow, inside the 1%. It does not get through every minute. In the 18 consecutive minutes from 14:11 to 14:28 the share is above 1%, as the flash crowd lifts the median, and it peaks at 4.9% and never reaches 5%. If the SLO is judged on the day (or a longer window), the baseline meets it, and if it is judged minute by minute it does not. That is a question about what the SLO means, not about the simulation, and it is worth settling before the number is used.
Two cautions about reading this. First, the new deploy’s failure here is not something the recording shows about a real service. It comes from putting a service that sits 0.26 ms under the line into a model in which load only ever makes latency worse. In lesson 4.2 the same two services produced a coin flip at 100,000 requests; here the answer is not a coin flip because there is nothing left to flip: the result is exact for the trace and the assumption. Second, that exactness is the exactness of arithmetic. If the real median does not rise with load the way the model says, the numbers change, and the 1,440 minutes are one day.
The growth question
The usual next question is “and when the traffic grows?” With a trace it is one more line: multiply every request count by a scale. The library of this lesson has no scale parameter. To recreate the table below, use a throwaway copy of the library in which Trace is a Calculate (y[0] = x[0] * 1.2, for example) fed by the FlatFile, and run it for each scale. The same numbers were also computed outside the model, in Python, with the exact normal distribution function. The two agree to 7.1e-8 in every minute’s share, at five scales (0.8, 1.007, 1.2, 1.208 and 1.5). The values below are exact for the assumption.
| Traffic scale | Baseline: day’s share | minutes above 1% | minutes above 5% | busiest minute | NewDeploy: day’s share | minutes above 1% | minutes above 5% | busiest minute |
|---|---|---|---|---|---|---|---|---|
| 0.8 | 0.588% | 16 | 0 | 2.68% | 1.330% | 1,440 | 0 | 4.80% |
| 1.0 (the recorded day) | 0.749% | 18 | 0 | 4.91% | 1.577% | 1,440 | 14 | 7.91% |
| 1.1 | 0.858% | 18 | 12 | 6.46% | 1.735% | 1,440 | 14 | 9.92% |
| 1.2 | 0.989% | 20 | 13 | 8.33% | 1.918% | 1,440 | 16 | 12.25% |
| 1.3 | 1.143% | 20 | 14 | 10.52% | 2.124% | 1,440 | 16 | 14.87% |
| 1.5 | 1.520% | 60 | 16 | 15.85% | 2.604% | 1,440 | 18 | 20.88% |
“When does the baseline start to breach?” has three answers, depending on what breaching means:
- The day reaches 1% at a scale of 1.208: about 21% more traffic than the recorded day, with the shape of the day unchanged. At 1.2 it is at 0.989%, just inside the line, and at 1.21 at 1.003%. That is the figure that fits the sentence “the service can take 20% growth”, and note how little margin it has: the same service at 1.25 is at 1.063%.
- A minute already breaches at the recorded day (18 minutes at 1.0), and the busiest minute reaches 1% at a scale of 0.49 (a single minute from half the traffic). Scaling the day mostly makes the flash crowd worse (and lengthens it by a couple of minutes). Minutes outside the flash crowd start to breach only when the daytime hump itself reaches the line: two minutes, at 12:51 and 13:00, at a scale of 1.4, and 40 of them between 12:09 and 13:41 at 1.5 (60 minutes above 1% in all, against 22 at 1.4).
- A minute above 5% first appears at a scale of 1.007: the recorded day’s worst minute (4.906%) is 0.7% more traffic from 5%. At 1.1 there are 12 such minutes.
The new deploy is above 1% for the day at every scale in the table and in the figure. It would need traffic to fall to about 0.09 of the recorded day, which is no use, to get under 1%. A deploy that sits on the line at zero load has no headroom to give.
The companion page lets you pick the service and the scale (1.0, 1.2 or 1.5) and shows the minute-by-minute chart, the day’s share, the minutes above 1% and 5%, and the full table of scales.
Check: do the numbers match the math?
The trace. The request columns of both imports equal the trace in all 1,440 minutes, and the trace adds up to 997,041 requests, the figure the share of the day is divided by.
One minute by hand. The spike minute, 14:16, 4,684 requests, for the new deploy:
- (4,684 / 3,000)^2 = 2.4378, so the load factor is 1 + 0.5 x 2.4378 = 2.2189 and the median is 33 x 2.2189 = 73.22 ms.
- z = ln(250 / 73.22) / 0.87 = 1.2280 / 0.87 = 1.4114.
- 1 - Phi(1.4114) = 0.07906, which is 7.906% of 4,684 requests: 370.3 slow requests. Grind’s row has 0.07906.
For the baseline in the same minute: the median is 30 x 2.2189 = 66.57 ms, z = ln(250 / 66.57) / 0.8 = 1.3233 / 0.8 = 1.6541, and 1 - Phi(1.6541) = 0.04906, which is 229.8 slow requests, again as in the output. (Through the polynomial of the model the same number is the density 0.10158 at z times the polynomial’s value of 0.48294, which is 0.04906.) And at the quiet end, minute 00:00 with 303 requests: the baseline share is 0.004097 and the new deploy’s 0.010125, as in the table of the first rows.
All the minutes. Python’s exact computation, with the standard normal distribution function from the error function, against the share column of Grind: the largest absolute difference over the 1,440 minutes is 6.7e-8 for the baseline and 7.0e-8 for the new deploy, which is the error bound of the polynomial. The slow-request column differs by under 3e-4 of a request in a minute. The day totals agree: the slow-request columns sum to 7,464.19 (Python: 7,464.16) and 15,722.29 (15,722.32).
Replay. A count of 2,880 gives two days in which rows 1,441 to 2,880 equal rows 1 to 1,440, and the seeds 405, 1 and 99 give identical output.
What this gives you: trace-driven simulation. FlatFile in sequential mode replays a recorded series one row at a time, onExhaustion decides what happens at the end, and a library with declare and Import runs the same recording through two versions of a service. A model with no randomness is a calculation: it needs no seed and no replications, and its answer is exact for the trace and the assumptions. Here, the recorded day would have breached the SLO under the new deploy, on every one of 1,440 minutes (1.58% of the day’s requests slower than 250 ms against the 1% allowed, 7.9% in the flash crowd), and the current service would have met the SLO for the day (0.75%) while breaching it for 18 minutes in the flash crowd; the baseline can take about 21% more traffic before the day itself breaches. All of it rests on the invented trace and on the assumption that the median latency rises with load.
This closes Module 4. It started with a latency model written once as a parameterized library, and used it for scenarios judged against an SLO together with the uncertainty of a single run, for fair comparisons that give two scenarios the same random numbers, for incidents injected into the traffic to test an alert rule against known answers, and now for a recorded day of traffic replayed against two deploys. Each lesson changed what the model was fed, and not what it was asking: whether the service meets its SLO, and how sure you can be. Module 5 comes next: an inventory model, where the stock level is shared state that many parts of the model read and change.