DEV Community

gxqwb
gxqwb

Posted on

XOR Filter vs Cuckoo Filter: Space, Speed, and the Right to Delete

A membership filter answers one question: have I seen this item before? Bloom filters have answered it for decades, but two newer designs have carved out distinct niches. One squeezes the set into a structure built from three hash functions and XOR operations, optimized for sets that are built once and then queried forever. The other stores short fingerprints in buckets and allows an item to be removed, which makes it useful when the set changes. Both are fast, both are approximate, and picking the wrong one shows up as either wasted memory or impossible requirements.

An XOR filter is a compact, immutable snapshot optimized for lookup speed and minimal space, while a cuckoo filter trades a little more memory for the ability to delete items and to grow incrementally, so the real question is whether your set ever shrinks.

That one sentence drives most engineering decisions here. A set that never changes can be compressed aggressively. A set that changes needs a structure with room to move.

Part One: How each structure actually works

An XOR filter stores no elements at all. It splits a hash value into three positions into an array of small fingerprints and fills the array so that the XOR of the three positions equals the fingerprint of the item. To test membership, you hash the query, XOR the three positions, and compare. If the result matches, the item is probably present; otherwise it is definitely absent. Construction solves a system of equations, and if the solver gets stuck on a cycle it retries with a differently seeded hash. The result is roughly 9.8 bits per item at a low false-positive rate, close to a Bloom filter but with one cache-friendly probe pattern.

A cuckoo filter stores a short fingerprint of each item in one of two candidate buckets. To insert, it tries either bucket; if both are full, it evicts an existing fingerprint and moves that item to its alternate bucket, repeating until a slot opens or a limit is reached. Lookup checks the two buckets. Deletion removes the fingerprint from whichever bucket holds it. Because displacement can fail under a full table, cuckoo filters operate below full capacity and use 16-bit or 12-bit fingerprints depending on the target error rate, landing around 10 to 14 bits per item in practice.

Part Two: What a billion items cost

Take a content cache that tracks 10 billion URLs. At 9.8 bits per item, an XOR filter occupies about 12.3 gigabytes. A cuckoo filter using 12-bit fingerprints at a typical load factor needs roughly 15 gigabytes. The difference is only about three gigabytes in absolute terms, but spread across 100 shard nodes it becomes about 30 megabytes per node — small enough to fit in the last-level cache on some hardware, which changes latency more than the raw ratio suggests.

Now consider the write path. Rebuilding an XOR filter means recollecting every item and solving the construction again, so a stream of new URLs requires a second filter layered on top of the first, periodically merged. A cuckoo filter simply inserts. At an ingestion rate of 50,000 items per second, the merging machinery for an immutable design becomes a system of its own, while the cuckoo filter absorbs the stream directly. Spending 25 percent more memory to delete an entire subsystem is often a good trade.

These are not academic numbers. Database and caching infrastructure is a market measured in tens of billions of dollars annually, and memory is the dominant cost in most in-memory systems. A filter that saves one bit per item on a hundred-billion-item deployment saves more than twelve gigabytes before replication, which is a genuine line item on an infrastructure bill.

An XOR filter is built by solving for the whole set at once, while a cuckoo filter is built one insertion at a time, so the former compresses better but the latter can accept a change without being rebuilt.

The mechanism explains the behaviour: global construction buys density, incremental construction buys flexibility.

Part Three: Where each design breaks

XOR filters break on change. There is no supported delete, no in-place insert, and no graceful way to handle a moving set. They also require the full key set at build time, which rules out streaming construction. And while lookups are fast, they touch three positions that may span cache lines, so careful layout still matters.

Cuckoo filters break under high load. When buckets near capacity, insertion requires long displacement chains and can fail outright, returning false for an item that should be present — a correctness problem, not just a performance one. Fingerprint collisions mean two different items can appear identical, which affects deletion: removing one can silently remove the other. They also use more memory than the densest alternatives, and the two-bucket probe touches locations that are not adjacent. Finally, deletion is only safe if the caller is certain the item was inserted; removing a fingerprint that was never there can corrupt another entry's presence.

A shared limitation deserves emphasis: both filters produce false positives. Neither can be trusted as the sole authority for an irreversible action. They are front-line filters that cheaply reject the majority of absent items before a slower, exact check decides the truth.

Part Four: An everyday analogy

Imagine a club with a guest list. The XOR filter is a list printed once before the doors open, beautifully compact, with every name encoded so the doorman can check it in a single glance. It is perfect for a private party where the guest list is frozen. If someone arrives who is not on the list, the doorman can say no instantly; if a guest leaves early, the printed list still says they are inside.

The cuckoo filter is a clipboard with two possible lines for each guest. Adding a name is easy, and crossing one off when the guest leaves is just as easy. Occasionally the clipboard fills up and the doorman has to shuffle names around to make room, which take a moment but works. The list is a little bulkier, but it reflects reality as the evening changes. A bouncer with the printed list is faster; a bouncer with the clipboard is accurate about who is still in the room.

What is actually transferable

The first lesson is to classify the set before choosing the structure. Frozen datasets — a fixed dictionary, a static block list, a one-time build of known keys — reward density. Streaming or expiring datasets — a rate limiter's active keys, a cache's resident objects, a session table — reward mutability. Asking "does this set ever shrink" eliminates half the options immediately.

The second lesson is to place filters where false positives are cheap. A filter in front of a disk lookup is excellent because a false positive costs one unnecessary read. A filter in front of a payment authorization is dangerous because a false positive costs money. The error rate is a design parameter, not a footnote, and it should be set from the price of being wrong.

The XOR filter and the cuckoo filter are not competing versions of one data structure but two answers to a lifecycle question, and the cost of a wrong answer is measured in rebuild machinery on one side and extra memory on the other.

Pick for the lifecycle first, then tune for space. The reverse order is how filters end up being rebuilt every night.

SOL: HCqPmPJM3NVtS6YrmVaTZYnuP8XZ2PFgQZKT94VK1LkS
Creating this takes real effort — thank you for the support.

Top comments (0)