Practice this topic in a realistic system design interview
Analytics systems often need to answer questions like:
Counting these exactly means remembering every item you've seen, which gets expensive fast. HyperLogLog estimates the count instead, using only a few kilobytes of memory.
In this chapter, we'll learn what HyperLogLog is, how it can estimate the number of unique items in a huge dataset using only a few kilobytes of memory, and where systems use it in practice.
Suppose you're running a large content platform. You want to show how many unique readers each article gets in a day.
We're not interested in the total number of views, but in how many different people actually read the article. If someone opens the same article ten times, they should still count as just one reader.
There's a simple, exact way to track this. For each article, maintain a set of user IDs. Whenever someone views the article, we add their ID to the set. Since a set only stores unique values, repeated views from the same user won't increase its size.
At the end of the day, we simply count the IDs in the set to get the number of unique readers.
Now let's look at how much memory this approach requires.
Suppose a popular article gets 50 million unique readers in a day. If each user ID takes 8 bytes, storing those IDs alone takes about 400 MB, even before counting the hash set overhead. And that's just for one article on a single day.
Now imagine your platform has a million articles. And you also want to track unique readers by week, month, country, and device type. Each of these combinations needs its own set, so memory usage grows very quickly.
The more unique readers you have, and the more ways you slice them, the more data you need to store. And if that data is spread across multiple servers, combining exact counts means sending all those IDs over the network.
| Question | Exact set | HyperLogLog |
|---|---|---|
| Unique readers of one article today | Grows with readers (~400 MB for 50M) | ~12 KB |
| Unique readers this week | Re-dedupe all the daily IDs | Merge 7 daily sketches |
| Unique readers per country and device | One large set per combination | One small sketch per combination |
| Unique readers across servers | Ship all IDs to one place | Ship ~12 KB per server |
This brings us to an important question: can we count unique items without storing every single one?