Abstract
Given $m$ distributed data streams $A_1, \dots, A_m$, we consider the problem of estimating the number of unique identifiers in streams defined by set expressions over $A_1, \dots, A_m$. We identify a broad class of algorithms for solving this problem, and show that the estimators output by any algorithm in this class are perfectly unbiased and satisfy strong variance bounds. Our analysis unifies and generalizes a variety of earlier results in the literature. To demonstrate its generality, we describe several novel sampling algorithms in our class, and show that they achieve a novel tradeoff between accuracy, space usage, update speed, and applicability.
Introduction
In large-scale data processing, estimating the cardinality of unique elements across distributed streams is a fundamental task. While algorithms like HyperLogLog (HLL) provide excellent space efficiency for simple count-distinct operations, they often struggle with complex set expressions—such as intersections or set differences—required for ad-hoc subpopulation queries. Traditional methods like K’th Minimum Value (KMV) are more flexible for these operations but may suffer from slower update speeds or inconsistent accuracy.
This paper introduces the Theta-Sketch Framework, a unified mathematical approach that encompasses several popular streaming algorithms. By identifying the core properties that guarantee unbiased estimation, the framework allows for the development of new algorithms, such as the Alpha Algorithm, which optimizes for both speed and stability.
The Theta-Sketch Framework and 1-Goodness
The framework defines a "sketch" as a pair $(\theta, S)$, where $\theta \in (0, 1]$ is a threshold and $S$ is the set of all hashed values from the stream that fall below $\theta$. The cardinality estimate is derived from the density of points in $S$ relative to $\theta$.
A central contribution of this work is the identification of the 1-Goodness condition. An algorithm is 1-Good if its threshold-choosing function $T(x)$ ensures that the resulting estimator is perfectly unbiased.
As shown in Figure 5, the KMV algorithm satisfies this property, ensuring reliable estimates. Conversely, Figure 3 illustrates a "Bad" threshold function that violates 1-Goodness, leading to biased results.
The Alpha Algorithm
Building on this framework, the authors introduce the Alpha Algorithm. Unlike KMV, which maintains a sorted heap (resulting in $O(\log k)$ update time), Alpha utilizes a technique inspired by Morris’s Approximate Counting to achieve $O(1)$ amortized update time.
Alpha also addresses the "sawtooth" error pattern seen in Adaptive Sampling. While Adaptive Sampling's accuracy oscillates as it periodically culls its sample set, Alpha manages its threshold more smoothly, providing the steady accuracy characteristic of KMV without the computational overhead.
Evaluation and Results
The framework was evaluated using both synthetic benchmarks and the Yahoo WebScope "Groups" Dataset, which contains real-world membership data for 640,000 groups.
- Performance: The Alpha Algorithm demonstrated superior update speed compared to heap-based KMV.
- Accuracy: Under equal memory constraints, Alpha achieved lower error rates than both KMV and Adaptive Sampling due to its ability to maintain higher hash table occupancy.
- Composability: The framework includes a "growing union rule" that allows sketches to be merged while maintaining optimal variance bounds. The variance of a merged estimate is proven to be no worse than an estimate generated from a single concatenated stream.
Limitations and Future Work
While the Alpha Algorithm is highly effective, it does not strictly satisfy monotonicity in all multi-stream scenarios involving non-disjoint sets. Additionally, for simple count-distinct tasks on single streams, HLL remains more space-efficient. Future work aims to extend the rigorous variance bounds to more complex nested set expressions.
Conclusion
The Theta-Sketch Framework provides a rigorous foundation for cardinality estimation across complex set expressions. By unifying existing methods and introducing the Alpha Algorithm, this work offers a practical and theoretically sound solution for high-velocity distributed data streams. These algorithms are currently implemented in the Apache DataSketches library and used in production environments at Yahoo.
Summary updated