A visual tour of Ribbon filters — how a band-shaped linear system over GF(2) gives static set membership in near-optimal space, and how on-the-fly Gaussian elimination makes it practical.
Every LSM-tree has a memory problem disguised as a read problem.
A key-value store keeps its data in immutable files, and every lookup has to decide which files could possibly contain the key. The standard answer is a Bloom filter: a compact, in-memory sketch of each file's key set. If the Bloom filter says no, the file is skipped without touching disk. If it says yes, the file is read and the key is checked for real.
Bloom filters are fast and simple, but they are not cheap. At a 1% false-positive rate a Bloom filter needs about 9.6 bits per key, while the information-theoretic minimum is only 6.64 bits. That gap is roughly 44% wasted memory — and in a database with billions of keys, memory is the budget that matters.
Ribbon filters, introduced by Peter Dillinger and Stefan Walzer, close most of that gap. The trick is not a new hash function or a new bit layout. It is a different shape of linear system, plus a Gaussian-elimination algorithm that is fast enough to build it in practice.
This post walks through the idea with interactive visualizations. We will build a Ribbon filter one equation at a time, query it, break it at the edges, and fix it with a trick called smash.
The Floor: How Few Bits Can a Filter Use?
A filter represents a static set . For every it must answer yes. For it may answer yes with probability at most , the false-positive rate.
The information-theoretic lower bound for a hashed filter with false-positive rate is
The intuition is a counting argument. A filter with bits per key can distinguish at most subsets of an -key universe. To cover all possible sets while only allowing an fraction of absent keys to collide, you need at least bits per key.
At , that floor is bits per key. Practical structures sit above it:
| Structure | Bits per key | Overhead over |
|---|---|---|
| Information-theoretic bound | 0% | |
| Bloom filter | ~44% | |
| Blocked Bloom | often or more | 50%+ |
| Classic Xor filter | ~23% | |
| Xor+ filter | ~8% plus a constant | |
| Ribbon, homogeneous | about | ~10% |
| Balanced Ribbon | under 1% |
Bloom filters pay for their simplicity: each key sets several bits, and the analysis that makes them work forces a load factor well below the information-theoretic optimum. Xor filters do better by solving a linear system, but their standard construction — peeling — has its own structural limit.
Ribbon filters keep the Xor query shape and change the matrix. That is what buys the space back.
Static Functions and XOR Probing
Start with the static-function view of a filter. Pick a hash function that maps each key to a set of row indices
and a fingerprint function . We store an bit matrix . For every key in we want
where is the -th row of and is bitwise XOR. In other words, the characteristic vector of satisfies a linear equation over the two-element field :
If the equations for all have a solution, we can answer a query by computing the same XOR:
For , this returns exactly. For , the fingerprint is effectively random and independent of , so the query matches only with probability . That gives a filter with false-positive rate and space bits, or bits per key.
The classic Xor filter chooses to be three random rows. Construction means solving the linear system. Peeling does this by repeatedly finding a row that occurs in exactly one equation, assigning that row, and removing the equation. It is fast, but it only works while the system is sparse enough. The 3-XORSAT peeling threshold caps the load at about , which translates into at least 22% space overhead.
Ribbon filters choose a different : one that makes the matrix banded, so Gaussian elimination never fills in outside a narrow ribbon.
The Ribbon Trick
Fix a ribbon width . For each key , pick
- a random start position , and
- a random coefficient vector whose first bit is .
The equation for is
In matrix form, row has its nonzero entries confined to columns through . If we sort the equations by start position, all the action lives in a diagonal band — the ribbon.
XOR is both addition and subtraction in . That is why Gaussian elimination here is just XOR the rows together — no multiplication, no fractions, no bookkeeping of signs.
The query is beautifully local. To test a key, read the rows of starting at , mask them with , and XOR the selected rows:
function query(x: Key): number {
let value = 0
const start = s(x)
const coeff = c(x)
for (let j = 0; j < w; j++) {
if ((coeff >>> j) & 1) value ^= Z[start + j]
}
return value
}That is one contiguous window of rows, not three random rows. It costs more memory traffic per query than a classic Xor filter — rows instead of 3 — but it makes the linear system far easier to solve at high load.
The parameters trade space for CPU:
- rows for keys, so the space overhead is roughly .
- Larger means a narrower band relative to the matrix, which allows smaller , but more rows per query.
- Smaller means less space and a higher chance that construction fails.
The paper's headline is that this trade-off can be pushed to under 10% overhead, and with an experimental load-balancing scheme, under 1%.
Building It: Boolean Banding on the Fly
The construction algorithm is the heart of the paper. It is called Rapid Incremental Boolean Banding ON the fly, or just on-the-fly banding. It looks less like textbook Gaussian elimination and more like building a hash table.
We maintain a reduced system with one row per solution row. Each occupied row stores a coefficient vector and a right-hand side , representing
To add a key's equation, start at row with its coefficient and fingerprint , then repeat:
i ← s(x); c ← c(x); b ← b(x)
loop:
if row i of M is empty:
store (c, b) in row i
return inserted
c ← c ⊕ M[i].c // XOR with the resident equation
b ← b ⊕ M[i].b
if c = 0:
if b = 0: return redundant
else: return inconsistent
j ← index of the first 1-bit of c
i ← i + j
c ← c ≫ jThere are only a few things happening:
- Empty row: drop the equation in. Done.
- Occupied row: XOR with the equation already there. Since both coefficient vectors start with a , the leading bits cancel. The result has some number of leading zeros.
- Shift: because the new leading is at offset , the equation now lives at row . Shift the coefficient vector right by and continue.
- All zero: if the coefficient is zero and the fingerprint is zero, the equation was redundant. If the coefficient is zero but the fingerprint is not, the system is inconsistent and construction fails.
No sorting is required. No pivot table is stored. The row index is the pivot, and the band shape guarantees the shifted equation never leaves the matrix. The expected number of XOR-and-shift steps is overall, with much smaller constants than the earlier structured-Gaussian-elimination approach.
Try it below. The grid is the reduced system : rows are equations, columns are solution rows, and each stored equation occupies a -wide window. Press Replay to watch keys arrive one at a time. Pink is the equation currently being inserted; indigo cells are stored equations.
Interactive ribbon
Boolean banding, one equation at a time
Ribbon width w
Space overhead ε20%
m = 32 rows · n = 26 keys
Construction
r = 7 fingerprint bits
Edge handling
uniform s(x)
The playground is deliberately tiny — 32 solution rows — so the whole matrix fits on screen. Small systems have much noisier construction success and false-positive rates than production filters with millions of keys. Use it to see the algorithm, not to tune parameters.
A few things to watch for:
- When a key lands on an occupied row, the pink equation XORs with the resident one and jumps to a later row. The jump is the
findFirstSetshift. - Some equations vanish as redundant (
0 = 0). That is fine; they add no information. - If an equation becomes
0 = bwith , the construction fails. That is the moment where you need more space, a wider ribbon, or a new hash seed. - The
max displacementstatistic is the furthest any equation had to travel. It is a good proxy for construction work.
The algorithm is on-the-fly because keys can arrive in any order; you can stop at the first failure and know that the prefix succeeded. It is incremental because you can undo the most recent insertions by clearing the rows they added. Those two properties are what make sharding and Balanced Ribbon possible.
Back-Substitution, Free Variables, and Queries
Once every equation is in , solve for from the bottom up. Row 's equation determines in terms of rows through , which have already been computed:
Rows that never received an equation are free variables. The system does not constrain them, so we can assign them anything — usually random bits. This detail becomes important for Homogeneous Ribbon filters.
After back-substitution, queries work exactly as advertised. The playground below opens on the Query tab. Pick a key that was inserted: the XOR of its selected rows reproduces its fingerprint. Pick a random key that was never inserted: the XOR is essentially random, and it matches only about once in tries.
Interactive ribbon
Boolean banding, one equation at a time
Ribbon width w
Space overhead ε20%
m = 32 rows · n = 26 keys
Construction
r = 7 fingerprint bits
Edge handling
uniform s(x)
read 16 rows starting at s(x) = 12
c(x) = 1011001010010010
Z[12] ⊕ Z[14] ⊕ Z[15] ⊕ Z[18] ⊕ Z[20] ⊕ Z[23] ⊕ Z[26] = 0x79
Measure the false-positive rate
Query 8192 random keys that were never inserted. Expect about 64 matches (1/128 ≈ 2−7).
A query reads the whole w-row window but XORs only the rows where c(x) has a 1. Rows marked ◦ are free variables: back-substitution never determined them, so they are filled with random bits.
In the query strip, each box is one row of in the -row window. Pink boxes are selected by ; the rest are read but masked out. The little ◦ marks a free variable — a row that back-substitution never pinned down. The Run 8,192 queries button measures the false-positive rate directly on random absent keys.
Bring Your Own Keys
The playground above uses synthetic keys, so its rows are just numbers. This one uses the strings you type. Each key is expanded into a start position , a 12-bit coefficient , and a 7-bit fingerprint . Press Build filter, then hover or click a key in the table to trace its path through the ribbon.
Bring your own keys
Real strings → start positions → ribbon rows
Edits take effect when you press Build. Duplicates are removed.
8 inserted keys · 32 rows · w = 12 · r = 7
click or hover a key to trace its path
trace: apple · s = 15 → row 15 · 0 shifts
| key | s(x) | c(x) | b(x) | stored row | shifts |
|---|---|---|---|---|---|
| apple | 15 | 101111111101 | 0x72 | 15 | 0 |
| banana | 18 | 111100101110 | 0x19 | 18 | 0 |
| cherry | 16 | 110000011010 | 0x14 | 16 | 0 |
| date | 8 | 101010001001 | 0x68 | 8 | 0 |
| elderberry | 6 | 110111100000 | 0x7f | 6 | 0 |
| fig | 8 | 110001110010 | 0x08 | 9 | 1 |
| grape | 3 | 101000010100 | 0x1a | 3 | 0 |
| honeydew | 8 | 100101011101 | 0x07 | 10 | 1 |
Toy hash expansion: FNV-1a → PRNG → (s, c, b). Production Ribbon filters use carefully decorrelated hashing and much larger structures. The 32-row matrix is fixed here so every row stays legible.
The table is the concrete mapping:
- is the row where the key starts.
- selects which of the rows it XORs.
- is the value a query must reproduce.
- stored row is where the equation ended up after collisions and shifts.
- shifts counts how many times it was bumped to the right.
Try querying a key that is in the list, then a word that is not. Present keys reproduce ; absent keys usually do not. If a key's equation is already implied by earlier rows, it becomes redundant — still in the set, but with no row of its own. Insertion order changes which key lands in which row, though not whether the system is solvable.
This lab uses a toy 32-bit hash expansion and a fixed 32-row ribbon, so it is for understanding the mapping, not for tuning parameters.
Smash: Rescuing the Edges
There is a subtle failure mode at the edges of a square Ribbon system, where .
Suppose we want to fill every slot. For the first slots to be filled, at least keys must start at or before position . With uniform random starts, the count near the beginning is a random walk, and it can fall behind. The same problem appears at the end of the matrix. Geometrically, the diagonal of the matrix can drift outside the ribbon.
The fix is called smash. Instead of sampling uniformly from , sample from a slightly wider interval and clamp the result to the legal range using min and max. This inflates the probability of the first and last start positions by a factor , the smash value. With and , the diagonal is firmly inside the ribbon, and the square matrix behaves like a random matrix — which is nonsingular with probability about .
The visualization below sorts the equations by start position. The dashed line is the matrix diagonal; a red cell means the diagonal has escaped the ribbon at that row. Toggle smash and watch the edge distribution change.
Interactive edge effect
The diagonal that falls off the ribbon
diagonal coverage
30/32 rows
2 diagonal cells sit outside the band. A perfect fill is unlikely without extra space or smash.
starts s(x)
max 16
Smash samples start positions from a wider interval and clamps them onto the first and last slots. Here ℓ = w/2 = 8.
Smash is not only for the perfectly square case. The paper's experiments show that it also improves construction success for practical , especially for smaller structures. It is part of what the authors call Standard Ribbon.
Homogeneous Ribbon: Delete the Fingerprints
So far the right-hand side has been a random fingerprint. What if we simply set it to zero for every key?
Then the system becomes homogeneous:
The trivial solution is useless — it would answer yes to everything. But the system usually has many solutions, and if we choose one uniformly at random by randomizing the free variables during back-substitution, the filter becomes interesting again.
This is a Homogeneous Ribbon filter. A query returns the XOR of the selected rows, and the answer is yes if that XOR is zero. For , the construction guarantees zero. For , the XOR is zero with some probability .
The homogeneous design has two big practical advantages:
- Construction can never fail. A homogeneous linear system always has at least the trivial solution. There is no inconsistency case.
- No fingerprint function is needed, which slightly simplifies hashing and construction.
The cost is a more delicate false-positive analysis. Let be the probability that a random absent key's row vector lies in the span of the rows for . Then
If the ribbon is too narrow or too densely packed, is not negligible, because some parts of are over-constrained and therefore insufficiently random. The paper proves that for , the overhead is at most and stays within a factor of two of . The dependence on disappears: Homogeneous Ribbon scales to any number of keys with constant and .
In practice, the recommended operating point is
For (about a 0.8% false-positive rate) and , that is — roughly 9% space overhead. The measured overhead is about 10%, because the actual false-positive rate is slightly above .
Switch the playground above to Homogeneous mode. The equations now all have ; no key can fail, and the query tab tests whether the XOR is zero. The free variables are visibly random, and they are doing real work.
Homogeneous Ribbon has high variance in false-positive rate for small . For small static sets, the paper recommends Standard Ribbon with smash instead. The two designs are complementary, not a strict upgrade.
Balanced Ribbon: Sharding with Bumping
Standard Ribbon has a scaling problem: for a fixed width and overhead, construction success degrades as grows. Homogeneous Ribbon scales, but its overhead depends on , so it is not the best choice when is large.
Balanced Ribbon is the experimental answer. It combines soft sharding with a load-balancing scheme inspired by cuckoo hashing.
The key idea is that a shard does not have to be an independent filter with its own memory. It can be a contiguous range of start positions inside one larger Ribbon. Adjacent soft shards overlap by probing rows, so they can borrow space from each other without any metadata. This is strictly better for space than hard sharding.
Each key gets two possible locations:
- a primary start position in an earlier shard, and
- a secondary start position in a later shard, usually on the next level.
Shards are organized into levels. Level 1 has the most shards; each subsequent level has half as many. A key is inserted into its primary shard if possible. If not, it is bumped to its secondary shard. The bumping is greedy: constructed shards are never revisited.
To keep the supply of bumpable keys steady, level 1 overloads its primary positions by a factor , while later levels reserve capacity for bumped entries. A good choice is , where is the number of shards. Keys within a shard are grouped into a handful of buckets using another hash; the bucket threshold is the only per-shard metadata. Because construction is incremental, a failed bucket can be undone and a different one tried.
The result is striking: with , Balanced Ribbon uses
bits per key. At a 1% false-positive rate that is about 0.7% overhead, and the authors tested it up to 4 billion keys. The cost is construction time — Balanced Ribbon is several times slower to build than Homogeneous Ribbon — and implementation complexity. It is best read as a demonstration of what the on-the-fly algorithm makes possible, not as a drop-in default.
One shard of Balanced Ribbon is exactly a Standard Ribbon filter. The balancing machinery only matters when there are many shards.
How Much Space, Really?
The chart below compares the space cost of the main filter families. The -axis is bits per key; the -axis is the information content . The diagonal is the information-theoretic floor. The horizontal gap between a curve and the diagonal is its space overhead.
Interactive comparison
Bits per key vs. information content
f = 2−7 ≈ 0.78%
Target false-positive rateλ = 7
Ribbon width w
Wider ribbons spend more CPU per query, but need less space.
| structure | bits / key | overhead |
|---|---|---|
| Information-theoretic bound | 7.00 | — |
| Bloom | 10.08 | +44.0% |
| Xor | 8.61 | +23.0% |
| Xor+ | 8.06 | +15.1% |
| Balanced | 7.04 | +0.6% |
| Ribbon, w = 64 | 7.63 | +9.0% |
The dashed diagonal is the information-theoretic floor. Bloom pays about 44% over it; classic Xor filters about 23%; Xor+ about 8% plus half a bit. Ribbon's formula is the paper's empirical recommendation ε ≈ (4 + r/4)/w; at r = 7, w = 64 the paper measures about 10% because the realized false-positive rate is slightly above 2−7. Balanced Ribbon is an experimental extension. Query time is not shown — Ribbon trades CPU for the space it saves.
Around a 1% false-positive rate, the numbers from the paper's benchmarks look like this:
| Filter | Space overhead | Construction | Query |
|---|---|---|---|
| Blocked Bloom | ~50% | 11 ns/key | 14 ns/key |
| Cuckoo (12-bit) | ~40–46% | 69–91 ns/key | 20–21 ns/key |
| Xor, | 23% | 148 ns/key | 15 ns/key |
| Xor+, | 14.5% | 171 ns/key | 35 ns/key |
| Ribbon, homogeneous | 10.1% | 83 ns/key | 39 ns/key |
| Balanced Ribbon | 0.7% | 292 ns/key | 49 ns/key |
Those timings are from the paper's single-socket Skylake test machine, for keys. The relative picture is what matters:
- Ribbon wins on space, sometimes by a large factor.
- Ribbon often wins on construction time, because on-the-fly banding is cheaper than peeling or structured Gaussian elimination at the same space.
- Xor filters often win on query time, because they touch three random rows instead of a window of .
- Blocked Bloom is still the fastest option when its 50% space overhead is acceptable, especially for short-lived or very hot filters.
Ribbon's other advantage is configurability. Bloom filters can use any amount of memory to represent any number of keys; most alternatives cannot. Ribbon recovers that property, and adds a smooth trade-off between space and construction time. You can give it more space to build faster, or less space and spend more CPU.
Practical Details That Matter
A few implementation choices make the difference between a paper prototype and a usable filter.
Layout. Classic Xor filters use row-major layout: the whole row of is contiguous. That is a poor fit for Ribbon, which touches many rows and few columns. Pure column-major layout is ideal for a single result bit, but bad for because the bits are far apart in memory. Ribbon uses interleaved column-major layout (ICML): rows of are packed into one word per column, and blocks of words are grouped together. A query touches at most two words per result bit, and back-substitution is a streaming operation that fits in registers.
Fractional fingerprints. A filter with uses 7 bits per row, but the application may have 7.5 bits per key to spend. Ribbon handles this by using bits for some rows and bits for the rest, within a single structure. The boundary is crossed by only a tiny fraction of queries because probes are local. The same layout makes the filter elastic: after construction you can drop entire columns, or part of the last column, to trade false-positive rate for memory.
Hashing. Ribbon needs a 64-bit hash per key, expanded into a start position, a coefficient vector, and a fingerprint. The main hazard is correlation between the start position and the other hash outputs. The reference implementation uses fastrange on the hash and multiplies by a large odd constant (Knuth multiplicative hashing) to decorrelate the pieces. Re-seeding on construction failure is cheap; alternatively, increasing by a factor of gives an effectively independent chance of success.
Sharding. For large , a natural shard size is around . With on-the-fly construction you can add keys until a shard fails, then start the next shard — no retries, no global rebuild. Shard boundaries cost duplicated bits, which is negligible when shards are reasonably large.
When Should You Reach for Ribbon?
Ribbon filters are a good fit when:
- the key set is static or rebuilt in large batches,
- the filter is long-lived and memory-resident,
- reads dominate, and a few extra nanoseconds per query are worth a large memory saving,
- you want Bloom-like control over the space/accuracy trade-off.
They are a poor fit when:
- keys are inserted and deleted continuously (use a dynamic filter),
- query latency is the only metric and 23% overhead is acceptable (classic Xor is hard to beat),
- the filter is tiny or short-lived (blocked Bloom is simpler and faster),
- you cannot afford the construction CPU or the implementation complexity.
The paper's intended use case is the largest levels of an LSM-tree, where filters live for days or weeks and are queried many times per key. RocksDB added an experimental Ribbon option in version 6.15.0, and the design has influenced how engineers think about the static-filter space–time frontier.
The Bigger Lesson
For years, the folklore was that Gaussian elimination was too slow for practical filters, and that peeling was the only game in town. Ribbon filters puncture that story. By choosing a banded matrix, the elimination becomes local, incremental, and almost hash-table-like. The same structure that makes queries cache-friendly also makes construction cheap.
The result is a filter that is not just a little smaller than Bloom, but practically smaller — and, for many operating points, faster to build than the alternatives that already beat Bloom on space. That is a rare combination: a new point on the frontier, not just a new implementation of an old one.
If you want the full analysis — the sgauss connection, the homogeneous false-positive bound, the balanced-allocation scheme, and the benchmark methodology — the paper is well worth a careful read.
Further Reading
- Peter C. Dillinger and Stefan Walzer. Ribbon filter: practically smaller than Bloom and Xor. ESA 2021.
- Thomas Mueller Graf and Daniel Lemire. Xor Filters: Faster and Smaller Than Bloom and Cuckoo Filters. ACM JEA 2020.
- Martin Dietzfelbinger and Stefan Walzer. Efficient Gauss Elimination for Near-Quadratic Matrices with One Short Random Block per Row. ESA 2019.
- Peter C. Dillinger. Reference Ribbon implementation, a fork of the fastfilter benchmark suite.
Related Articles
Perfect Hash Functions: Collision-Free by Design
A deep dive into perfect hash functions — what they are, why they matter, how to find one efficiently with parallel search, and how they compare to JavaScript's built-in Map.
Fuzzy Search: Typos, Tries, and the Algorithms Behind Instant Results
A deep dive into how fuzzy search works — from edit distance and tries to stemming, BK-trees, and how Lucene/Elasticsearch find typo-tolerant matches at scale.
From Regex to Automata to Generators
How regular expressions compile to deterministic finite automata — with an interactive visualizer, a DFA-powered string generator, and a note on closing the loop with property-based testing.