Sub topic 14
Related structures: cuckoo and xor filters
This chapter assumes you know how a standard Bloom filter inserts and queries items, why it has no false negatives, and how its false positive rate depends on m, k and n. It also assumes you have seen the deletion problem and counting Bloom filters.
Why look beyond Bloom filters
A Bloom filter needs about 1.44 · log2(1/ε) bits per item for a false positive rate ε. Its lookups touch k scattered memory locations, and it cannot delete. Two newer structures address these points. Both store short fingerprints (a few bits of an item's hash) instead of setting shared bits.
Cuckoo filters
A cuckoo filter is a table of buckets, each holding a few fingerprints. Every item has two candidate buckets. Insertion places the fingerprint in either one. If both are full, it evicts a resident fingerprint to that fingerprint's other bucket, and repeats up to a fixed number of times.
The filter keeps only fingerprints, not the original items. So it finds the other bucket from the fingerprint alone, using partial-key cuckoo hashing: i2 = i1 XOR hash(fingerprint). XOR is its own inverse, so the same formula maps either bucket to the other.
import hashlib
def _h(data):
return int.from_bytes(hashlib.sha256(data).digest()[:8], "big")
def candidates(item, num_buckets, fp_bits=8):
"""num_buckets must be a power of two."""
digest = hashlib.sha256(item.encode("utf-8")).digest()
i1 = int.from_bytes(digest[:8], "big") % num_buckets
fp = int.from_bytes(digest[8:10], "big") % (2 ** fp_bits - 1) + 1 # never 0
i2 = i1 ^ (_h(fp.to_bytes(2, "big")) % num_buckets)
return fp, i1, i2
def alternate(bucket, fp, num_buckets):
return bucket ^ (_h(fp.to_bytes(2, "big")) % num_buckets)
fp, i1, i2 = candidates("apple", 1024)
print(fp, i1, i2)
print(alternate(i2, fp, 1024) == i1) # True: XOR is reversibleA lookup checks the fingerprint in at most two buckets. Deletion removes one matching fingerprint. This is safe only for items that were actually inserted. Deleting an item that was never added can remove another item's fingerprint and create a false negative.
The cost is that insertion can fail when the table is nearly full. You then have to resize or rebuild. Inserting the same item many times also overflows its two buckets, so duplicates must be controlled.
Xor filters
An xor filter is static. You build it once from the complete key set, and you cannot add or remove items afterward. The table has about 1.23 · n slots of fingerprints. Each key hashes to three slots, one in each third of the table. The build step assigns slot values so that, for every key:
fingerprint(key) == table[h0] XOR table[h1] XOR table[h2]
A query computes the same three positions, XORs the stored values, and compares the result with the key's fingerprint. An absent key matches only by chance, with probability about 2^-r for r-bit fingerprints.
With 8-bit fingerprints, the false positive rate is about 0.39% at roughly 9.84 bits per item. A Bloom filter needs about 11.5 bits per item for the same rate. Queries always read exactly three locations.
Building can fail for an unlucky hash seed. The usual fix is to retry with a new seed.
Comparing the options
| Property | Bloom | Counting Bloom | Cuckoo | Xor |
|---|---|---|---|---|
| Insert after build | Yes | Yes | Yes, can fail when full | No |
| Delete | No | Yes | Yes | No |
| Memory reads per lookup | k | k | 2 buckets | 3 |
| Space at low ε | Highest | About 4x Bloom | Lower | Lowest |
Exercise
A service builds a filter once per night from a fixed list of 50 million blocked URLs. The filter is then read-only until the next build. Which structure fits best, and why might a cuckoo filter be a worse choice?
Show answer
An xor filter fits best. The key set is static, so its smaller size and fixed three-read lookups are an advantage. A cuckoo filter supports inserts and deletes the service does not need. It also uses more space at the same false positive rate and can fail an insert when full.