The Count-Min Sketch

•20 min read•

The Count-Min Sketch is a probabilistic data structure designed to estimate the frequency of events within a massive data stream. Operating in sub-linear space, it provides a highly memory-efficient alternative to traditional hash tables for counting occurrences of items. By accepting a mathematically bounded probability of overcounting due to hash collisions, the structure allows computer scientists and software engineers to process billions of events using only kilobytes or megabytes of memory. In an era defined by unbounded data generation, from high-frequency financial trading networks to global social media platforms, the ability to summarize data streams in real-time has become a critical operational requirement. Traditional data structures simply cannot scale to meet the memory demands of modern stream processing, making probabilistic approaches highly practical.

Invented in 2003 by Graham Cormode and S. Muthukrishnan, the algorithm relies on a two-dimensional array of counters and a set of pairwise independent hash functions. When an item arrives in the stream, it is hashed multiple times to update corresponding counters across different rows of the array. To query the frequency of an item, the algorithm retrieves the minimum value among its hashed counter locations, effectively filtering out the noise generated by collisions with other items. This straightforward design avoids the complex tree balancing or dynamic resizing operations required by exact counting methods.

Because of its simplicity, predictable performance, and strong theoretical guarantees, the Count-Min Sketch has been widely adopted across distributed systems, network traffic monitoring, and large-scale data analytics. It forms a foundational component in modern databases and stream processing libraries, enabling real-time analytics on high-velocity data where exact counting is computationally infeasible. The algorithm represents a major advancement in computer science, demonstrating that sacrificing a small, quantifiable amount of accuracy can yield exponential improvements in system scalability.

Problem Definition and Theoretical Context

In theoretical computer science and database management, the data stream model describes a scenario where data arrives continuously, rapidly, and in volumes too large to store entirely in memory or on disk. A stream consists of a sequence of updates $(i_t, c_t)$, where $i_t$ represents an item from a massive universe of possible items $U$, and $c_t$ represents the count or weight of that update at time $t$ [1]. The stream is considered practically infinite, meaning algorithms must process each update in a single pass and cannot afford to store the raw data for later re-evaluation.

The goal of frequency estimation is to answer point queries: given an item $i$, what is its total accumulated count $f_i$ in the stream? If the universe $U$ is small, an exact answer is easily maintained using an array or a hash table. However, in real-world systems, the universe of items is often astronomically large. For example, tracking the frequency of all possible source and destination IP address pairs in a network router involves a universe of $2^{64}$ possible combinations for IPv4, and vastly more for IPv6. Storing an exact counter for every unique item observed requires space proportional to the number of distinct elements, denoted as $O(|U|)$ or $O(N)$, which quickly exhausts the fast static random-access memory (SRAM) available on network hardware [2].

To solve this memory bottleneck, researchers developed approximate query processing algorithms known as sketches. A sketch compresses the data stream into a fixed-size memory footprint, sacrificing exact accuracy for sub-linear space complexity. The fundamental tradeoff is that a sketch uses $O(1)$ or $O(\log N)$ space, completely decoupling the memory requirements from the number of unique items in the stream.[31]

Stream processing models are generally divided into distinct categories based on the nature of the updates $c_t$. In the cash register model, updates are strictly positive ($c_t > 0$), meaning items are only added to the stream. This represents scenarios like counting page views or network packets. In the turnstile model, updates can be positive or negative, representing both arrivals and departures, such as users logging in and out of a service. The strict turnstile model adds the constraint that the total frequency $f_i$ of any item never drops below zero [3].[32] The standard Count-Min Sketch is optimized for the cash register model, where it provides a strict one-sided error guarantee, though it can be adapted for the turnstile model.

Beyond the cash register and turnstile models, modern stream processing often employs the sliding window model. In this paradigm, only the most recent updates or events within a specific time frame are considered valid, and older events expire. This requires specialized adaptations of sketches to forget stale data, ensuring that the frequency estimates reflect only the current state of the system rather than the entire historical record.

