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.

Image for post
Image for post

We will be using Redis with RedisBloom Module to implement it.

  1. Checking if value exists or not should be fast. So an in-memory data structure should be the better choice here.
  2. Redis has out of box support for many different data structures including Bloom Filter.
  1. Redis — We can set up Redis using docker check out the docker-compose file here.
  2. Java 8+
  3. 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.

This class holds the configuration for Redisson
We set the Redis URL here
We create beans for Redisson Config and Client

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).

Create and load data in BloomFilter
User Service checks with bloom filter if it contains the name
API which exposes this functionality
API : /rBloom/username/exists?name=John
Response : true
API : /rBloom/username/exists?name=Johni
Response : false
API : /rBloom/username/exists?name=Gaurav
Response : false

Thanks for reading! 🎉

I like building stuff.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store