DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Discover Hidden Patterns with Intelligent K-Means Clustering
  • Debunking LLM Intelligence: What's Really Happening Under the Hood?
  • Process Mining Key Elements
  • The Transformer Algorithm: A Love Story of Data and Attention

Trending

  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Data Contracts as the "Circuit Breaker" for Model Reliability
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • How to Save Money Using Custom LLMs for Specific Tasks
  1. DZone
  2. Data Engineering
  3. Data
  4. Mastering Approximate Top K: Choosing Optimal Count-Min Sketch Parameters

Mastering Approximate Top K: Choosing Optimal Count-Min Sketch Parameters

A practical guide to configuring width (w) and depth (d) in Count-Min Sketch for efficient top-k estimation and real-time analytics.

By 
Baskar Sikkayan user avatar
Baskar Sikkayan
·
Sep. 15, 25 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.6K Views

Join the DZone community and get the full member experience.

Join For Free

What Is Top K?

The "Top K" problem refers to determining the top-k elements with the highest frequencies or relevance scores from vast, rapidly changing data streams. In modern real-time systems — such as e-commerce platforms, social media, and streaming services — it's vital to quickly identify the most relevant items or events. Real-world examples include:

  • Trending Twitter hashtags rapidly shifting based on tweet volume
  • Most-watched Netflix movies updating hourly across regions
  • Top Amazon products ranking sales in real time
  • Popular YouTube videos updating hourly based on view velocity

The "Top K" approach is essential for use cases like:

  • Recommendation engines (e.g., Netflix’s "Top 10 Today" drives engagement)
  • Social trends (e.g., Twitter’s regional "Trending Now" sparks real-time interactions)
  • Leaderboards (e.g., Duolingo motivates learning by ranking users)
  • Monitoring and alerts (e.g., dashboards highlight anomalies in system performance or fraud detection)

Challenges and examples with the "Top K" method and real-time analytics:

  • High-volume streams – rapidly changing data (e.g., Black Friday product views)
  • Out-of-order events – delayed data skewing analytics
  • Failures and state management – server crashes affecting stateful data
  • Multi-data center coordination – aggregating regional results accurately and quickly
  • Duplicates and replays – replays or retries causing inflated counts
  • Hot keys and data skew – one product dominating data streams

Solving the Top K Problem: Approximate Top K Using Count-Min-Sketch

Computing the top-k most frequent items (e.g., most viewed videos, most searched terms, trending hashtags) is a fundamental problem. However, the method you choose depends heavily on the scale, latency requirements, accuracy constraints, and resource availability for the project.

The approximate top-k approach sacrifices a small amount of accuracy in favor of high performance, lower memory usage, and scalability. It is ideal for high-throughput systems where exact precision isn't critical. Overview of common techniques:

  • Count-Min Sketch (CMS) is a probabilistic data structure that uses multiple hash functions to estimate frequency counts with a bounded error.
  • Heavy Hitters Algorithm (e.g., Space-Saving or Misra-Gries) maintains counters only for the most frequent items seen so far.
