Working with Bloom Filters
1. Adding Elements to Bloom Filter
| Command | Description |
|---|---|
BF.ADD key item | Add; creates with defaults if missing |
2. Checking Element Existence
| Command | Returns |
|---|---|
BF.EXISTS key item | 1 probably present, 0 definitely absent |
3. Adding Multiple Elements
| Command | Description |
|---|---|
BF.MADD key i1 i2 ... | Add many, returns per-item added status |
BF.INSERT key [CAPACITY c] [ERROR e] [EXPANSION x] [NOCREATE] [NONSCALING] ITEMS i ... | Add with custom params |
4. Checking Multiple Elements
| Command | Returns |
|---|---|
BF.MEXISTS key i1 i2 ... | Array of 0/1 |
5. Creating Custom Bloom Filter
BF.RESERVE seen 0.001 1000000 EXPANSION 2
| Argument | Meaning |
|---|---|
| error_rate | Target FP rate (e.g. 0.001) |
| capacity | Expected unique items |
EXPANSION n | Sub-filter growth factor |
NONSCALING | Reject inserts past capacity |
6. Getting Filter Info
| Command | Returns |
|---|---|
BF.INFO key | Capacity, size, filters, items, expansion |
7. Understanding False Positive Rate
| Aspect | Detail |
|---|---|
| No false negatives | "absent" is always true |
| False positives | Tunable; grows as filter fills |
8. Configuring Filter Capacity and Error Rate
| Error Rate | Bits/item |
|---|---|
| 1% | ~9.6 |
| 0.1% | ~14.4 |
| 0.01% | ~19.2 |
9. Using for Duplicate Detection
Example: De-dup processed events
BF.RESERVE events:seen 0.001 10000000
if redis.call('BF.EXISTS', 'events:seen', id) == 0 then
redis.call('BF.ADD', 'events:seen', id)
process(id)
end