Bloom Filter using Redis

Gaurav Sharma
1 min readJul 9, 2020

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?

  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.

Environment Setup

  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.

Let’s Setup Redisson

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

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

Create and load data in BloomFilter
User Service checks with bloom filter if it contains the name
API which exposes this functionality

Results

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! 🎉

--

--