Like a Bloom filter, but you can also delete items.
A cuckoo filter is a small structure, like a Bloom filter, that answers "is this item in the set?" with "definitely not" or "maybe yes". The big difference: it can also delete items.
For each item the filter computes a short fingerprint (for example 8 bits) and two bucket numbers. The first bucket is a hash of the item. The second is the first bucket combined (XOR) with a hash of the fingerprint:
fingerprint = short_hash(item) bucket1 = hash(item) % number_of_buckets bucket2 = bucket1 XOR hash(fingerprint)
Because XOR works both ways, bucket1 = bucket2 XOR hash(fingerprint) too. So given only a bucket and the fingerprint stored in it, the filter can find the other bucket without knowing the original item. That is what makes kicking out possible.
| Operation | What it does | Cost |
|---|---|---|
| Insert | Put the fingerprint in a free slot of bucket 1 or 2. If both are full, kick one out and relocate it, repeat up to a limit. | Fast on average. Slower when the table is nearly full. May fail. |
| Lookup | Check the slots of the two buckets for the fingerprint. | At most two bucket reads. |
| Delete | If the fingerprint is in one of the two buckets, clear one copy. | At most two bucket reads. Only delete items you inserted. |
| Setting | Effect | Note |
|---|---|---|
| Fingerprint size (f bits) | Longer means fewer false positives, but more memory | False positive rate is roughly 2 x slots / 2^f |
| Slots per bucket | More slots allow a fuller table | Four slots per bucket can reach around 95% full. One slot only about half. |
| Number of buckets | Sets capacity | Often kept as a power of two so XOR stays inside the table |
| Max kicks | How hard to try before giving up | A few hundred is common. After that the insert fails and the table is too full. |
Bucket 2 is bucket 1 XOR hash(fingerprint), so a kicked fingerprint can find its other home without the original word. Each word uses the same fixed hash, so the picture repeats. Delete is safe only for a word that really was inserted.
insert(item):
f = fingerprint(item); i1 = hash(item) % B; i2 = i1 XOR hash(f)
if bucket[i1] or bucket[i2] has a free slot: store f there; return ok
i = pick i1 or i2
repeat up to MAX_KICKS:
swap f with a random fingerprint in bucket[i]
i = i XOR hash(f) // the evicted one goes to its other bucket
if bucket[i] has a free slot: store f; return ok
return full| Term | Meaning in easy words |
|---|---|
| Cuckoo filter | A compact set-membership structure that stores fingerprints in buckets and supports delete |
| Fingerprint | A short hash of an item (for example 8 bits) stored in place of the item |
| Bucket | A small group of slots. Each item can live in one of two buckets |
| Slot | One cell in a bucket that holds a single fingerprint |
| Cuckoo hashing | A hash table idea where an item has two homes and can push another one out of its place |
| Eviction / kick | Pushing a stored fingerprint out of its bucket to make room |
| Relocation | Moving the evicted fingerprint to its other bucket |
| Partial-key hashing | Finding the other bucket from the fingerprint alone: bucket XOR hash(fingerprint) |
| Load factor | The share of slots that are in use. High load makes inserts slow and may make them fail |
| False positive | A 'maybe yes' for an item that was never inserted, caused by an equal fingerprint in the same buckets |
| Max kicks | The limit on how many relocations an insert may try before it fails |
| Choice | We gain | We lose |
|---|---|---|
| Longer fingerprints | Fewer false positives | More memory per item |
| More slots per bucket | Higher load before failing | More comparisons per lookup, and slightly more false positives |
| Higher max kicks | Fewer failed inserts | Some inserts become slow |
| Cuckoo vs Bloom | Delete, and often less space at low error | More complex, and inserts can fail |
| Cuckoo vs counting Bloom | Less memory, simpler lookups | A hard capacity limit |
| Run it nearly full | Best use of memory | Slow inserts and a risk of failure |