Sub topic 1
Why Counting Distinct Values Is Expensive
Prerequisites
This chapter assumes you know basic Python, including sets and dictionaries. You should also be comfortable with binary numbers and with the idea of a hash function. No probability is needed yet.
The problem
Suppose a website logs every page view with a user ID. You want to know how many different users visited today. This is the distinct counting problem, also called the cardinality estimation problem. The number of distinct items is the cardinality of the stream.
A plain event count is cheap. You keep one integer and add 1 for each event. Counting distinct items is harder because the same ID can arrive many times, and you must count it only once.
The exact solution
The direct approach is to remember every ID you have seen.
def count_distinct(stream):
seen = set()
for item in stream:
seen.add(item)
return len(seen)
print(count_distinct(["alice", "bob", "alice", "carol", "bob"])) # 3This is correct. Its cost is the problem. The set must hold every distinct item, so its memory grows in proportion to the cardinality. If there are n distinct items, you need space for n items.
What this costs in practice
A Python set of strings uses far more than the raw characters, because each entry carries object and hash-table overhead. Even with a compact representation, such as one 16-byte ID per user, the cost adds up quickly.
| Distinct users | Raw ID storage at 16 bytes each |
|---|---|
| 1 million | 16 MB |
| 100 million | 1.6 GB |
| 10 billion | 160 GB |
Real systems rarely count only one thing. A dashboard may track distinct visitors per page, per country, per hour, and per campaign. Each counter needs its own set, so the totals multiply.
Distributed systems add another cost. If ten servers each see part of the traffic, one server cannot just add their counts, because the same user may appear on several servers. You would have to ship every ID to one place and merge the sets, which costs network traffic as well as memory.
Why you cannot just shrink the set
You might try to reduce memory with a smaller structure. Each option has a catch:
- Storing only hashes of items instead of the items themselves helps with item size. Memory still grows linearly with the number of distinct items.
- A Bloom filter, which is a compact structure that tests whether an item was probably seen, is smaller than a set. Its size must still be chosen in advance based on the expected number of items, and it grows with them.
- A bitmap with one bit per possible ID is compact only when IDs fall in a small range. For 128-bit IDs it is impossible.
There is also a theoretical limit. To count exactly, an algorithm must be able to tell apart every possible set of items seen so far. That requires memory proportional to the cardinality. So no clever exact algorithm exists. To use less memory, you must accept an approximate answer.
The trade we want
In many applications an answer such as "about 10.2 million visitors" is as useful as "10,213,447 visitors". Dashboards, capacity planning, and anomaly detection rarely need the last digits. In return for a small error we want:
- Memory that stays fixed no matter how many distinct items arrive.
- One pass over the data, with constant work per item.
- Sketches from separate servers that can be combined without sending raw IDs.
HyperLogLog meets all three goals. The rest of this tutorial builds it step by step. It begins with hashing, so that every item becomes a string of random-looking bits.
Exercise
A service has 200 million distinct users, each identified by a 16-byte ID. About how much memory does an exact set need for the raw IDs alone, and why does merging counts from two servers by simple addition give a wrong answer?
Show answer
About 3.2 GB (200 million × 16 bytes), and more in practice because of set overhead. Adding the two servers' counts is wrong because a user seen by both servers is counted twice. Only the union of the two sets gives the distinct count.