02 The Theory
Bloom Filters 101
Now onto the fun part, math! Whenever you work with Bloom Filters you have to take into account the guarantees:
- If negative. Guarantees that the input is NOT in the list of strings.
- If positive. The input may or may NOT be in the filter. Meaning it could be a true positive and a false positive.
Why? Because of the same reason Bloom Filters are incredibly fast. They do not work like any other seraching algorithm, they do not query each element in the list and check for equality. Rather they apply a hash function to the existing data and perform a modulus operation on its value. It begins with a large list of bits at 0 which we will name B (Bloom). The size of the list is variable and will be from now on called L (length). Once we have the result of the hash function, which will be referrenced as F (Function). We modulus it by L and change the resulting digit index of B into a 1. Like so:
- Input :"/admin" L: 8 B: [0,0,0,0,0,0,0,0]
- F("/admin") -> qwerty
- qwerty (ASCII addition) % L -> 5
- B: [0,0,0,0,0,1,0,0]
Now in this example with an original list of 4 from now on referred to as T (Targets). The resulting list of bits will be a bloom filter and:
- Resulting B: [1,0,1,0,0,1,1,0] Inputs: ["/hi","/bye", "/admin"]
- F("/hi") % 8 -> 1. B[1] -> 0. True negative.
- F("/bye") % 8 -> 6. B[6] -> 1. False positive.
- F("/admin") % 8 -> 5. B[5] -> 1. True positive.
In this simple example we can clearly see the speed benefit, as a lookup on a bloom filter is now O(F) which we want to be faster than O(n). Another thing that arises is the simple reality that if L is not much bigger than T you are gonna get a LOT of false positives and that is when things get interesting.
Reducing False Positives
As you may have already figured out, there are multiple ways of reducing false positives. We are going to be using:
- Increasing L based on a target increase from T.
- Performing multiple F. Denoted from now on as Fn, where n is the number of hash functions.
When we increase n, we expect a positive to be a list of size n with all values equal to 1. The moment there is a 0, we will have a true negative.
I could try and show the optimal equations for calculating the optimal number for L and n. But the internet is a thing and people have already figured this math out [1]. So I wont.
Optimal bit array size:
L = -( T × ln(p) ) / (ln(2))² # where p is the desired false positive probaility
Optimal number of hash functions:
n = (L / T) × ln(2)
So the goal will be to calculate programatically these two numbers at application startup. One thing to keep in mind is that if n is too high, the number of bits that will be 1 will be too high and the false positive probability increases.
Finding The Right Hash Functions
The hash function that we should use should be distributed in a way that removes collisions and repeatable so that with a different ket we can simply increase the size of n. For this, we are going to be using murmur as our hash function. Why? Because it seems the most agreeed upon and even chromium uses it. [2]
Well i'll be more honest, I will be trying more different kinds like fnv1a. But I think that making this decision by using examples will be better than actually eating my head over it.
That will be the basic idea of the theory behind it!