
- Problem: On a signup page for an app with ~500 million users, querying the database for every typed username creates massive read load.
- Solution: A Bloom filter provides a fast, memory-efficient membership test that lets you quickly rule out usernames that are definitely not registered — reducing the number of database lookups.
- A Bloom filter is a bit array of size
m, initially all zeros, pluskindependent hash functions. - Each hash function maps an input (for example, the username “john”) to an index in
[0, m-1]. - To insert an item: compute the
khash indices and set the bits at those indices to 1. - The item itself is not stored — only bits in the bit array are flipped.
- To query membership: hash the query and check the
kbits. If any bit is 0, the item is definitely not present. If all are 1, the item is probably present (could be a false positive).
- Insert “John”: hashes -> {4, 19, 42} ⇒ set bits at 4, 19, 42 to 1.
- Query “Alan”: hashes -> {8, 19, 42}. Bits 19 and 42 might be 1, but if bit 8 is 0 then “Alan” is definitely not present and you can skip the database lookup.
- If all required bits are 1, the filter returns “possibly present” and you must confirm with the database.
Bloom filters are probabilistic membership structures: they never yield false negatives (when used without deletions and with stable hash functions), but they can yield false positives. Use them to cheaply rule out non-membership; when the filter says “present”, follow up with an exact check (e.g., a database query).
k positions for a query are set to 1 by other insertions. For example, “Mike” might hash to {4, 27, 61} — those bits could already be 1 because other users (“John”, “Ross”, “Sarah”) flipped them. The Bloom filter would report “Mike” as possibly present even though “Mike” was never inserted, so you must verify with the canonical store.

- Time complexity: insert and query are both O(k) because you compute
khashes and check/updatekbits. - Space efficiency: Bloom filters use far less memory than storing full items (e.g., usernames).
- False positive probability (approximate):
- Optimal number of hash functions for given
mandn:
- A practical optimization: derive
khash values from two base hashes (double hashing) instead of computingkindependent hashes — this reduces CPU cost while approximating independence.
Operation and space complexity
Example: space estimate
- A filter with about 5 billion bits uses ~625,000,000 bytes (~600 MB). That’s far smaller than storing 500 million username strings with full metadata.
- Standard Bloom filters do not support safe deletions; clearing bits can introduce false negatives.
- Use counting Bloom filters (which keep small counters per position) or alternatives (Cuckoo filters, scalable Bloom filters) if you need deletions, resizing, or lower false-positive behavior.

Do not use a plain Bloom filter if you require deletions or exact membership without additional checks. Deleting by clearing bits can create false negatives. For deletions, use a counting Bloom filter or a different data structure (e.g., Cuckoo filter).
- Estimate expected
n(number of items) up front. - Choose
mso memory fits your budget. - Compute
k_optfrommandn, then tune experimentally for acceptable false positive ratep. - Use double hashing to reduce hash computation overhead.
- Always verify “possibly present” results against a persistent store (DB) before taking irreversible actions.
- Consider alternatives (counting Bloom, Cuckoo filters, RedisBloom) if you need deletions, counting, or dynamic resizing.
- Bloom filters are space-efficient probabilistic data structures for approximate set membership.
- They can definitively tell you an item is not present; when they say “present”, follow up with an exact check.
- Tune
mandkfor your expectednto balance memory and false positive rate. - Use counting variants or other filters if you need deletions or extended functionality.
- Bloom filter — Wikipedia
- RedisBloom module — practical Bloom filter implementation for Redis
- Probabilistic Data Structures — Survey