Concept Simulators / Storing and finding data
Bloom filters and sketches
Bloom filter, HyperLogLog and count-min sketch: small memory, small known error.
On the roadmap:Staff Engineer · step 2: Approximate answers at scale
Settings
Which structure
Bits set
31%
Wrongly says yes
2.8%
Formula predicts
2.9%
Memory
1.0 KB
2.8% of absent items were wrongly reported as present, against 2.9% predicted. The best number of hash functions for this size is 6. A 'no' is always right.
The first 1,024 bits (dark means set)
False positives as the number of hash functions changes
false positive rate
In plain words
Some questions do not need an exact answer. A Bloom filter says whether an item is probably in a set, HyperLogLog estimates how many different items there are, and a count-min sketch estimates how often each item appears. Each uses a tiny, fixed amount of memory, and pays with a small, known error.
Why it matters
Keeping every user, key or event in memory is impossible at large scale, and often unnecessary. A structure a thousand times smaller that is right to within a few per cent, and wrong in one safe direction, can answer the question that matters. Here you can resize each one and watch the error respond.
How real systems do it
The simulation is a simplified model. Here is what production systems add or do.
- Bigtable, HBase, Cassandra and RocksDB keep a Bloom filter for each file of data, so a lookup for a key that is not there usually skips reading the file at all.
- Redis has HyperLogLog built in (PFADD and PFCOUNT). Each one uses at most about 12 KB and has a standard error of about 0.81%, however many items it counts.
- Large analytics systems offer approximate distinct counts, such as approx_count_distinct in Spark and Presto and BigQuery's APPROX_COUNT_DISTINCT, which use HyperLogLog variants.
- Count-min sketches track heavy hitters in a stream, such as trending queries, without storing every item. A false positive from a Bloom filter or an overcount from a sketch is acceptable when the cost of being wrong is small.
Study: Bloom filters and sketches
Once you have played with it, learn the same idea from people who build these systems. Watch first for the picture, then read for the detail.
6 minBloom Filters | Algorithms You Should Know #2 | Real-world Examples
ByteByteGo · 307K views
Then
27 minWhat are Bloom Filters? - HashingGaurav Sen · 145K views
24 minBloom filter for System Design | Bloom filter applications | learn bloom filter easilyTech Dummies - Narendra Lakshmana Gowda · 116K views
20 minCount min sketch | Efficient algorithm for counting stream of data | system design componentsTech Dummies - Narendra Lakshmana Gowda · 110K views
26 minData Structures for Big Data in Interviews - Bloom Filters, Count-Min Sketch, HyperLogLogHello Interview · 73K views