Pros Cons
Low memory footprint (can track millions of keys in MBs of memory) May produce false positives
Mergeable across distributed nodes or data centers Ranking can be slightly off (e.g., item shown in Top 10 might actually be #11)
Very fast and suitable for real-time updates Not suitable for use cases requiring exact precision
Great for dashboards and trending feeds


What are "heavy hitters" and why do they matter? In a large data stream, heavy hitters are the few items that occur significantly more often than the rest. They are the items that dominate — the most clicked, most searched, or most viewed. Real-world examples include:

  • A few products account for 90% of sales on a retail website
  • A few hashtags trend while thousands go unnoticed
  • A handful of videos go viral on YouTube while most get a few views

Difference between heavy hitters and long tails:

  • Heavy hitters – top n items with heavy frequency
  • Long tails – large number of items with low frequency

What Do Heavy Hitters Have to Do With Approximate Top K?

When you only want the most frequent items, you don’t need to count every single event exactly or store every rare item’s frequency. You just need a structure that is fast, memory efficient, and good at capturing heavy hitters — that's where Count-Min Sketch shines.

CMS tolerates small overestimates for rare items but still tracks the frequent items accurately enough, which is why it's ideal for streaming top k or alerting systems.

Key takeaway: Count-Min Sketch gives us a fast and scalable way to focus on what matters most, the heavy hitters, without wasting memory on the long tail.

Count-Min Sketch Explained

As noted above, Count-Min Sketch is a probabilistic data structure that provides an efficient way to estimate the frequency of elements in a data stream using limited memory. It allows us to answer: "How many times has item x appeared so far?"

CMS is fast to update, memory efficient, and mergeable across systems, though it may overestimate (but never underestimates). It works by using a 2D array of counters with dimensions d × w:

  • d = number of hash functions (rows)
  • w = width of each row (number of buckets)

Every time an element appears:

  1. It is hashed using d different hash functions.
  2. Each hash function maps the element to a column index in its row.
  3. The counters at each of these d positions are incremented.

To estimate the frequency of an element, hash it using the same d hash functions and look up the corresponding counters — the minimum of these values is returned as the estimated frequency.

When configuring a CMS, you must consider two primary parameters:

  1. Accuracy (ε) – determines the acceptable error margin.
  2. Confidence (δ) – indicates the probability with which the error stays within the specified margin.

Width (w) controls the error in frequency estimation and determines how many buckets per hash function: W=⌈e/ε⌉

  • ε (epsilon) is the error margin in frequency estimation.
  • e is Euler's number (~2.718).

Depth (d) controls the probability of error (confidence) and determines how many hash functions are used. More depth = higher resilience to hash collisions. The probability that an estimate is greater than the actual count + εN is at most δ (confidence level): D=⌈log(1/δ)⌉

  • δ (delta) is the probability that the estimation is incorrect.

Count-Min Sketch Accuracy and Confidence

The frequency estimate of an element in CMS has an error bounded by εN\varepsilon NεN, where NNN is the total number of elements processed. The probability of this bound holding is at least 1−δ.

Example calculation: If we want an error margin of at most 1% (ε=0.01) and a confidence level of 99% (δ=0.01), then:

  • W=⌈e/0.01⌉=⌈271.8⌉=272 
  • D=⌈log(1/0.01)⌉=⌈log(100)⌉=⌈2⌉=2

Thus, a CMS with a width of 272 and depth of 2 would give us an approximate frequency count with 1% error and 99% confidence.

Practical recommendations:

  • Typical applications
    • If high accuracy is critical (e.g., frequency counts used for billing), set a very small ε (e.g., 0.0001 or smaller).
    • For log analysis or analytics, slightly higher ε (e.g., 0.001-0.01) is typically acceptable.
    • Depth (d) around 7-10 is common, providing > 99% confidence.
  • Memory considerations
    • Increasing w improves accuracy linearly but increases memory proportionally.
    • Increasing d improves confidence exponentially but uses additional memory linearly.

The commonly given CMS formulas focus only on relative accuracy, not explicitly on unique-key volume. In practice:

  • Always factor in the total event volume and desired absolute error explicitly.
  • Choose accuracy (ε) based on total event count.
  • The higher the total volume, the smaller ε you'll need (to maintain fixed absolute error margin), resulting in higher memory consumption.

Let’s consider a scenario with 1 billion unique videos, where:

  • w = e/ε
  • d = ln(1/δ)

Here, accuracy ε means the CMS guarantees that the estimated count will at most be the TRUE COUNT + (ε × TOTAL COUNT) with probability ≥ (1 – δ). Thus, accuracy directly depends on the total number of events (total count), not merely on the number of unique keys (distinct keys or volume).

How Should You Factor "Volume" into CMS Settings?

Since accuracy in a CMS is relative to total event counts, you must first:

  • Estimate your expected total number of events (not just distinct items).
  • Choose ε to ensure acceptable absolute error (Absolute Error = ε × total events).

Adjusting CMS Width and Depth Practically Based on Volume

Example: Let's say that you have one billion unique video IDs. Assume that you track view events (each ID viewed multiple times), and suppose each video ID averages 1,000 views. Total events is ~1 billion IDs × 1,000 views each ≈ 1 trillion (10¹²) total events.

The desired error is ±1,000 counts per video (reasonable relative error for top-k analysis); thus:

  • The absolute error = 1,000
  • Accuracy (ε) = Absolute Error / Total Events = 1,000 / 1,000,000,000,000 = 1e-9

Now, compute CMS parameters clearly:

  • Width (w) ≈ e/ε = 2.718 / 1e-9 ≈ 2,718,000,000 (2.7 billion)
  • Depth (d) to achieve high confidence (δ = 1e-6), d = ln(1/δ) ≈ ln(1,000,000) ≈ 14
  • Memory required ≈ w × d × 4 bytes ≈ (2.7 × 10⁹) × 14 × 4 bytes ≈ ~151 GB!

This clearly illustrates why factoring total event volume (rather than just the unique keys) is critical when choosing CMS parameters practically.

  • Width is strongly driven by total event volume and desired error margin.
  • Depth of 10-14 typically gives excellent confidence (> 99.99%+).

One Billion Unique Videos Scenario

In reality, although you have one billion unique videos, only a small fraction of videos typically receive clicks in short time windows (e.g., last 1 min, 5 mins, 1 hour, 1 day):

  • Most videos get zero (or very few) clicks in short windows.
  • Top videos (popular videos) get significantly more clicks (heavy hitters).
  • There's a highly skewed distribution (e.g., Zipfian distribution) — a small number of videos dominate the click volume.

This means that you do not need ultra-high accuracy for low-frequency videos (e.g., videos clicked just once or twice). Small errors, like ±10 or even ±100, for rarely clicked videos usually don't matter practically. Accuracy is critical primarily for the heavy hitters (top-k videos).

Practical Recommendations to Optimize CMS Memory

Given the real-world scenario, here’s how to optimize CMS practically.

Set Your Acceptable Absolute Error Higher

For 100 billion total events, instead of demanding ±100 accuracy universally, it's usually sufficient to have higher error (e.g., ±1,000 or even ±10,000 clicks) for the rarely clicked videos because being off by ±10,000 views on a video clicked only 10 times doesn't practically matter — it's already ranked low.

The practical choice is to have an acceptable error of ±1,000 clicks (for a 100 billion total event scenario), where:

ε = 1,000/ 100,000,000,000 = 1e-8

Memory dramatically reduces (151 GB → 15 GB) by accepting a slightly higher absolute error.

Use Sliding Window Count-Min Sketch (Time Based)

Practically, you're mostly interested in recent time periods (e.g., clicks in the last minute/hour/day). Sliding Window CMS periodically reset (or decay) counts to only consider recent clicks. This keeps total event counts much smaller within each window, further reducing the absolute error.

Within 5 minutes, total clicks might be only 1 billion (instead of 100 billion). Acceptable ±100 absolute error now implies:

ε = 100/1,000,000,000 = 1e-7

Width (w) ≈ 27 million, and memory ≈ 1.5 GB (very practical!).

Combine CMS With Heavy Hitters (Top-K) Algorithm

Count-Min Sketch alone is approximate, especially problematic for top-k analytics. Use CMS combined with another algorithm designed specifically for tracking heavy hitters: the Space-Saving algorithm (most popular) or the Misra-Gries algorithm. CMS helps for general frequency estimation, while the heavy hitter algorithm provides exact frequencies for the most-clicked videos.

Example practical combination:

  • Use Space-Saving algorithm with:
    • Capacity = 10,000 (track top 10,000 precisely)
    • Memory cost = very small (~a few MB)
  • Use CMS with slightly relaxed accuracy for less popular videos.

This gives you exact counts for the top ~10K videos and reasonable approximations for the remaining billions. Very low memory is required (CMS width ~27 million or lower).

For the one billion videos scenario (realistic traffic distribution), suppose in a 5-minute window:

  • Only 10% of videos (~100 million) receive clicks
  • Total count is around 1 billion clicks (10 clicks each on average for clicked videos)
  • Desired accuracy for top videos is ±100 clicks absolute

Then:

  • ε = 100 / 1,000,000,000 = 1e-7
  • w = e/ε ≈ 27 million
  • d = 14
  • Memory ≈ 1.5 GB (practically feasible!)

Additionally, if you combine with the Space-Saving (capacity = 10K videos) algorithm for heavy hitters, this is the result:

  • Top-k videos = exact count (space saving)
  • All other videos = ±100 clicks (very reasonable)
  • Memory is ~1.5 GB (CMS) + ~few MB (space saving)

Practical Advice For Large-Volume Cases

In large volume cases, such as the one billion unique videos scenario:

  • Don't blindly apply formulas without practical context.
  • Clearly realize that total count and data distribution (skewed clicks) matter enormously.
  • Increase acceptable error for rarely clicked videos.
  • Use Sliding Window to focus accuracy only on recent, relevant windows.
  • Combine CMS with heavy hitters algorithms (Space-Saving).

This approach ensures excellent analytics accuracy, practically with minimal memory usage, even at extreme scales like billions of unique video IDs.

Conclusion

Real-time identification of top-k items is crucial for platforms that rely on fast, data-driven decisions, whether it's trending hashtags, most-viewed products, or viral videos. Exact counting methods become impractical at scale due to immense memory and processing demands. The Count-Min Sketch algorithm, especially when combined with heavy hitters techniques like Space-Saving, offers an optimal balance between accuracy, speed, and resource efficiency.

By intelligently choosing parameters based on real-world data volume and acceptable error margins, and by combining CMS with heavy hitter algorithms, you can effectively capture and track the most relevant items in streaming data. This practical approach ensures high-performance analytics even under extreme scale, empowering businesses to stay responsive, insightful, and competitive.

Content management system Algorithm Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Discover Hidden Patterns with Intelligent K-Means Clustering
  • Debunking LLM Intelligence: What's Really Happening Under the Hood?
  • Process Mining Key Elements
  • The Transformer Algorithm: A Love Story of Data and Attention

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook