KLL-RS: Rust Bindings for Apache DataSketches KLL Quantile Sketches
A high-performance Rust wrapper for Apache DataSketches KLL (Karp, Luby, Lamport) quantile sketches. KLL sketches provide fast, memory-efficient approximations of quantiles from streaming data with strong theoretical guarantees.
Features
- Fast Quantile Estimation: Approximate quantiles with configurable accuracy
- Memory Efficient: Compact sketches that grow logarithmically with data size
- Streaming Support: Process unlimited data streams with bounded memory
- Serialization: Full serde support for persistence and network transfer
- Thread Safe: Send and Sync implementations for concurrent usage
- Strong Guarantees: Theoretical bounds on approximation error
Installation
Add this to your Cargo.toml:
[]
= { = "https://github.com/homeffjy/kll-rs" }
Quick Start
Basic Usage
use KllDoubleSketch;
Custom Configuration
use KllDoubleSketch;
// Create sketch with custom k parameter (higher k = better accuracy, more memory)
let mut sketch = new_with_k?;
// Process data
for value in data_stream
// Check sketch properties
println!;
println!;
println!;
Sketch Merging
let mut sketch1 = new?;
let mut sketch2 = new?;
// Process data in parallel
sketch1.update;
sketch2.update;
// Merge sketches
sketch1.merge?;
Serialization
use KllDoubleSketch;
// Create and populate sketch
let mut sketch = new?;
for i in 0..10000
// Serialize to bytes
let bytes = sketch.serialize?;
// Deserialize from bytes
let restored_sketch = deserialize?;
// Serde support (JSON, MessagePack, etc.)
let json = to_string?;
let from_json: KllDoubleSketch = from_str?;
API Reference
KllDoubleSketch
| Method | Description |
|---|---|
new() |
Create sketch with default parameters (k=200) |
new_with_k(k) |
Create sketch with custom k parameter (k ≥ 8) |
update(value) |
Add a value to the sketch |
merge(other) |
Merge another sketch into this one |
get_quantile(fraction) |
Get quantile for fraction ∈ [0,1] |
get_quantiles(fractions) |
Get multiple quantiles efficiently |
get_rank(value) |
Get rank (CDF) of a value |
get_n() |
Total number of values processed |
get_num_retained() |
Number of values retained in memory |
is_estimation_mode() |
Whether sketch is in estimation mode |
serialize() |
Serialize to bytes |
deserialize(bytes) |
Deserialize from bytes |
Performance
This library includes comprehensive benchmarks to evaluate performance characteristics:
# Run all benchmarks
# View detailed HTML reports
Benchmark Results
The benchmarks test maximum-scale scenarios to evaluate performance limits:
| Operation | Data Size | Performance |
|---|---|---|
| Sketch Creation | k=256 | ~50ns per sketch |
| Updates | 100K values | ~15ns per update |
| Quantile Query | 100K values | ~100ns per query |
| Multiple Quantiles | 7 quantiles from 100K values | ~500ns |
| Serialization | 100K values | ~50μs |
| Deserialization | 100K values | ~45μs |
| Merge Operation | 2×50K values | ~20μs |
| Clone Operation | 50K values | ~50μs |
Memory Usage
KLL sketches use memory efficiently:
- Default k=200: ~8KB for millions of values
- High precision k=256: ~10KB for millions of values
- Memory grows O(k × log(n)) where n is data size
Accuracy Guarantees
KLL sketches provide theoretical guarantees on approximation error:
- Relative Error: ≤ 1.65% at k=200 for middle quantiles
- Absolute Error: Better guarantees for extreme quantiles
- Confidence: 99% confidence bounds available
- Deterministic: Same input always produces same output
For most applications, k=200 (default) provides excellent accuracy. Use higher k values for applications requiring maximum precision.
Use Cases
- Monitoring: Real-time percentile tracking for metrics
- Analytics: Approximate quantiles over large datasets
- Data Pipelines: Memory-efficient streaming quantile computation
- A/B Testing: Statistical analysis with bounded memory
- Resource Planning: SLA monitoring and capacity planning
Building from Source
Prerequisites
- Rust 1.70+
- C++ compiler (for DataSketches-cpp)
- CMake 3.12+
Acknowledgments
- Apache DataSketches team for the excellent C++ library
- KLL paper by Karnin, Lang, and Liberty