DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Boosting Application Performance With MicroStream and Redis Integration
  • Implementing Infinite Scroll in jOOQ
  • Intercepting Filter Design Pattern in Java

Trending

  • DZone's Article Submission Guidelines
  • Mocking Kafka for Local Spring Development
  • A Deep Dive into Tracing Agentic Workflows (Part 1)
  • Retesting Best Practices for Agile Teams: A Quick Guide to Bug Fix Verification
  1. DZone
  2. Coding
  3. Java
  4. Redis-Based Bloom Filter for Java

Redis-Based Bloom Filter for Java

Learn how to use Bloom filters in Java and Redis with the Redis Java client Redisson.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Dec. 20, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
25.3K Views

Join the DZone community and get the full member experience.

Join For Free

Learn how to use Bloom filters in Java and Redis with the Redis Java client Redisson.

A 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:

Java




x
11


 
1
RBloomFilter<SomeObject> bloomFilter = redisson.getBloomFilter("sample");
2
// initialize Bloom filter with 
3
// expectedInsertions = 55000000
4
// falseProbability = 0.03
5
bloomFilter.tryInit(55000000L, 0.03);
6
 
          
7
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
8
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
9
 
          
10
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
11
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:

Java
xxxxxxxxxx
1
11
 
1
RClusteredBloomFilter<SomeObject> bloomFilter = redisson.getClusteredBloomFilter("sample");
2
// initialize Bloom filter with 
3
// expectedInsertions = 255000000
4
// falseProbability = 0.03
5
bloomFilter.tryInit(255000000L, 0.03);
6
 
          
7
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
8
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
9
 
          
10
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
11
 
          


And that's it!

Further Reading

Redis for Java Developers: Tutorial and Code Examples

Java Distributed Caching in Redis

Quickstart: How to Use Redis on Java

Java (programming language) Filter (software) Redis (company)

Opinions expressed by DZone contributors are their own.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Boosting Application Performance With MicroStream and Redis Integration
  • Implementing Infinite Scroll in jOOQ
  • Intercepting Filter Design Pattern in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook