Quick revision
The short answer
A Bloom filter sets k bits, chosen by k hash functions, for every item added; a lookup checks the same k bits, so if all are set the item is maybe present, and if any is unset it is definitely absent. In Redis we create one with BF.RESERVE key error_rate capacity, add with BF.ADD or BF.MADD, check with BF.EXISTS or BF.MEXISTS, and inspect with BF.INFO; items cannot be removed. When we need deletion we choose a Cuckoo filter, for frequencies a Count-Min Sketch, and for the heaviest items Top-K.
What to remember
In this answer, we saw that a Bloom filter sets k bits per item and checks them on lookup, so it can return false positives but never false negatives, and cannot remove items. In Redis, BF.RESERVE sets the error rate and capacity, BF.ADD and BF.MADD add, BF.EXISTS and BF.MEXISTS check, and BF.INFO shows capacity, size and sub-filters. A Cuckoo filter adds deletion, a Count-Min Sketch estimates frequencies and Top-K tracks heavy hitters; they come with Redis 8 and Redis Stack, with Valkey offering a separate bloom module, and they work best as quick pre-checks backed by an exact check.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Bloom filter sets k bits, chosen by k hash functions, for every item added; a lookup checks the same k bits, so if all are set the item is maybe present, and if any is unset it is definitely absent. In Redis we create one with BF.RESERVE key error_rate capacity, add with BF.ADD or BF.MADD, check with BF.EXISTS or BF.MEXISTS, and inspect with BF.INFO; items cannot be removed. When we need deletion we choose a Cuckoo filter, for frequencies a Count-Min Sketch, and for the heaviest items Top-K.
Why this question is asked
Many systems spend most of their time confirming that something is not there: a username that is not taken, a URL not yet crawled, a product id that does not exist. A Bloom filter answers 'definitely not' from memory in microseconds and lets us skip the expensive check. Interviewers ask about it to see whether we understand why it can say 'maybe' but never a wrong 'no', and whether we can pick the right probabilistic structure for a job. In this answer, we will see how the filter works, use the BF commands, look at the sizing trade-off, compare the related structures, and check which Redis engines provide them.
1How a Bloom Filter Works
A Bloom filter is an array of m bits, all 0 at the start, and k hash functions, each mapping an item to one position in the array. To add an item, we compute its k positions and set those bits to 1. To check an item, we compute the same k positions and look at the bits.
If any of them is 0, the item was never added, because adding it would have set that bit; that answer is certain. If all of them are 1, the item was probably added, but those bits might also have been set by other items, so 'present' can be wrong. That is a false positive. False negatives cannot happen.
Items cannot be removed. Clearing an item's bits might clear a bit that another item also set, and that other item would then wrongly read as absent, which breaks the one guarantee the filter has.
- Hash the item k timesfor example k = 10 positions
- Are all k bits set?YesMaybe present: confirm with the real dataNoDefinitely absent: skip the lookup
2 steps in order. Play them, or step through them one at a time.
Only 'absent' is certain; 'present' needs confirmation.
2The Main BF Commands
BF.RESERVE creates a filter for a target error rate and expected number of items, and Redis sizes the bit array and the number of hash functions from them. BF.ADD and BF.MADD add one or many items; BF.EXISTS and BF.MEXISTS check them.
BF.INFO shows the capacity, the memory size, the number of sub-filters and the number of items inserted. That last pair matters: if items inserted passes capacity, the filter grows by adding a sub-filter, which we come back to below. The exact BF.INFO layout differs a little between versions.
3Sizing: Error Rate Against Memory
For a target false-positive rate p, the filter needs about -ln(p) / (ln 2)^2 bits per item, and the best number of hash functions is about that number times ln 2. For p = 0.01 that is about 9.6 bits and 7 hashes per item; for p = 0.001, about 14.4 bits and 10 hashes. So 1 million items at 0.1 percent take about 1.8 MB, whatever the length of the items. A set of the same million URLs would take tens of megabytes.
Capacity matters as much as the error rate. If we add many more items than the capacity, more bits are set and the false-positive rate rises, so Redis adds sub-filters as the filter fills, which costs more memory and more work per lookup. It is better to reserve a realistic capacity up front.
4Cuckoo Filter, Count-Min Sketch and Top-K
A Cuckoo filter answers the same question as a Bloom filter, maybe present or definitely absent, but stores small fingerprints of items in buckets instead of setting bits, so it can delete: CF.ADD, CF.EXISTS and CF.DEL. We choose it when items must be removable, such as a set of active sessions. Deleting an item that was never added can remove another item's fingerprint, so we delete only what we know we added.
A Count-Min Sketch answers a different question: roughly how many times has this item been seen? CMS.INCRBY adds to an item's count and CMS.QUERY returns an estimate that can be too high but never too low. It suits frequencies, such as requests per IP, without a counter per key.
Top-K keeps the k items seen most often in a stream: TOPK.ADD feeds items, TOPK.LIST returns the current leaders. It suits heavy hitters, such as the most-searched terms or the noisiest clients.
| Structure | Answers | Delete? | Example |
|---|---|---|---|
Bloom (BF.*) | maybe present / definitely absent | no | skip DB lookups for unknown ids |
Cuckoo (CF.*) | maybe present / definitely absent | yes (CF.DEL) | active sessions, revocations |
Count-Min Sketch (CMS.*) | approximate count per item | no | requests per IP |
Top-K (TOPK.*) | the k most frequent items | no | most searched terms |
HyperLogLog (PF*) | number of distinct items | no | unique visitors |
5Where These Commands Come From
These types come from the RedisBloom module. Redis Stack bundles it, and Redis 8 includes these data types in the main distribution. Valkey offers its own separate bloom module with BF commands, and managed services differ in which modules they enable. So before designing around CF, CMS or TOPK, we check what our engine and hosting provide.
Typical uses share a pattern: a cheap pre-check in front of an exact one. We skip database lookups for ids that cannot exist, avoid recrawling URLs or re-sending notifications we have already handled, and show quick hints, always backed by the real data when the answer is 'maybe'.
6Follow-up questions
Why must a positive answer still be confirmed against the real data?
Because 'all bits set' can happen for an item that was never added, when other items happened to set those same bits. At a 0.1 percent error rate, one in a thousand absent items is reported present. If we acted on that, a new username would be wrongly rejected or a URL never crawled. So a positive answer only means 'go and check', while a negative answer can be trusted and saves the check.
Can I merge two Bloom filters?
Mathematically, two filters with the same size and hash functions can be combined with a bitwise OR, but the BF commands do not offer a merge, so in practice we add items to the filters we want or rebuild a combined one from the source data.
What happens if I call BF.ADD on a key that does not exist?
It creates a filter with default settings, an error rate of 0.01 and a capacity of 100 unless the module is configured otherwise. That default is rarely right for production, so we create filters explicitly with BF.RESERVE.
7Conclusion
In this answer, we saw that a Bloom filter sets k bits per item and checks them on lookup, so it can return false positives but never false negatives, and cannot remove items. In Redis, BF.RESERVE sets the error rate and capacity, BF.ADD and BF.MADD add, BF.EXISTS and BF.MEXISTS check, and BF.INFO shows capacity, size and sub-filters. A Cuckoo filter adds deletion, a Count-Min Sketch estimates frequencies and Top-K tracks heavy hitters; they come with Redis 8 and Redis Stack, with Valkey offering a separate bloom module, and they work best as quick pre-checks backed by an exact check.
Open with
A Bloom filter is a bit array plus k hash functions. Adding an item sets the k bits its hashes point to, and a lookup checks the same k bits.
Then, if they want more
If any bit is zero, the item was definitely never added; if all are one, it was maybe added, because other items could have set those bits. So there are false positives but no false negatives, and you can't remove items, since clearing bits could break other items.
In Redis I create one with BF.RESERVE key error-rate capacity, for example 0.001 and a million, add with BF.ADD or BF.MADD, check with BF.EXISTS or BF.MEXISTS, and look at BF.INFO for capacity, size, number of sub-filters and items inserted. At 0.1 percent it costs about 14 bits per item.
If I need deletion, I'd use a Cuckoo filter, CF.ADD, CF.EXISTS, CF.DEL. If I need how often something appeared, a Count-Min Sketch with CMS.INCRBY and CMS.QUERY, which can overestimate but never underestimates. For the heaviest hitters, Top-K with TOPK.ADD and TOPK.LIST.
These come with Redis 8 and Redis Stack from RedisBloom, while Valkey has its own bloom module, so I'd check what our engine provides. Typical use is skipping database lookups for ids that can't exist, with positives always confirmed against real data.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Redis 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how a Bloom filter works, the main BF commands, and when you would choose a...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/1867b9733e/