System Designcalculator

Bloom filter sizer

A Bloom filter answers “have I seen this item?” in a fixed amount of memory. It never says no to an item it holds, but it sometimes says “maybe” to an item it has never seen. That is a false positive, and you choose how often it happens by choosing the size.

Memory needed

1,000,000 items at a 1% target need 1.2 MB (9,585,059 bits) and 7 hash functions, for a false-positive rate of 1%.

Memory1.2 MB9,585,059 bits
1%9.91%510Hash functions (k)False-positive rateBest: k = 7

Your numbers

How many distinct items the filter will hold.

Share of lookups for items never added that still answer “maybe”.

Sizing

Memory1.2 MB9,585,059 bits
Hash functions7Ideal (m ÷ n) ln 2 = 6.64
False-positive rate1%With 7 hashes
Bits per item9.591,000,000 items

Try a 64-bit filter

Add words, then check words you never added. A check answers “maybe” only when all of its bits are set. With the starting words and 3 hash functions, try “cheese” or “salt”: never added, yet every one of their bits is set. Words can’t be taken out again, because a bit may be shared with another word.

19 of 64 bits set by 8 words; expected false-positive rate 3.1%. Outlined: the bits for “peach”.

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 16
  18. 17
  19. 18
  20. 19
  21. 20
  22. 21
  23. 22
  24. 23
  25. 24
  26. 25
  27. 26
  28. 27
  29. 28
  30. 29
  31. 30
  32. 31
  33. 32
  34. 33
  35. 34
  36. 35
  37. 36
  38. 37
  39. 38
  40. 39
  41. 40
  42. 41
  43. 42
  44. 43
  45. 44
  46. 45
  47. 46
  48. 47
  49. 48
  50. 49
  51. 50
  52. 51
  53. 52
  54. 53
  55. 54
  56. 55
  57. 56
  58. 57
  59. 58
  60. 59
  61. 60
  62. 61
  63. 62
  64. 63
  • applebits 16, 7, 62
  • bananabits 47, 28, 9
  • cherrybits 58, 5, 16
  • grapebits 47, 50, 53
  • lemonbits 6, 43, 16
  • mangobits 41, 20, 63
  • orangebits 51, 60, 5
  • peachbits 48, 5, 26

What it shows

A Bloom filter has m bits and k hash functions. Adding an item sets the k bits its hashes point to. Checking an item looks at the same k bits. If any is clear, the item was never added. If all are set, it probably was.

Choose what to work out: the memory for a target false-positive rate, the rate for a size you have, or how many items fit. A 64-bit demo below lets you add words and watch false positives happen.

How to read it

  • Hash functions is the whole number that gives the lowest rate, close to (m ÷ n) ln 2.
  • Bits per item sets the rate. Each 10× lower false-positive rate costs about 4.8 more bits per item, whatever the number of items.
  • The chart shows the rate for each number of hashes. Too few and items leave too little evidence. Too many and the bits fill up. The marker is the best k.
  • In the demo, outlined bits belong to the word you are checking. “False positive” means a never-added word had every bit set by others.

Questions

Can a Bloom filter give a false negative?

Not if items are only added. Adding an item sets all of its bits, and nothing in a standard Bloom filter clears a bit, so checking that item always finds them set.

Can I remove an item from a Bloom filter?

Not from a standard one. A bit may have been set by several items, so clearing it could make other items look absent. Rebuild the filter, or use a variant that keeps a count per position instead of a single bit.

Do I need k different hash functions?

No. Kirsch and Mitzenmacher showed that two hash functions, combined as h1 + i × h2 for i from 0 to k − 1, give the same asymptotic false-positive rate. The demo filter here works that way, using MurmurHash3 with two seeds.

How accurate are these numbers?

They use the standard approximation p ≈ (1 − e^(−kn/m))^k, which is close for the large filters used in practice. Very small filters, like the 64-bit demo, can stray from it.

Learn it properly

Sources

Explore further

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.