Bloom Filter for Valkey & Redis on Java

Published on
April 26, 2026

When coding data-intensive Java applications today, developers often face the challenge of efficiently determining set membership in very large datasets. Whether you need to verify available usernames, prevent duplicate URL crawling, or shield a database from malicious caching attacks, a common defense layer in front of a Redis-based Java cache, traditional Java Collections like HashSet quickly become cumbersome and use inordinate amounts of memory.

Probabilistic data structures like the Bloom Filter help overcome these challenges by trading complete determinism for extreme space efficiency. The Bloom Filter returns results with a more controllable rate of false positives and no false negatives.

For Java developers, interacting directly with low-level Valkey or Redis data structures like Bloom Filters can be tedious. Since Valkey and Redis lack native Java support, developers waste time on custom serialization, complex schema management, and context switching. Redisson handles serialization through pluggable codecs (Kryo, Jackson, Protobuf, and more) and exposing every Valkey/Redis structure as an idiomatic Java object. Redisson offers a sophisticated solution to this problem by abstracting complex Valkey/Redis operations into intuitive, object-oriented Java interfaces. Here's how it works.

Native Performance with RBloomFilterNative

Redisson PRO provides supercharged Bloom Filter efficiency through its RBloomFilterNative interface. It integrates directly with the native Bloom Filter capabilities of Valkey and Redis, allowing you to execute operations natively on the server. The native performance of RBloomFilterNative vastly reduces network overhead, CPU load, and response latency.

You simply initialize the filter with your desired expected capacity and acceptable error rate, and Redisson PRO handles the rest. You can easily add elements, check for existence individually or in bulk, and even retrieve specific telemetry on the filter's capacity and size.

Take a look at this code sample to see how RBloomFilterNative works:

import org.redisson.api.RBloomFilterNative;
import org.redisson.api.RedissonClient;
import org.redisson.api.BloomFilterInfo;
import java.util.Arrays;
import java.util.Set;

public class BloomFilterNativeExample {

    public static void main(String[] args) {
        RedissonClient redisson = ... // Initialize your RedissonClient

        // Get access to the Native Bloom Filter
        RBloomFilterNative bloomFilter = redisson.getBloomFilterNative("username-blacklist");

        // 1. Initialize the Bloom filter
        // Set the expected false positive rate (e.g., 3%) and capacity (e.g., 55 million items)
        bloomFilter.init(0.03, 55000000L);

        // 2. Add elements to the filter
        bloomFilter.add("user_spammer_1");
        bloomFilter.add("user_spammer_2");
        
        // Add multiple elements at once
        bloomFilter.add(Arrays.asList("user_bot_A", "user_bot_B"));

        // 3. Perform membership tests
        // Check if a single element exists
        boolean isPresent = bloomFilter.exists("user_spammer_1");
        System.out.println("Spammer 1 exists in blacklist: " + isPresent);

        // Check multiple elements at once
        Set presentItems = bloomFilter.exists(Arrays.asList("user_bot_A", "clean_user"));
        System.out.println("Items present from batch check: " + presentItems);

        // 4. Retrieve filter statistics
        long count = bloomFilter.count();
        System.out.println("Approximate item count: " + count);

        // Get comprehensive filter info
        BloomFilterInfo info = bloomFilter.getInfo();
        System.out.println("Filter capacity: " + info.getCapacity());
        System.out.println("Filter memory size (bytes): " + info.getSize());
    }
}

If you need an implementation that runs against any Valkey or Redis deployment without the Bloom module, Redisson also provides the standard RBloomFilter and a clustered variant for data partitioning across very large datasets.

Optimize High-Performance Java Apps With Redisson PRO

Implementing a highly efficient, server-side Bloom Filter doesn't have to mean hours of complex coding and testing. With Redisson PRO, you can leverage the native Bloom Filter features of Valkey and Redis and enjoy significant memory efficiency — all while keeping your Java codebase clean and maintainable.

To bring enterprise-grade tooling, superior database performance, and highly optimized data structures to your Java applications, learn more about Redisson PRO today.

Similar articles