Core Mechanism and Mathematical Foundation

The Count-Min Sketch operates using a two-dimensional array of integer counters, denoted as a matrix $C$ with $d$ rows and $w$ columns. Initially, all counters in the $d \times w$ matrix are set to zero. The algorithm also initializes $d$ pairwise independent hash functions, $h_1, h_2, \dots, h_d$. Each hash function corresponds to one row of the matrix and maps an input item from the universe $U$ to a column index in the range $[1, w]$ [4].[33]

The requirement for pairwise independent hash functions is a critical mathematical detail. The concept of universal hashing, introduced by J. Lawrence Carter and Mark N. Wegman in 1979 [5], provides the foundation for this mechanism. Storing a truly random hash function would require as much memory as the data itself, defeating the purpose of the sketch. Instead, pairwise independent hash functions provide the necessary statistical randomness while requiring only constant space. These are often implemented using simple modular arithmetic, such as $h(x) = ((ax + b) \bmod p) \bmod w$, where $p$ is a large prime and $a$ and $b$ are randomly chosen integers. This ensures that the probability of any two distinct items hashing to the same bucket is exactly $1/w$.

When an update $(i, c)$ arrives in the stream, the algorithm iterates through each of the $d$ hash functions. For each row $j$ from $1$ to $d$, the item $i$ is hashed to find the column index $k = h_j(i)$. The counter at $C[j][k]$ is then incremented by $c$.

To estimate the frequency of an item $i$, the algorithm again hashes the item using the $d$ hash functions to locate its corresponding counter in each row. Because multiple items may hash to the same column in any given row (a hash collision), the value in any single counter may be larger than the true frequency of $i$. However, in the cash register model where all counts are positive, a counter can never be smaller than the true frequency. Therefore, the algorithm returns the minimum value among the $d$ counters as its estimate $\hat{f}_i$.

The following pseudocode demonstrates the initialization, update, and estimation procedures:

function initialize(w, d):
    table = 2D array of size d by w, initialized to 0
    hash_functions = array of d independent hash functions
    return (table, hash_functions)

function update(item, count):
    for j from 1 to d:
        index = hash_functions[j](item) modulo w
        table[j][index] = table[j][index] + count

function estimate(item):
    min_estimate = infinity
    for j from 1 to d:
        index = hash_functions[j](item) modulo w
        if table[j][index] < min_estimate:
            min_estimate = table[j][index]
    return min_estimate

The time complexity for both the update and estimate operations is $O(d)$, which is constant with respect to the number of unique items in the stream. The space complexity is $O(w \cdot d)$ counters [6].

The dimensions $w$ (width) and $d$ (depth) are not chosen arbitrarily. They are derived mathematically from user-defined parameters for acceptable error ($\epsilon$) and probability of failure ($\delta$). Let $N$ be the total sum of all counts in the stream (the L1 norm). The Count-Min Sketch guarantees that the estimated frequency $\hat{f}_i$ satisfies the inequality $f_i \le \hat{f}_i \le f_i + \epsilon N$ with a probability of at least $1 - \delta$ [7].

To achieve this guarantee, the width is set to $w = \lceil e / \epsilon \rceil$, where $e$ is the base of the natural logarithm, and the depth is set to $d = \lceil \ln(1 / \delta) \rceil$. The mathematical proof relies on Markov's inequality, a fundamental theorem in probability theory that bounds the probability of a non-negative random variable exceeding a certain value. Specifically, Markov's inequality states that for a non-negative random variable $X$ and a constant $a > 0$, the probability that $X \ge a$ is at most the expected value of $X$ divided by $a$. In any single row of the Count-Min Sketch, the expected overestimation added to a specific item's counter by other items in the stream is bounded by $N / w$ (or is exactly $(N - f_i) / w$). By applying Markov's inequality, the probability that the overestimation in one row exceeds $\epsilon N$ is at most $(N/w) / (\epsilon N) = 1 / (w \epsilon)$. Substituting the chosen width $w = e / \epsilon$, this probability becomes exactly $1/e$.

