Sub topic 1
The problem: counting items in a stream with limited memory
A data stream is a sequence of items that arrives one at a time, and you usually see each item only once. Examples are search queries, network packets, page views, and log lines. A common question about a stream is: how many times has item x appeared so far? This chapter explains why that question is hard at scale and what kind of answer a count-min sketch gives you. Later chapters build the structure itself.
Prerequisites
This chapter assumes you know:
- Basic Python, including dictionaries and loops.
- How hash tables work, and that a hash function maps a key to a number.
- Basic probability, such as expected value.
- Big-O notation.
The exact solution
The obvious method is a dictionary that maps each item to its count. Python's Counter does this.
from collections import Counter
import random
random.seed(1)
counts = Counter()
for n in range(1, 1_000_001):
counts[random.randrange(10_000_000)] += 1
if n in (1_000, 10_000, 100_000, 1_000_000):
print(n, len(counts))Each line prints the number of items read and the number of dictionary entries. The entry count stays close to the number of items read, because most random items are new. Exact counting needs one entry per distinct item, so memory grows with the number of distinct items. It does not grow with the number of events.
Why this breaks down
Suppose a service sees one billion distinct URLs in a day. A dictionary entry holds a key, a counter, and table overhead. Assume about 100 bytes per entry. That comes to roughly 100 GB for one counting table. Three further constraints make it worse:
- The table must stay in fast memory, because every arriving item triggers an update.
- You may need one table per time window, per region, or per customer.
- Tables on different machines must be combined, which means sending the whole table over the network.
What a sketch trades away
A sketch is a summary of the stream with a size you choose before the stream starts. It processes each item in constant time and never stores the items themselves. In exchange, it gives up exactness. You get an estimate of each count, and you accept a bounded error.
The count-min sketch makes this trade in a specific way:
- Memory is fixed up front and does not depend on how many distinct items appear.
- Each update and each query takes the same small amount of work.
- Estimates are never below the true count. They can be too high.
- The size of the possible overcount is set by parameters you pick.
The error is stated relative to N, the total number of items seen so far. You can say "overcount by at most 0.1% of N, with 99% probability." Later chapters define this as the (ε, δ) guarantee.
When approximate is good enough
Many questions only need roughly right answers for the large counts:
- Which queries are trending? A count of 1,000,000 versus 1,000,050 does not change the answer.
- Is this IP address sending far more traffic than normal?
- Roughly how popular is this item, so a cache can decide whether to keep it?
An error of a few hundred on a count of a million is harmless. The same error on an item that appeared 3 times can be large in relative terms. Keep this in mind, because it is the main limit of the method.
Exercise
A stream has 2 billion events but only 5,000 distinct items. Does exact counting with a dictionary have a memory problem here? What if the stream has 2 billion distinct items?
Show answer
With 5,000 distinct items, a dictionary holds only 5,000 entries, so memory is small and exact counting is the better choice. With 2 billion distinct items, it needs about 2 billion entries, which is too much memory. Exact counting memory depends on distinct items, not on total events.