shardtail is a small Python library and CLI for latency percentiles across sharded services. It implements a DDSketch-style logarithmic-bucket sketch with a relative-error guarantee, a compact binary encoding, per-minute rolling windows that merge exactly across shards and time, and the baselines a dashboard usually uses (average of shard p99s, traffic-weighted average, max, a 1,000-item reservoir). Everything is scored against exact quantiles computed from the raw data of a seeded synthetic gateway (about 400,000 requests, 16 shards, 12 routes, 60 minutes). The data is synthetic.
An API platform runs 16 gateway shards. Each shard reports per-route p50 and p99 every minute, and the dashboard averages them across shards and then across minutes for the hourly SLO view. Percentiles do not average. Busy shards are slower than quiet ones, and one shard has GC pauses, so the averaged p99 understates the fleet tail while the max of p99s overstates it. Shipping raw latencies is too expensive, so each shard ships a sketch instead: a summary that merges exactly across shards and across time and has a stated error bound.
flowchart LR
S["16 shard latency streams"] --> M["per-minute log-bucket sketches<br/>(one per shard, route, minute)"]
M --> A["merge across shards<br/>minute ring per route"]
A --> H["merge 60 minutes<br/>rolling hour"]
H --> Q["sketch quantiles"]
S --> R["raw arrays"]
R --> E["exact quantiles"]
R --> B["averaged, max and reservoir<br/>baselines"]
Q --> C["per-route SLO table<br/>relative errors"]
E --> C
B --> C
sketch.py: bucketicovers(gamma^(i-1), gamma^i]withgamma = (1+a)/(1-a). A bucket is reported as2*gamma^i/(gamma+1), which is withinaof every value in it. Buckets are a sparsedictof integer counts. Values below 1e-9 go to a zero bucket. If more thanmax_bucketsare in use, the lowest ones collapse into their upper neighbour.codec.py: header with accuracy, counts, zero count and collapse floor, then zig-zag varint delta-encoded bucket indices and varint counts. The round trip is exact.window.py: a ring of per-route, per-minute sketches. A late report for a minute that is already in the ring merges into it, so arrival order does not matter. A rolling hour merges 60 minutes.baselines.py: average, traffic-weighted average and max of shard p99s, the mean of every (shard, minute) p99, and Algorithm R reservoir sampling with a seeded generator.gen.py: the seeded traffic. Shard share follows Zipf (s = 1.2) and latency grows with share./searchis bimodal (cache hit about 8 ms, miss about 120 ms),/checkouthas a Pareto tail (shape 2.5), and shard 11 adds 250-600 ms to 2% of its requests in minutes 20-35.report.pybuilds the per-route table;cli.pyis the Typer CLI.
The guarantee is tested, not assumed. Every quantile whose exact value lies above the collapse
floor is within relative error a of sorted(data)[floor(q*(n-1))]. Values below 1e-9 are kept
only as a zero count and report 0.0, so the bound does not cover them (latencies are far above it). Merging adds integer counts, so
a merged sketch is bucket-for-bucket identical to the sketch of the concatenated data. That is what
makes shard and time roll-ups legitimate.
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
shardtail demo --seed 3
shardtail bench
shardtail slo --route /checkout --threshold-ms 300
pytestshardtail demo --seed 3 (errors are relative to the exact quantile; negative means the estimate
understates; sketch_B is the encoded sketch size and raw_B the same latencies as float64):
seed=3 requests=400259 shards=16 routes=12 accuracy=0.01
Hourly p99 (ms), minutes 0-59
route exact_p99 sketch_p99 err avg_shard_p99 err max_shard_p99 err sketch_B raw_B
/checkout 411.7 415.8 +1.0% 274.6 -33.3% 540.7 +31.3% 583 267296
/search 497.5 497.8 +0.1% 293.0 -41.1% 583.1 +17.2% 746 425760
/login 149.3 149.9 +0.4% 101.0 -32.3% 298.6 +100.0% 527 320648
/cart 159.8 159.2 -0.3% 94.7 -40.7% 185.8 +16.3% 475 293344
/profile 118.3 117.9 -0.3% 71.0 -40.0% 141.3 +19.4% 532 241504
/orders 329.1 327.1 -0.6% 196.5 -40.3% 393.9 +19.7% 478 211464
/catalog 159.6 159.2 -0.3% 98.7 -38.2% 197.3 +23.6% 591 373208
/inventory 260.6 262.5 +0.7% 155.0 -40.5% 311.7 +19.6% 513 187176
/recommend 578.9 584.2 +0.9% 345.4 -40.3% 694.1 +19.9% 596 212528
/upload 1169.4 1176.4 +0.6% 692.1 -40.8% 1475.3 +26.2% 484 106664
/notify 65.1 64.7 -0.6% 38.1 -41.5% 73.6 +13.1% 435 160720
/health 12.1 12.1 -0.1% 6.7 -44.7% 13.4 +10.8% 371 401760
Other baselines, minutes 0-59
route wavg_shard err avg_minute err reservoir err p999_sketch err p999_reserv err
/checkout 379.5 -7.8% 152.7 -62.9% 391.6 -4.9% 944.0 +0.2% 884.4 -6.1%
/search 407.3 -18.1% 220.9 -55.6% 496.2 -0.3% 685.5 -0.6% 821.0 +19.1%
/login 126.2 -15.5% 64.1 -57.0% 144.1 -3.4% 214.9 -0.4% 203.5 -5.7%
/cart 130.9 -18.0% 69.8 -56.3% 167.2 +4.7% 223.7 -0.9% 262.4 +16.2%
/profile 99.3 -16.0% 47.5 -59.8% 122.0 +3.1% 172.5 +0.3% 154.8 -9.9%
/orders 272.6 -17.2% 130.8 -60.3% 291.4 -11.5% 478.3 -1.0% 393.9 -18.4%
/catalog 137.9 -13.6% 69.1 -56.7% 169.9 +6.4% 267.8 +0.6% 249.6 -6.2%
/inventory 218.2 -16.3% 98.4 -62.2% 246.2 -5.5% 399.5 -0.2% 527.4 +31.8%
/recommend 483.4 -16.5% 208.7 -64.0% 564.5 -2.5% 925.4 +0.4% 933.6 +1.3%
/upload 998.0 -14.7% 367.2 -68.6% 1204.8 +3.0% 1939.5 +0.5% 2029.5 +5.2%
/notify 52.3 -19.7% 24.9 -61.8% 69.0 +5.9% 87.4 -0.2% 97.0 +10.9%
/health 9.4 -22.5% 5.7 -53.2% 12.4 +2.8% 15.0 +0.9% 15.1 +1.7%
GC window p99 (ms), minutes 20-35
route exact_p99 sketch_p99 err avg_shard_p99 err max_shard_p99 err sketch_B raw_B
/checkout 423.4 424.2 +0.2% 271.9 -35.8% 568.2 +34.2% 444 71800
...
shardtail bench (the largest observed error over p50, p90, p95, p99 and p99.9 on /checkout,
against the stated bound):
route=/checkout requests=33412 raw_bytes=267296
accuracy buckets bytes ratio max_err bound
0.005 433 1012 264x 0.49% 0.5%
0.010 237 583 458x 0.99% 1.0%
0.020 128 331 808x 1.94% 2.0%
shardtail slo --route /checkout --threshold-ms 300:
route=/checkout requests=33412 threshold_ms=300
under threshold: between 97.58% and 97.70% (sketch bound)
exact from raw data: 97.61%
- Logarithmic buckets, not rank-error sketches. Latency SLOs care about the tail in relative
terms: 5 ms of error at p99 = 400 ms is nothing, at p50 = 10 ms it is a lot. A relative bound
holds at every quantile and merging is plain addition. The cost is that the bucket count grows
with the log of the value range, not with a fixed memory budget;
max_bucketscaps it by collapsing the lowest buckets, which gives up accuracy for the smallest values only. - Collapse breaks bucket-exact merging. Without a collapse, merge is exact, commutative and associative (tested with hypothesis). Once buckets collapse, the result depends on merge order, although the bound still holds above the collapse floor. The default of 2,048 buckets is far above what request latencies need at 1% accuracy.
- The shard baselines are given exact per-shard p99s. The table computes each shard's p99 over
the queried window from raw data, which is better than a dashboard gets from per-minute reports.
The mean of every (shard, minute) p99 is also reported (
avg_minute) as the per-minute roll-up a dashboard does. Its -53% to -69% error is mostly a small-sample artifact, not a property of averaging alone: on/checkoutthe median (shard, minute) cell holds 16 requests and 59% hold fewer than 20, and with the rank rulefloor(q*(n-1))a cell of 3 to 100 requests returns its second-largest value (a cell of 1 or 2 returns its minimum), far below the true p99. Keeping only cells of 20+ requests lifts/checkoutfrom 152.7 ms to 239.0 ms, and 50+ to 348.5 ms, against an exact 411.7 ms. Readavg_minuteas an upper bound on how misleading a per-minute roll-up can be on thin cells, not as the typical dashboard error; the per-shard hourly figures are the cleaner comparison. - The reservoir baseline samples whole-route data per query.
reservoir_quantiledraws one 1,000-item Algorithm R sample from the route's full window for each query. It is not a per-shard streaming reservoir that gets merged, so it shows what a fixed-size sample costs at the tail, not how a deployed per-shard reservoir would behave. - CDF after a collapse. The sandwich
F(t) <= cdf(t) <= F(gamma*t)holds when nothing was collapsed or the threshold's bucket is at or above the collapse floor; below the floor,cdfcan undercount. - Rank rule
floor(q*(n-1)). Exact quantiles, shard quantiles and the sketch all use one rank definition, so differences in the table come from the method and not from interpolation. - Vectorised adds share one index function.
addandadd_arrayboth go through the same NumPylog/ceilpath, so a value always lands in the same bucket whichever way it arrives. - Float rounding. A value within one ulp of a bucket edge can sit a hair outside the bound; the
tests allow a relative slack of 1e-9 on top of
a.
pytest69 tests, including hypothesis properties for the error bound, merge exactness, the CDF sandwich
and the codec round trip. The recovery tests check on the seeded data that averaging shard p99s
understates the /checkout hourly p99 by at least 10%, that the max of shard p99s overstates it by
at least 25% during minutes 20-35, that the sketch stays within 1% everywhere, and that a
1,000-item reservoir misses /checkout p99.9 by more than 5% on at least 4 of 5 seeds.
src/shardtail/
sketch.py log-bucket sketch (add, merge, quantile, cdf, collapse)
codec.py binary encoding and size reporting
window.py per-minute ring and rolling windows
baselines.py averaged, max and reservoir baselines, exact quantiles
gen.py seeded gateway traffic
report.py per-route SLO table
cli.py shardtail demo | bench | slo
tests/ unit, property and CLI tests
MIT