Because the $d$ hash functions are independent, the probability that the overestimation exceeds $\epsilon N$ in all $d$ rows simultaneously is $(1/e)^d = e^{-d}$. Substituting $d = \ln(1/\delta)$, this probability simplifies exactly to $\delta$. Thus, the minimum of the $d$ counters will be within $\epsilon N$ of the true frequency with probability $1 - \delta$ [8]. This mathematical rigor is what separates the Count-Min Sketch from ad-hoc heuristic counting methods.

Discovery and Historical Development

The Count-Min Sketch was invented by computer scientists Graham Cormode and S. Muthukrishnan in 2003 while both were affiliated with the Center for Discrete Mathematics and Theoretical Computer Science (DIMACS) at Rutgers University [9]. During this period, DIMACS was a major hub for research into massive data sets, driven by the rapid expansion of the internet and the telecommunications industry. Researchers were actively seeking algorithms that could process network packets at line rate without requiring prohibitive amounts of expensive static random-access memory (SRAM). The Count-Min Sketch emerged as a direct response to these hardware constraints. The algorithm was first presented at the Workshop on Management and Processing of Data Streams (MPDS) in 2003. It was subsequently published in the proceedings of the 2004 Latin American Symposium on Theoretical Informatics (LATIN) [10], and a highly cited expanded version was published in the Journal of Algorithms in 2005 [7].[34]

The development of the Count-Min Sketch built upon several decades of prior art in probabilistic data structures. In 1970, Burton Howard Bloom introduced the Bloom filter, a space-efficient probabilistic data structure used to test whether an element is a member of a set [11]. While Bloom filters share the concept of multiple hash functions and sub-linear space, they only store binary values (bits) and cannot track frequencies. In 2000, Fan, Cao, Almeida, and Broder introduced the Counting Bloom Filter, which replaced bits with small counters to allow deletions, but its space requirements remained proportional to the number of distinct elements in the set rather than providing the sub-linear bounds of a sketch [12].

In 1983, Philippe Flajolet and G. Nigel Martin developed the Flajolet-Martin algorithm to estimate the total number of distinct elements in a stream, but it could not answer point queries for individual items [13]. In 1996, Noga Alon, Yossi Matias, and Mario Szegedy introduced the AMS sketch, which estimates the frequency moments of a stream (such as the sum of the squares of all frequencies, known as the L2 norm). The AMS sketch was a major breakthrough in streaming algorithms and earned its authors the Gödel Prize [14].[35]

The most direct predecessor to the Count-Min Sketch was the Count-Sketch, introduced in 2002 by Moses Charikar, Kevin Chen, and Martin Farach-Colton [15]. The Count-Sketch uses a similar 2D array of counters but incorporates sign hash functions that map items to $+1$ or $-1$. It estimates frequencies using the median of the counters rather than the minimum. While Count-Sketch provides an unbiased estimator and bounds error relative to the L2 norm, the Count-Min Sketch was designed to be simpler, faster to update, and bounded by the L1 norm, making it highly effective for skewed data distributions.

The impact of Cormode and Muthukrishnan's invention was formally recognized in 2014 when their 2004 LATIN paper was awarded the Imre Simon Test-of-Time Award, an honor given to papers published in the LATIN conference at least ten years prior . The award committee highlighted how the algorithm successfully bridged the gap between deep theoretical bounds and practical, high-performance software engineering.

Algorithmic Variants and Refinements

Since its introduction, researchers have developed several variants of the Count-Min Sketch to optimize its performance for specific use cases, reduce its memory footprint, or adapt it to different streaming models.

Conservative Update

