Working with Probabilistic Data Structures

1. Understanding Bloom Filters

PropertyDetail
OperationAdd + membership query (no delete)
ErrorsFalse positive possible; false negative never
Optimal hashesk = (m/n) × ln(2)
FP rate(1 − e^(−kn/m))^k
Use casesCache pre-check, dedup, LSM SST filter

Example: Guava BloomFilter

BloomFilter<String> bf = BloomFilter.create(
    Funnels.stringFunnel(StandardCharsets.UTF_8),
    1_000_000,   // expected insertions
    0.01);        // false positive rate

bf.put("user:42");
bf.mightContain("user:42"); // true
bf.mightContain("user:99"); // probably false

2. Implementing Counting Bloom Filters

PropertyDetail
Cells4-bit counters instead of bits
SupportsAdd, remove, query
Cost~4× space of standard Bloom
RiskCounter overflow (cap at max)

3. Implementing HyperLogLog

PropertyValue
PurposeApproximate cardinality (distinct count)
Memory~12KB for <2% error
Standard error1.04 / √m
MergeableYes — combines for federated count
Used byRedis PFCOUNT, BigQuery APPROX_COUNT_DISTINCT

4. Understanding Count-Min Sketch

PropertyDetail
PurposeApproximate frequency of items in stream
Structured × w matrix; d hash functions
Error boundOverestimate ≤ ε·N with prob 1−δ
Use casesHeavy hitters, network anomaly detection

5. Implementing Cuckoo Filters

PropertyDetail
vs BloomSupports delete; better space-efficiency at low FP
MechanismCuckoo hashing of fingerprints
Lookup2 buckets only; very fast
RiskInsert failure when load > ~95%

6. Understanding MinHash for Similarity

PropertyDetail
PurposeEstimate Jaccard similarity of sets
Mechanismk hash functions; record min hash per set
EstimateFraction of matching min-hashes
Used byNear-duplicate detection (LSH), recommendations

7. Implementing Approximate Membership Queries

StructureBest For
BloomStatic sets, no delete
Counting BloomDynamic with delete
CuckooLower FP rate, supports delete
Quotient filterCache-friendly, mergeable

8. Understanding False Positive Rates

FilterBits/element @ 1% FP
Bloom~9.6
Counting Bloom (4-bit)~38
Cuckoo (16-bit fingerprint)~13
Quotient~12

9. Implementing Space-Efficient Counting

StructureUse Case
HyperLogLogDistinct count
Count-Min SketchFrequency per item
Top-K (Stream-Summary)Heavy hitters
T-Digest / DDSketchStreaming quantiles

10. Understanding Trade-offs in Probabilistic Structures

TradeDetail
Space ↓ vs Accuracy ↓Tunable via parameters
Speed ↑O(1) operations
No exact answersBest for "good enough" use cases
MergeabilityCritical for distributed aggregation