Quantile-Tracking Algorithms for Streaming Data: t-Digest, GK, and Reservoir Sampling Comparisons

Introduction & Market Landscape

Quantiles and percentiles are foundational to monitoring, anomaly detection, and SLO calculations across finance, observability, ad tech, and telemetry pipelines. Real systems ingest millions of events per second and cannot retain full history; computing exact quantiles requires either unbounded memory or multiple passes. Streaming quantile algorithms provide compact summaries that approximate distributional properties with provable or empirical error bounds. In e-commerce, latency p95 guides capacity planning; in observability, tail latencies determine incident escalation. The choice among t-digest, Greenwald-Khanna (GK), and reservoir sampling affects accuracy at tails, memory footprint, update cost, and ease of merge across workers. Operators must balance worst-case guarantees, typical-case behavior on skewed data, and merging behavior in sharded architectures. This article contrasts the three approaches, explains where each shines, and gives practical guidance for production deployment.

The Core Solution & Value Proposition

At a high level, each algorithm compresses history into a small structure that can be updated online and queried for approximate quantiles. t-Digest clusters values into variable-sized centroids that provide high relative accuracy at extreme quantiles. GK maintains an ordered list of tuples (value, g, delta) that guarantee an absolute error bound on rank; it is deterministic and simple to reason about. Reservoir sampling keeps a uniform sample of fixed size to produce unbiased quantile estimates with variance governed by sample size; it is trivial to implement and merges by concatenation and re-sampling. The value proposition differs: t-digest optimizes tail accuracy with merge-friendly centroids, GK provides deterministic rank-error bounds when memory is constrained, and reservoir sampling gives quick, resource-light estimates and handles arbitrary data distributions at the cost of weaker tail guarantees. Choosing the right sketch depends on whether you need strict error bounds, excellent tail fidelity, or minimal engineering complexity.

Comprehensive Feature Breakdown

Understanding internal components clarifies tradeoffs. t-Digest maintains weighted centroids, merging nearby points until a compression threshold is hit; its k-size parameter and compression function control centroid growth near the median versus tails. Practical implementations use incremental insertion and periodic compression, and support associative merges by summing centroids followed by re-compression. GK stores sorted entries with g (gap) and delta (slack) values; upon insertion it updates surrounding gaps and occasionally compresses adjacent entries when combined slack satisfies a bound. GK provides an epsilon parameter directly tied to rank error, making it attractive for SLAs. Reservoir sampling—specifically Vitter's Algorithm R for streaming—retains k items uniformly at random; quantile queries sort the reservoir and index into ranks. Reservoirs are sensitive to sample size when the distribution is heavy-tailed: extremes may be absent or poorly estimated. Examples: for p95 on a distribution with rare spikes, t-digest with compression 100 often outperforms a 10k reservoir. For p50 on bounded data, a small reservoir may suffice. GK will guarantee rank within epsilon*n but may buffer many entries when epsilon is tiny.

Operational Efficiency & Strategic Impact

Implementations differ in update cost, concurrency model, and diagnostic needs. t-Digest inserts amortized O(log m) per update using centroid search and O(m) for full compression; it merges across shards efficiently, which suits map-reduce and streaming frameworks. However, nondeterministic centroid boundaries complicate deterministic testing: reproduce-heavy-tail tests with synthetic extremes to validate tail error. GK's updates cost O(log m) to find location plus occasional linear scans for compression; its deterministic guarantees simplify SLA proofs and unit tests. Reservoir sampling is O(1) per update with occasional random replacement; it is simplest to test for unbiasedness by checking order statistics over repeated runs. In production, track sketch size, number of centroids or tuples, and max observation to detect drift. For fault tolerance, persist sketches frequently and design merge semantics carefully: t-Digest and reservoir re-sampling merge naturally, GK requires merging algorithm that preserves epsilon bounds (commonly rebuilding GK from merged raw tuples). For monitoring, include synthetic quantile checks and compare two algorithms in parallel during rollout to detect distribution shapes where a chosen sketch degrades.

Conclusion & Future Outlook

No single quantile algorithm is universally best. Use t-digest when tail fidelity and mergeability across distributed workers matter, especially for latency percentiles and skewed distributions. Prefer GK when deterministic rank-error guarantees are required to meet regulatory or contractual SLAs. Choose reservoir sampling when simplicity, minimal CPU, and unbiased sampling are sufficient and tail behavior is not critical. In many systems a hybrid approach is pragmatic: run a small reservoir for real-time quick checks, t-digests for accurate SLAs, and periodic exact computations on retained windows for audit. Future directions include hybrid sketches combining determinism and tail-focused compression, improved parallel merge algorithms with bounded error, and automated algorithms that adapt compression to observed skew. Practical deployments should instrument error against baseline offline computations, expose sketch diagnostics to operators, and pick parameters with clear mappings to expected errors under representative workloads.

Architecture & Engineering

Facing a similar challenge in your system?

I help engineering teams and product leaders architect resilient backends, integrate production-grade AI, and eliminate architectural bottlenecks.