The most prominent refinement is the Count-Min Sketch with Conservative Updates (CMS-CU). The conservative update strategy was originally proposed in 2002 by Cristian Estan and George Varghese for multi-stage network filters [16], and was later formally applied to the Count-Min Sketch by Amit Goyal, Hal Daumé III, and Graham Cormode [17].

In the standard Count-Min Sketch, an update increments all $d$ hashed counters by $c$. In the conservative update variant, the algorithm first computes the current estimate $\hat{f}i = \min{j} C[j][h_j(i)]$. Then, instead of blindly adding $c$ to all $d$ counters, it updates each counter $C[j][h_j(i)]$ to the maximum of its current value and $\hat{f}_i + c$. Practically, if $c = 1$, this means the algorithm only increments the counters that are currently tied for the minimum value. This heuristic significantly reduces the overestimation bias caused by hash collisions, as counters that are already artificially inflated by other items are left alone [18]. However, using conservative updates breaks the linearity of the sketch, meaning two sketches can no longer be simply added together to merge them, and it strictly limits the structure to the cash register model (no negative updates) [19].

Count-Median Sketch

When a data stream includes negative updates (the turnstile model), the minimum operator used in the standard Count-Min Sketch fails. A negative update could cause a counter to drop below the true frequency of an item, violating the one-sided error guarantee. To handle the turnstile model, the algorithm is modified to use the median of the $d$ counters rather than the minimum. This variant, often called the Count-Median Sketch, provides an unbiased estimate and maintains similar error bounds, though it requires increasing the width and depth by constant factors to achieve the same confidence intervals as the standard version [3].

Heavy Hitters and Top-K

The Count-Min Sketch answers point queries, meaning the user must know the item $i$ to query its frequency. It cannot natively iterate over the counters to output the most frequent items in the stream. To solve the Heavy Hitters or Top-K problem, the sketch is paired with an auxiliary data structure, typically a min-heap [20].[36] Whenever an item is updated in the sketch, its new estimated frequency is queried. If this estimate exceeds the smallest frequency currently stored in the min-heap, the item is inserted or updated in the heap. This composite structure allows systems to maintain a real-time list of the most frequent items using minimal additional space.

Sliding Window Count-Min Sketch

In many real-time monitoring applications, such as identifying trending topics on social media or detecting sudden spikes in network traffic, historical data becomes irrelevant. The Sliding Window Count-Min Sketch adapts the structure to time-decaying data. This is typically achieved by maintaining multiple smaller sketches representing discrete time buckets (a tumbling window approach) and discarding the oldest bucket as time progresses. Alternatively, systems can periodically multiply all counters by a decay factor to implement an exponential decay function. These techniques ensure that the sketch reflects recent heavy hitters without the counters growing indefinitely.[37]

Learned Count-Min Sketch

With the rise of artificial intelligence, researchers have explored augmenting probabilistic data structures with machine learning. The Learned Count-Min Sketch (LCMS) introduces a neural network or a simpler predictive model as a pre-filter before the sketch [21]. The model attempts to predict whether an incoming item is a high-frequency element. If the model predicts a high frequency, the item is tracked in a small, exact hash table. If the model predicts a low frequency, the item is relegated to the standard Count-Min Sketch. By separating the heavy hitters from the long tail of rare items, the LCMS significantly reduces the error rate for a given memory budget, albeit at the cost of the computational overhead required to execute the model during stream ingestion.

Spectral Bloom Filters

Independently and concurrently in 2003, Saar Cohen and Yossi Matias introduced Spectral Bloom Filters, a data structure conceptually isomorphic to the Count-Min Sketch [22]. Spectral Bloom Filters also use multiple hash functions and an array of counters to estimate frequencies, and they introduced the concept of minimal increase, which is mathematically identical to the conservative update strategy.

Real-World Applications and Implementations

The Count-Min Sketch has transitioned from theoretical computer science into widespread production use across the software industry. Its low memory footprint and predictable performance make it ideal for systems that process massive volumes of data.

