Sub topic 12
Limitations and common pitfalls
This chapter assumes you have read the earlier chapters and have the HyperLogLog class from the implementation chapter. You should know what registers, rank, precision p and merging by register-wise maximum mean.
HyperLogLog gives up exactness and per-item information to stay small. Most mistakes come from asking it for something it never stored.
What a sketch cannot do
A sketch holds m small integers (the maximum rank seen in each register). Everything it cannot do follows from that.
- No membership test. You cannot ask "has
alicebeen seen?". Many items map to the same register, and only the largest rank is kept. Use a Bloom filter or a set for membership. - No deletion. A register stores a maximum. If you remove an item, you cannot tell whether it was the one that set the register, or what the second-largest rank was. To count over a sliding window, keep one sketch per time bucket and merge the recent buckets.
- No listing of items. The sketch stores no items, so you cannot export "the distinct users".
- No exact answer. The standard error is about
1.04 / sqrt(m), which is 1.63% atp=12. This is a typical error, not a guarantee. Individual results can fall outside one standard error.
Intersections are unreliable
Merging gives a union directly. An intersection is not directly available, but you can estimate it with inclusion-exclusion: |A ∩ B| = |A| + |B| - |A ∪ B|.
a = HyperLogLog(p=12)
b = HyperLogLog(p=12)
for i in range(100_000):
a.add(f"user-{i}")
for i in range(99_000, 199_000): # overlaps with a on 1,000 users
b.add(f"user-{i}")
union = HyperLogLog(p=12)
union.merge(a)
union.merge(b)
intersection = a.estimate() + b.estimate() - union.estimate()
print(round(intersection)) # true value is 1000Each of the three estimates can be off by about 1,600. Those errors add up, so the result can be off by thousands, and it can even come out negative. The true overlap here is only 1,000, which is smaller than the noise. Inclusion-exclusion works only when the intersection is a large share of the union. For small overlaps, use a different structure, such as a MinHash or theta sketch.
Hash quality and consistency
The whole analysis assumes the hash output behaves like uniform random bits. Three failure modes follow.
- A weak hash. Hashes with poor bit mixing, such as a trivial integer hash, skew the rank distribution and bias the estimate. Use a well-mixed hash such as MurmurHash3, xxHash, or truncated SHA-1 as in our class.
- Too few bits. With a 32-bit hash, estimates near several billion distinct items collide and saturate. This is why HyperLogLog++ moved to 64-bit hashes.
- Different hashes in different places. Merging only makes sense if both sketches hashed the same item to the same value. Python's built-in
hash()on strings changes between processes because of hash randomization. Sketches built from it in two workers cannot be merged meaningfully. Always use an explicit, stable hash function.
Normalize items before hashing. "Alice" and "alice " are different strings and count as two items.
Merge pitfalls
- Sketches must share the same
p. OurmergeraisesValueErrorotherwise. A sketch with a largerpcan be folded down to a smaller one, but our class does not implement this. - They must also use the same hash function and seed.
- Merging is idempotent: merging a sketch with itself, or merging the same data twice, changes nothing. This is a benefit. It means retried jobs do not double-count.
Redis-specific points
PFCOUNTon a single key caches its result, butPFCOUNTon several keys merges them on the fly each time, which costs more.PFADDon a key holding a non-HyperLogLog string raises an error. Keep sketch keys separate from other data.- Redis uses a fixed precision (
p=14, 16,384 registers, about 12 KB when dense, with error about 0.81%). You cannot tune it.
Summary of pitfalls
| Wish | Why it fails | Alternative |
|---|---|---|
| "Was X seen?" | Only maxima per register are stored | Bloom filter |
| Remove an item | Maximum cannot be undone | Per-window sketches |
| Small intersection | Errors of three estimates add up | MinHash, theta sketch |
| Exact count | Estimate has about 1.6% error at `p=12` | Exact set or database |
| Merge across services | Different hash or `p` | Fix both up front |
Exercise
A team keeps one HyperLogLog per day and wants "users active on both Monday and Tuesday". They compute |Mon| + |Tue| - |Mon ∪ Tue|. About 2 million users are active each day and the true overlap is about 5,000. Why is this a poor design, and what would you check first?
Show answer
The three estimates each carry an error of tens of thousands at typical precision (1.63% of 2 million is about 32,000), which is far larger than the 5,000 overlap, so the result can be wildly wrong or negative. Inclusion-exclusion only works when the intersection is a large share of the union. Use a sketch designed for set operations (theta or MinHash), or an exact method for this query. Before that, also check that both sketches use the same p and the same stable hash function.
Exercise
Two workers each build a sketch with Python's built-in hash(item) and send them to a server to merge. Why is the merged estimate wrong?
Show answer
String hashing in Python is randomized per process, so the same item gets different hash values in each worker. The same user is counted as two different items, and the union is overcounted. Use a stable hash such as SHA-1 or MurmurHash3 with a fixed seed.