Bloom Filter using Redis
Previously I discussed about the Theory of Bloom Filter and implemented a simple version in Java. Let’s find out how we can implement a Bloom Filter for a distributed production environment.
We will be using Redis with RedisBloom Module to implement it.
Why Redis?
- Checking if value exists or not should be fast. So an in-memory data structure should be the better choice here.
- Redis has out of box support for many different data structures including Bloom Filter.
Environment Setup
- Redis — We can set up Redis using docker check out the docker-compose file here.
- Java 8+
- Spring Boot
Github Project for reference — Redis-Bloom-Filter
We will use the Redisson as our library to connect with Redis as it has out of box support for bloom filters.
Let’s Setup Redisson
Using Redisson
We create a Bloom filter of type String and initiate it with expected data size and probability of false positives. (Read Section : Important Parameters for more details).
We then fill it up with random generated names. (See Library : Name-Machine).
Results
API : /rBloom/username/exists?name=John
Response : trueAPI : /rBloom/username/exists?name=Johni
Response : falseAPI : /rBloom/username/exists?name=Gaurav
Response : false
Thanks for reading! 🎉