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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Understanding and Mitigating IP Spoofing Attacks
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  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
24.7K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!