Redis, a popular in-memory data store, implements the Count-Min Sketch through its RedisBloom module. Web applications use this implementation to track user behavior, enforce API rate limits, and count page views without the unbounded memory growth associated with standard hash tables [23]. Because Redis operates entirely in RAM, the sub-linear space complexity of the sketch allows developers to monitor millions of unique keys using only a few megabytes of memory.

The Apache DataSketches project, originally developed at Yahoo and now maintained by the Apache Software Foundation, provides production-ready implementations of the Count-Min Sketch in C++, Java, and Python . This library is integrated into major distributed databases like Apache Druid and PostgreSQL to enable fast approximate queries. The complexity of implementing these structures safely in production was highlighted in October 2026, when a memory corruption vulnerability (CVE-2026-103634) was disclosed in the C++ implementation of the DataSketches Count-Min sketch. The flaw allowed a maliciously crafted sketch to bypass bounds checking during deserialization, demonstrating the challenges of handling probabilistic structures in untrusted environments [24].

In the Hadoop and Spark ecosystems, Twitter developed Algebird, a Scala library for abstract algebra on data streams [25]. Algebird implements the Count-Min Sketch as a monoid. In abstract algebra, a monoid is a set equipped with an associative binary operation and an identity element. Because the standard Count-Min Sketch is a linear transformation, two sketches of the exact same width and depth can be merged by simply adding their corresponding counters together. This algebraic property allows distributed systems to partition a massive data stream across hundreds of worker nodes, compute a sketch on each node independently, and merge them at a master node. The resulting merged sketch is mathematically identical to a sketch computed sequentially over the entire stream, making the algorithm highly amenable to MapReduce paradigms [26].

In network engineering, the Count-Min Sketch was integrated into AT&T's Gigascope, a stream database for network applications. Routers and switches use the sketch to identify elephant flows (high-volume network connections) in real-time, enabling automated traffic engineering and distributed denial-of-service (DDoS) mitigation without slowing down packet processing [2].

The algorithm is also heavily utilized in Natural Language Processing (NLP). When training language models or analyzing large text corpora, counting the frequencies of words, bigrams, or n-grams requires massive memory. Goyal, Daumé III, and Cormode demonstrated that using a Count-Min Sketch with conservative updates allows NLP systems to estimate word frequencies with high accuracy while reducing memory usage by an order of magnitude compared to exact counting [17].

Furthermore, the Count-Min Sketch has significantly impacted computational biology, particularly in the field of genomics. When assembling genomes from high-throughput DNA sequencing data, biologists must count the frequencies of short DNA substrings known as k-mers. Because a single sequencing run can generate billions of reads, exact k-mer counting using hash tables often requires hundreds of gigabytes of RAM. To solve this, the khmer software package utilizes the Count-Min Sketch to perform online k-mer counting and digital normalization [27]. By accepting a small false-positive rate, khmer allows researchers to filter out sequencing errors and assemble complex genomes on standard desktop computers rather than expensive high-memory server clusters.

In machine learning, particularly when dealing with high-dimensional categorical data, the Count-Min Sketch is employed for feature hashing. When training models on massive vocabularies, assigning a unique index to every possible feature exhausts memory. By hashing features into a Count-Min Sketch, models can compress the feature space into a fixed-size vector. While hash collisions mean that multiple distinct features might share the same weight, the sparsity of most real-world datasets ensures that the model can still learn effectively without requiring an exact mapping.

Other programming languages and ecosystems have native or community-supported implementations, such as the streaming_algorithms crate in Rust, the data.sketches package in R [28], and the Clearspring stream-lib library in Java [29].

Significance, Limitations, and Tradeoffs

