Working with Bloom Filters

1. Adding Elements to Bloom Filter

CommandDescription
BF.ADD key itemAdd; creates with defaults if missing

2. Checking Element Existence

CommandReturns
BF.EXISTS key item1 probably present, 0 definitely absent

3. Adding Multiple Elements

CommandDescription
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

CommandReturns
BF.MEXISTS key i1 i2 ...Array of 0/1

5. Creating Custom Bloom Filter

BF.RESERVE seen 0.001 1000000 EXPANSION 2
ArgumentMeaning
error_rateTarget FP rate (e.g. 0.001)
capacityExpected unique items
EXPANSION nSub-filter growth factor
NONSCALINGReject inserts past capacity

6. Getting Filter Info

CommandReturns
BF.INFO keyCapacity, size, filters, items, expansion

7. Understanding False Positive Rate

AspectDetail
No false negatives"absent" is always true
False positivesTunable; grows as filter fills

8. Configuring Filter Capacity and Error Rate

Error RateBits/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

10. Comparing with Set-Based Membership Testing

Bloom Filter

  • Tiny memory (~10 bits/item)
  • Tunable false positives
  • No deletion
  • No enumeration

SET

  • Exact
  • Supports SREM, SMEMBERS
  • ~50-100 bytes/item
  • Best when count small