Quick revision
The short answer
At a 0.1 percent false-positive rate a Bloom filter needs -ln(0.001) / (ln 2)^2, about 14.4 bits per item, so 500 million usernames need about 7.2 billion bits, roughly 900 MB, with about 10 hash functions. We create it with BF.RESERVE usernames 0.001 600000000 to leave room for growth, load it in pipelined BF.MADD batches from the database, and add each new username at sign-up. A single key of about 1 GB is a big key that sits on one shard and makes replication, migration and deletion heavy, so we split it into many filters by a hash of the username, and treat every positive answer as a hint that the database's unique index confirms.
What to remember
In this answer, we sized the filter with m = -n ln(p) / (ln 2)^2: about 14.4 bits per item at 0.1 percent, so 500 million usernames need about 900 MB and 10 hash functions, or about 1.08 GB when reserving 600 million for growth. We created it with BF.RESERVE, loaded it with pipelined BF.MADD while sign-ups added new names, and saw why a single 1 GB key concentrates load and makes replication, migration and deletion heavy, so we split it into 64 filters by a hash of the username. We plan capacity instead of relying on expansion, and every positive answer is confirmed by the database's unique index.
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.
At a 0.1 percent false-positive rate a Bloom filter needs -ln(0.001) / (ln 2)^2, about 14.4 bits per item, so 500 million usernames need about 7.2 billion bits, roughly 900 MB, with about 10 hash functions. We create it with BF.RESERVE usernames 0.001 600000000 to leave room for growth, load it in pipelined BF.MADD batches from the database, and add each new username at sign-up. A single key of about 1 GB is a big key that sits on one shard and makes replication, migration and deletion heavy, so we split it into many filters by a hash of the username, and treat every positive answer as a hint that the database's unique index confirms.
Why this question is asked
An instant 'that username is taken' hint makes sign-up feel fast, and checking a Bloom filter in memory is far cheaper than querying a table of 500 million rows on every keystroke. Interviewers ask this design because it combines arithmetic, bulk loading and the operational problems of very large keys. In this answer, we will do the sizing step by step, create and load the filter, look at what goes wrong with one huge key, split it, plan for growth, and keep the database as the authority.
1Sizing the Filter Step by Step
For n items and a target false-positive rate p, the optimal number of bits is m = -n ln(p) / (ln 2)^2, and the best number of hash functions is k = (m / n) ln 2. These come from choosing k so that about half of the bits end up set when the filter is full, which is where false positives are least likely.
Notice that the size does not depend on how long the usernames are; a 3-character name and a 30-character name cost the same 14.4 bits. Some RedisBloom versions round the bit array up, so we check the real size with BF.INFO after creating it.
2Creating and Loading It
We create the filter explicitly with the error rate and a capacity above today's count, so growth over the next year or two does not push it past capacity. BF.RESERVE allocates the whole bit array immediately, so memory is used up front, not as items arrive.
The initial load reads usernames from the database in primary-key order and adds them in batches with BF.MADD, many items per command, many commands per pipeline. With batches of 1,000 and a few pipelines in parallel, a load of 200,000 items a second finishes 500 million in about 40 minutes. From the moment loading starts, the sign-up service also adds every new username with BF.ADD, so names created during the load are not missed. Usernames are normalised the same way everywhere, for example lower-cased, so 'Alice' and 'alice' map to the same bits.
6 messages in order. Play them, or step through them one at a time.
A 0 from the filter skips the database; a 1 is always confirmed.
3Why One 1 GB Key Is a Problem
A key this large is what operators call a big key, and it causes trouble well beyond memory. It lives in one hash slot, so in Redis Cluster all of it sits on one shard, and every username check, potentially thousands a second while people type, goes to that one primary. If a replica has to resynchronise, the snapshot carries the whole gigabyte. Resharding has to move the key as a single unit, which blocks the source and target nodes while it is serialised. Deleting it with DEL blocks the server while memory is freed; UNLINK frees it in the background, but we have to remember to use it.
None of this is a correctness problem, but each is an operational surprise waiting for the worst moment.
4Splitting Into Many Filters
The fix is to split the filter by a hash of the username into, say, 64 smaller filters, usernames:0 to usernames:63. Each holds about 9.4 million items of capacity and takes about 17 MB. The sign-up service computes the shard number from a stable hash of the normalised username, so a given name always goes to the same filter for both adds and checks.
Now the filters spread across the cluster's shards, checks spread across primaries, a resync or migration moves 17 MB at a time, and rebuilding one shard is quick. The error rate per lookup is unchanged, because each name is checked against exactly one filter sized for its share.
| Concern | One 1 GB filter | 64 filters of 17 MB |
|---|---|---|
| Placement | one shard, one primary | spread across all shards |
| Check traffic | all on one primary | spread evenly |
| Resync, migration | moves 1 GB at once | moves 17 MB at a time |
| Delete or rebuild | slow, heavy | one shard at a time |
We avoid String.hashCode() for the shard choice if other systems written in other languages will also compute it; a named hash such as MurmurHash3 is easy to reproduce anywhere. The bloom.exists call stands for whichever client wrapper we use.
5Planning for Growth
If a filter receives more items than its capacity, RedisBloom adds a sub-filter, by default twice as large (EXPANSION 2), with a tighter error rate. The filter keeps working, but every lookup now checks all its sub-filters, memory grows in jumps, and the combined error rate drifts above the target. Relying on that is a sign we stopped planning.
So we monitor items inserted against capacity with BF.INFO, alert at 80 percent, and rebuild with a larger capacity well before the limit. With 600 million reserved and, say, 30 million new users a year, we have about three years, which we write down so the next team knows.
6The Filter Is Only a Hint
A positive answer always goes to the database, where a unique index on the normalised username is the real authority; even the final INSERT at sign-up relies on that constraint, not on the filter. A negative answer lets us show 'available' immediately and is also checked by the constraint when the user submits.
And because the filter is derived data, losing it is not a disaster: we can rebuild it from the database at any time, and in the meantime fall back to querying the database directly, more slowly.
7Follow-up questions
How would you rebuild it without a gap while sign-ups continue?
Build the new filters under new names, such as usernames:v2:0 to usernames:v2:63, while the old ones keep serving. Before the bulk load starts, switch the sign-up service to add new usernames to both the old and the new filters, so nothing created during the load is missed. Load the existing names from the database, verify item counts against the table, then flip a configuration value so checks read v2, and finally UNLINK the old filters.
Why not just use a Redis set of usernames?
Because it would be exact but huge: 500 million strings with set overhead would take tens of gigabytes, against under 1 GB for the filter. For a hint that is confirmed by the database anyway, a 0.1 percent false-positive rate is a very good trade.
Does a false positive hurt the user?
Only slightly. At 0.1 percent, one in a thousand free names first looks possibly taken, the database check then shows it is free, and the user sees 'available' a few milliseconds later. The filter never wrongly says a taken name is free.
8Conclusion
In this answer, we sized the filter with m = -n ln(p) / (ln 2)^2: about 14.4 bits per item at 0.1 percent, so 500 million usernames need about 900 MB and 10 hash functions, or about 1.08 GB when reserving 600 million for growth. We created it with BF.RESERVE, loaded it with pipelined BF.MADD while sign-ups added new names, and saw why a single 1 GB key concentrates load and makes replication, migration and deletion heavy, so we split it into 64 filters by a hash of the username. We plan capacity instead of relying on expansion, and every positive answer is confirmed by the database's unique index.
Open with
Bits per item is minus ln p over ln 2 squared. For p equals 0.001 that's about 14.4 bits, so 500 million usernames need about 7.2 billion bits, roughly 900 megabytes, and the best number of hash functions is about 10.
Then, if they want more
I'd create it with BF.RESERVE usernames 0.001 600000000, so there's room to grow, which is about 1.08 gigabytes allocated up front.
To load it, a job reads usernames from the database in key order and sends BF.MADD in batches of around a thousand, pipelined, which loads 500 million in under an hour, and the sign-up service adds every new name with BF.ADD from the moment loading starts. Names are normalised the same way everywhere.
Keeping it in one key is risky: it's a big key, so it sits on one shard, every check hits one primary, and resyncs, resharding and deletion move or free a gigabyte at once. I'd split it into, say, 64 filters by a stable hash of the username, about 17 megabytes each, spread across the cluster.
I'd monitor items inserted against capacity, because past capacity it adds sub-filters and lookups get slower and less accurate. And it's only a hint: a positive goes to the database's unique index, and the filter can always be rebuilt from the database.
About this answer
- Last updated
- 13 November 2026
- 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 "Size the Bloom filter, explain how you would create and load it, and describe the risks...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/7613451a02/