The primary significance of the Count-Min Sketch lies in its L1 norm error guarantee. The error bound $\epsilon N$ is proportional to the sum of all frequencies in the stream. This makes the sketch exceptionally accurate for highly skewed data distributions, such as the Zipfian distributions commonly found in natural language, web traffic, and e-commerce purchases. In a Zipfian distribution, the frequency of any item is inversely proportional to its rank in the frequency table. A small number of items account for the vast majority of the total counts, while a massive long tail of items appears only once or twice. Because the heavy hitters dominate the stream, they are largely immune to the small amounts of noise added by collisions with the long-tail items. Consequently, the relative error for these frequent items remains very low [30].

By contrast, the Count-Sketch algorithm bounds its error relative to the L2 norm (the square root of the sum of squared frequencies). For highly skewed distributions, the L2 norm is significantly smaller than the L1 norm, meaning Count-Sketch can theoretically provide tighter error bounds for the same memory footprint. However, the Count-Min Sketch avoids the computational overhead of multiplying by sign hash functions and calculating medians, making it faster to execute per update and easier to implement directly in hardware logic (like Application-Specific Integrated Circuits or Field-Programmable Gate Arrays).

The Count-Min Sketch has specific limitations. It is a biased estimator; in the standard cash register model, it always overestimates or returns the exact true frequency, but it never underestimates. While this one-sided error is useful for thresholding (if the estimate is below a threshold, the true frequency is definitively below that threshold), it is less suitable for applications requiring unbiased statistical variance.

Additionally, the sketch performs poorly on uniform distributions. If all items in a massive universe appear with roughly equal frequency, the hash collisions accumulate evenly across all counters. In this scenario, the minimum value returned by the sketch will be heavily inflated by the uniform background noise, rendering the estimates largely useless unless the width $w$ is increased to a point where the memory savings are negated.

Finally, the standard Count-Min Sketch cannot natively handle deletions. If an item is removed from the stream, decrementing the counters can cause the minimum operator to underestimate the true frequency of other items that share those hash buckets. While the Count-Median variant solves this for the turnstile model, it sacrifices the strict one-sided error guarantee and requires more computational overhead to find the median during queries.

Despite these tradeoffs, the Count-Min Sketch remains a foundational algorithm in computer science. By successfully balancing memory efficiency, processing speed, and mathematical rigor, it provides a highly practical solution to the challenges of massive data streams.

Sources (37)
  1. courses.csail.mit.edu
  2. ACM Digital Library
  3. cs.dartmouth.edu
  4. sites.google.com
  5. doi.org
  6. web.stanford.edu
  7. doi.org
  8. cs.utah.edu
  9. Wikipedia:
  10. doi.org
  11. ACM Digital Library
  12. ACM Digital Library
  13. doi.org
  14. ACM Digital Library
  15. doi.org
  16. ACM Digital Library
  17. ACM Digital Library
  18. arxiv.org
  19. ACM Digital Library
  20. link.springer.com
  21. arxiv.org
  22. ACM Digital Library
  23. redis.io
  24. seclists.org
  25. github.com
  26. Wikipedia:
  27. doi.org
  28. cran.r-project.org
  29. github.com
  30. ACM Digital Library
  31. Charu C. Aggarwal, Data Streams (Springer Science & Business Media, 2007)
  32. Graham Cormode, Ke Yi, Small Summaries for Big Data (Cambridge University Press, 2020)
  33. Marios Mavronicolas, Algorithms and Complexity (Springer Nature, 2023)
  34. Martin Farach-Colton, LATIN 2004: Theoretical Informatics (Springer Science & Business Media, 2004)
  35. Charu C. Aggarwal, Data Mining (Springer, 2015)
  36. Gabor Szabo, Gungor Polatkan, P. Oscar Boykin, Social Media Data Mining and Analytics (John Wiley & Sons, 2018)
  37. Stefan Michaelis, Nico Piatkowski, Marco Stolpe, Solving Large Scale Learning Tasks. Challenges and Algorithms (Springer, 2016)