DEV Community

Cover image for How HyperLogLog Counts Billions of Views with 12KB RAM
Doogal Simpson
Doogal Simpson

Posted on • Originally published at doogal.dev

How HyperLogLog Counts Billions of Views with 12KB RAM

TL;DR: HyperLogLog (HLL) is a probabilistic data structure that estimates cardinality (unique counts) with minimal memory. Instead of storing millions of unique IDs, it hashes incoming data and tracks the longest consecutive sequence of ones at the start of the binary string to statistically estimate the total count.

I absolutely love elegant data structures. There is something incredibly satisfying about solving a massive scaling bottleneck not by throwing expensive hardware at it, but by using clever mathematics. Take video view counters on high-volume platforms.

If you try to keep track of every unique viewer by storing their user IDs in a standard in-memory set, your servers are eventually going to run out of memory. This is where HyperLogLog comes in, and honestly, it is one of my favorite engineering hacks of all time.

How do you count unique views at scale without crashing your database?

To count millions of unique views at scale, databases use probabilistic data structures like HyperLogLog instead of keeping track of every individual user ID. By trading a tiny margin of error for immense memory savings, systems can estimate billions of unique items using only a few kilobytes of RAM. This makes it possible to track real-time analytics with constant memory overhead.

Imagine your team is building a video platform. If you build it the naive way, you keep track of every unique user ID in a hash set to avoid double-counting. But when a video hits millions of views, storing those unique IDs eats up gigabytes of memory. Multiply that across millions of videos, and your database cluster is dead.

HyperLogLog changes the game. Instead of asking "Who exactly watched this video?" it asks "What is the statistical probability of the data patterns we have seen so far?"

How does the coin-flipping analogy explain HyperLogLog?

HyperLogLog works on the statistical probability of observing rare sequences in random data, much like flipping a coin repeatedly. If you tell me your longest uninterrupted sequence of heads is ten, I can estimate that you have flipped the coin roughly 1,024 times. The longer the sequence, the less likely it is to happen by chance, giving us a highly reliable proxy for the total number of attempts.

Think about the math behind coin flipping. If I flip a coin constantly and record the longest uninterrupted streak of heads:

  • One head in a row is a 1-in-2 chance.
  • Two heads in a row is a 1-in-4 chance.
  • Ten heads in a row is a 1-in-1024 chance.

If you tell me your longest uninterrupted streak is ten heads, I can reasonably guess that you have been flipping that coin about a thousand times. We can apply this exact probability logic to computer data.

How does the HyperLogLog algorithm process user IDs in practice?

The algorithm hashes every incoming user ID into a uniform binary string and records the maximum number of consecutive ones at the beginning of the string. Rather than storing the actual IDs, it only updates a small array of registers with these maximum run lengths.

When a user views a video, we hash their unique ID and convert it into a binary string of ones and zeros. We treat this binary string exactly like our coin flips, where a 1 represents a head and a 0 represents a tail. We then read through the beginning of this binary string to find the longest uninterrupted series of ones.

If the longest sequence of ones we see is ten, the odds dictate that we have processed roughly a thousand unique IDs. Instead of saving the giant user IDs, we only record that single number—ten—in a specific register. We discard the user ID immediately, keeping our memory footprint incredibly small.

Metric Naive Set (Exact Count) HyperLogLog (Probabilistic)
Memory Usage O(N) - Scales with unique items (Gigabytes) O(1) - Constant size (typically 12 KB)
Accuracy 100% exact ~99% (standard error of 1%)
Write Speed Slows down as sets grow Constant time O(1) writes
Primary Use Case Financial ledger, billing systems Analytics, unique visitor dashboards

How does HyperLogLog handle statistical noise and outliers?

HyperLogLog reduces statistical noise by dividing the incoming hashed values into multiple independent buckets (or registers) and averaging their results. It uses a harmonic mean instead of a simple average to prevent extreme outliers from skewing the final estimate.

If you only used a single coin-flipper, one lucky user ID with an unusually long string of ones would throw off your entire estimate. To fix this, the algorithm splits the hash: the first few bits determine which bucket the data goes into, and the rest of the string is analyzed for the run of ones. By averaging these buckets using a harmonic mean, the statistical noise is smoothed out, leaving us with an incredibly accurate estimate.

I love this data structure so much because it shows how deep, creative computer science can solve massive infrastructure problems with elegant simplicity.

FAQ

What is the accuracy rate of HyperLogLog?

HyperLogLog typically operates with a standard error of less than 1%. For most analytics dashboards, unique visitor trackers, and video view counters, this negligible margin of error is a perfectly acceptable trade-off for the massive hardware savings.

Can HyperLogLog tell you if a specific user has viewed a video?

No, HyperLogLog cannot tell you if a specific user has viewed a video because it does not store any user IDs. Once a hashed ID is processed, its identity is discarded, leaving behind only the statistical metadata of its bit pattern.

When should I use HyperLogLog over a Bloom Filter?

You should use HyperLogLog when you need to calculate the total number of unique items (cardinality estimation). You should use a Bloom Filter when you need to answer membership queries, such as checking if a specific username has already been taken during registration.

Top comments (0)