Thứ Hai, 24 tháng 8, 2015

Bloom Filter

Creating a Simple Bloom Filter

Bloom filters are super efficient data structures that allow us to tell if an object is most likely in a data set or not by checking a few bits. Bloom filters return some false positives but no false negatives. Luckily we can control the amount of false positives we receive with a trade off of time and memory.
You may have never heard of a bloom filter before but you've probably interacted with one at some point. For instance if you use Chrome, Chrome has a bloom filter of malicious URLs. When you visit a website it checks if that domain is in the filter. This prevents you from having to ping Google's servers every time you visit a website to check if it's malicious or not. Large databases such as Cassandra and Hadoop use bloom filters to see if it should do a large query or not.
From Cassandra's Architecture Overview:
Cassandra uses bloom filters to save IO when performing a key lookup: each [Sorted String Table] has a bloom filter associated with it that Cassandra checks before doing any disk seeks, making queries for keys that don't exist almost free.

What You'll Need

  • A bit array (as the name suggests it's just an array of bits)
  • A quick non-cryptographic hash function like murmurhash3 or cityhash
For Python you can do:
  1. sudo pip install bitarray
  2. sudo pip install mmh3

How They Work

A bloom filter is essentially a huge bit array. Bloom filters work by hashing an object several times using either multiple hash functions or the same hash function with a different seed. This insures that when we hash an object we're unlikely to get the same result. For each time an object is hashed the corresponding hash value in the bit array is then marked as 1. This is why we use a bit array. Rather than needing say 4 bytes to store a 1 or a 0 we can simply do it in a bit.
Here's an example to help make it easier to understand. Note we mod by the size of the bit array to prevent index out of bounds:
  1. from bitarray import bitarray
  2. import mmh3
  3.  
  4. bit_array = bitarray(10)
  5. bit_array.setall(0)
  6.  
  7. b1 = mmh3.hash("hello", 41) % 10 #Equals 0
  8. bit_array[b1] = 1
  9. b2 = mmh3.hash("hello", 42) % 10 #Equals 4
  10. bit_array[b2] = 1
At this point our bit array looks like this: 1000100000
If we want to check if "hello" is in our data set we do the same opreation back.
  1. b1 = mmh3.hash("hello", 41) % 10 #Equals 0
  2. b2 = mmh3.hash("hello", 42) % 10 #Equals 4
  3. if bit_array[b1] == 1 and bit_array[b2] == 1:
  4. print "Probably in set"
  5. else:
  6. print "Definitely not in set"
The reason it is only probably in the set is because a combination of items added to the data set could end up setting the same bits to 1. For instance say we have an empty bloom filter and maybe hashing "world" with seed 41 equalled 0 and hashing "foo" with seed 42 equalled 4. Then when you attempt to search for "hello" you'd get that it is probably in the set even though it was never added. You can prevent this from happening by using a larger bit array and more hash functions. There is some math to choosing these numbers that I'll get in to towards the end of the post.

The Class

Alright let's do this! Bloom filters take in 2 variables: size and number of times to run our hash function(s).
  1. from bitarray import bitarray
  2. import mmh3
  3.  
  4. class BloomFilter:
  5. def __init__(self, size, hash_count):
  6. self.size = size
  7. self.hash_count = hash_count
  8. self.bit_array = bitarray(size)
  9. self.bit_array.setall(0)
  10. def add(self, string):
  11. #Awesome code goes here
  12. def lookup(self, string):
  13. #Awesome code goes here
Using our knowledge from earlier we can create a function to add items to our filter in just 3 lines of code.
  1. def add(self, string):
  2. for seed in xrange(self.hash_count):
  3. result = mmh3.hash(string, seed) % self.size
  4. self.bit_array[result] = 1
We hash the string with our hash function modded by the size of the bit array and then set that value in our bit array to 1. For each iteration our seed increases by 1 so that we can get different numbers from each hash.
The code for the lookup function is almost the same as the add function. For each hash we're just checking if the resulting value is set to 1 or 0 in our bit array. If we find a 0 then we respond back with "Nope". Otherwise it's probably there so we respond back with "Probably".
  1. def lookup(self, string):
  2. for seed in xrange(self.hash_count):
  3. result = mmh3.hash(string, seed) % self.size
  4. if self.bit_array[result] == 0:
  5. return "Nope"
  6. return "Probably"
That's it. That's a bloom filter. Pretty simple right?

Finding The Right Values

The big thing we want to control when creating our bloom filter is our false positive rate. To do this we'll need to have an idea of the size of our data set. Wikipedia has the math and fancy graphs behind choosing your values. Here's what you need to know though:
The formula to calculate the size of your bit array (m = size of bit array, n = expected number of items, and p = probability percentage represented as a decimal):
m equals negative n times the natural log p all over the natural log of 2 sqaured
The formula to calculate the number of hashes to use (k = number of hashes to use):
k equals negative m over n times the natural log of 2
You can also play around with values using this formula which will give you your false positive probability:
false probability equals ( 1 minus [ 1 minus 1 over m] raised to the power k times n ) raised to the power of k

Testing Our Code

The built-in american english dictionary on Ubuntu has about 100,000 words in it so let's use that with a bloom filter. According to WolframAlpha a bloom filter with 1.1 million bits and 7 hash functions has about a 0.5% chance of a false positive.
  1. from bitarray import bitarray
  2. import mmh3
  3.  
  4. class BloomFilter:
  5. def __init__(self, size, hash_count):
  6. self.size = size
  7. self.hash_count = hash_count
  8. self.bit_array = bitarray(size)
  9. self.bit_array.setall(0)
  10. def add(self, string):
  11. for seed in xrange(self.hash_count):
  12. result = mmh3.hash(string, seed) % self.size
  13. self.bit_array[result] = 1
  14. def lookup(self, string):
  15. for seed in xrange(self.hash_count):
  16. result = mmh3.hash(string, seed) % self.size
  17. if self.bit_array[result] == 0:
  18. return "Nope"
  19. return "Probably"
  20.  
  21. bf = BloomFilter(500000, 7)
  22.  
  23. lines = open("/usr/share/dict/american-english").read().splitlines()
  24. for line in lines:
  25. bf.add(line)
  26.  
  27. print bf.lookup("google")
  28. >>> Nope
  29. print bf.lookup("Max")
  30. >>> Probably
  31. print bf.lookup("mice")
  32. >>> Probably
  33. print bf.lookup("3")
  34. >>> Nope
Here's some code we can use to do a time comparison. We'll create a huge list of all our words then iterate through them one by one til we find our target string.
  1. bf = BloomFilter(500000, 7)
  2. huge = []
  3.  
  4. lines = open("/usr/share/dict/american-english").read().splitlines()
  5. for line in lines:
  6. bf.add(line)
  7. huge.append(line)
  8.  
  9. import datetime
  10.  
  11. start = datetime.datetime.now()
  12. bf.lookup("google")
  13. finish = datetime.datetime.now()
  14. print (finish-start).microseconds
  15. >>> 57
  16.  
  17.  
  18. start = datetime.datetime.now()
  19. for word in huge:
  20. if word == "google":
  21. break
  22. finish = datetime.datetime.now()
  23. print (finish-start).microseconds
  24. >>> 9891
  25.  
  26.  
  27. start = datetime.datetime.now()
  28. bf.lookup("apple")
  29. finish = datetime.datetime.now()
  30. print (finish-start).microseconds
  31. >>> 10
  32.  
  33.  
  34. start = datetime.datetime.now()
  35. for word in huge:
  36. if word == "apple":
  37. break
  38. finish = datetime.datetime.now()
  39. print (finish-start).microseconds
  40. >>> 1935
Those are some pretty big time differences. We saw an over 10,000% performance increase which can really make a difference when doing expensive lookups. Not to mention the memory footprint is much smaller than storing each item in a list. 1.1 million bits is only 0.13 megabytes. Not bad at all if you ask me.
You can grab the source from this post on GitHub.