Redis-Based Bloom Filter for Java
Learn how to use Bloom filters in Java and Redis with the Redis Java client Redisson.
Join the DZone community and get the full member experience.
Join For FreeA Bloom filter is a probabilistic data structure that is used to efficiently test whether an element is present in a set. The use of Bloom filters can help reduce the number of expensive disk lookups for key-value pairs.
In the Java programming language, developers can use a variety of pre-built Bloom filter data structures, including the BloomFilter class in Google’s Guava core Java libraries.
You may also like: Redis for Java Developers: Tutorial and Code Examples
Redis is an open-source in-memory data structure store that can be used to implement a NoSQL database. However, Java is not compatible with Redis out of the box.
Java developers will have to use a Redis Java client in order to access features such as Bloom filters. In this article, we’ll discuss how to use Bloom filters in Java and Redis with the Redis Java client Redisson.
Bloom Filters in Redis and Java with Redisson
Redisson is an ultra-fast, lightweight Java client for Redis that provides many common Java objects and functionality, including Bloom filters.
The following example code demonstrates how to use Bloom filters in Redisson with the RBloomFilter interface:
RBloomFilter<SomeObject> bloomFilter = redisson.getBloomFilter("sample");
// initialize Bloom filter with
// expectedInsertions = 55000000
// falseProbability = 0.03
bloomFilter.tryInit(55000000L, 0.03);
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
bloomFilter.count();
Bloom filters are a probabilistic data structure: they can definitively state that an element is not present in the set, but can only say that an element may be present in the set. The falseProbability
parameter controls the probability of having a false positive with the given RBloomFilter
.
The expectedInsertions
parameter defines the expected number of insertions per element. An RBloomFilter
object may contain up to 2^32 bits.
Redisson also supports distributed Bloom filters in Redis via the RClusteredBloomFilter
interface. The RClusteredBloomFilter
is more memory-efficient, shrinking the memory used across all Redis nodes. An RClusteredBloomFilter
object may contain up to 2^64 bits. Note that the RClusteredBloomFilter
is only available in Redisson's cluster mode.
The following example code demonstrates how to use the RClusteredBloomFilter
interface:
xxxxxxxxxx
RClusteredBloomFilter<SomeObject> bloomFilter = redisson.getClusteredBloomFilter("sample");
// initialize Bloom filter with
// expectedInsertions = 255000000
// falseProbability = 0.03
bloomFilter.tryInit(255000000L, 0.03);
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
And that's it!
Further Reading
Redis for Java Developers: Tutorial and Code Examples
Opinions expressed by DZone contributors are their own.
Comments