Fundamentals 12 min read

Using Bloom Filters with Redis and Redisson: Theory, Installation, and Practical Code Samples

This article explains the concept of Bloom filters, their probabilistic behavior and limitations, demonstrates how to integrate them into Redis via the RedisBloom module, and provides complete Java/Redisson examples for creating, populating, and querying Bloom filters to prevent cache penetration and duplicate processing.

IT Services Circle
IT Services Circle
IT Services Circle
Using Bloom Filters with Redis and Redisson: Theory, Installation, and Practical Code Samples

Bloom filters are space‑efficient probabilistic data structures introduced by Burton Bloom in 1970; they can quickly test set membership with false‑positive possibilities but no false negatives, and they do not support element deletion.

The article first revisits cache‑related problems (cache penetration, cache breakdown, cache avalanche) and shows how Bloom filters can be used to avoid repeated database queries when storing large historical records, such as deduplicating news recommendations or filtering already‑seen items.

What is a Bloom filter? It uses a bit array initialized to 0 and k independent hash functions. Adding an element sets the k bits to 1; checking an element queries the same k bits – if all are 1 the element is considered present (may be a false positive), otherwise it is definitely absent.

Redis integration – Redis 4.0 introduced the RedisBloom module. The article provides download links, shows how to compile the module (tar -zxvf, make) and load it via loadmodule /opt/app/RedisBloom-2.2.14/redisbloom.so in redis.conf, then restart Redis. For clusters, the module must be added to every instance.

redis-server -v
Redis server v=6.2.6 sha=00000000:0 malloc=libc bits=64 build=b5524b65e12bbef5

Key RedisBloom commands demonstrated:

BF.RESERVE key error_rate capacity [EXPANSION n] [NONSCALING] – create a filter.

BF.ADD key item – add a single element.

BF.MADD key item1 item2 … – add multiple elements.

BF.EXISTS key item – test existence (1 = exists, 0 = not).

BF.MEXISTS key item1 item2 … – batch test.

BF.INFO key – retrieve filter statistics (capacity, size, number of filters, items inserted, expansion rate).

Example scenario: when an order is created, its ID is added to a Bloom filter (e.g., BF.ADD orders 10086) so subsequent requests can quickly determine if the order has already been processed, preventing cache penetration.

To inspect a filter: BF.INFO orders returns capacity, size, number of filters, items inserted, and expansion rate.

The article also mentions that Bloom filters cannot delete elements; for deletable structures, Cuckoo filters are suggested.

Redisson (Java) integration – add the Maven dependency:

<dependency>
  <groupId>org.redisson</groupId>
  <artifactId>redisson-spring-boot-starter</artifactId>
  <version>3.16.7</version>
</dependency>

Configure Redis in application.yml (host, port, ssl). Then create a Spring service that obtains a RBloomFilter<T> from the injected RedissonClient and initializes it with tryInit(expectedInsertions, falseProbability):

@Service
public class BloomFilterService {
    @Autowired
    private RedissonClient redissonClient;

    public <T> RBloomFilter<T> create(String filterName, long expectedInsertions, double falseProbability) {
        RBloomFilter<T> bloomFilter = redissonClient.getBloomFilter(filterName);
        bloomFilter.tryInit(expectedInsertions, falseProbability);
        return bloomFilter;
    }
}

A unit test demonstrates inserting 10,000 elements, counting them, checking false‑positive rate, and finally deleting the filter:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RedissionApplication.class)
public class BloomFilterTest {
    @Autowired
    private BloomFilterService bloomFilterService;

    @Test
    public void testBloomFilter() {
        long expectedInsertions = 10000L;
        double falseProbability = 0.01;
        RBloomFilter<Long> bloomFilter = bloomFilterService.create("ipBlackList", expectedInsertions, falseProbability);
        for (long i = 0; i < expectedInsertions; i++) {
            bloomFilter.add(i);
        }
        long elementCount = bloomFilter.count();
        log.info("elementCount = {}.", elementCount);
        int falsePositives = 0;
        for (long i = expectedInsertions; i < expectedInsertions * 2; i++) {
            if (bloomFilter.contains(i)) {
                falsePositives++;
            }
        }
        log.info("False positive count = {}.", falsePositives);
        bloomFilter.delete();
    }
}

When using Redis Cluster, the appropriate Redisson type is RClusteredBloomFilter<SomeObject>.

Overall, the guide provides a complete workflow from theory to hands‑on implementation for using Bloom filters in Redis and Java applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavarediscachingData Structuresbloom-filterredissoncache-penetration
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.