Near-Cache with Bloom Filters

What You Will Learn

How to configure a near-cache on the Hot Rod client so that frequently read entries are stored locally, avoiding remote calls. You will also enable bloom filters to reduce the invalidation overhead when entries change on the server.

Prerequisites

  • Java 17+

  • An Infinispan Server running on localhost:11222 (or Docker/Podman available for Testcontainers)

Step 1: Add the Hot Rod Client Dependency

Add the Infinispan Hot Rod client to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-client-hotrod</artifactId>
</dependency>

Step 2: Configure Near-Caching with Bloom Filters

Enable near-caching on a specific remote cache. Set the mode to INVALIDATED, configure the maximum number of entries, and turn on bloom filters:

      ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
      // Add an additional cache with near caching configuration
      builder.remoteCache(CACHE_WITH_NEAR_CACHING)
              .configuration(TutorialsConnectorHelper.TUTORIAL_CACHE_CONFIG.replace("CACHE_NAME", CACHE_WITH_NEAR_CACHING))
              .nearCacheMode(NearCacheMode.INVALIDATED)
              .nearCacheMaxEntries(20)
              .nearCacheUseBloomFilter(true);

      // Connect to the server with the near cache configuration for the test cache
      cacheManager = TutorialsConnectorHelper.connect(builder);
  • NearCacheMode.INVALIDATED keeps the local near-cache consistent by invalidating entries when they change on the server.

  • nearCacheMaxEntries limits memory usage on the client.

  • nearCacheUseBloomFilter(true) reduces the number of invalidation messages the server sends to the client.

Step 3: Compare Read Performance

Populate both a regular cache and the near-cache, then perform 10,000 random reads on each to see the difference:

   static void readCache(RemoteCache<Integer, String> cache) {
      Instant start = Instant.now();
      Random random = new Random();
      random.ints(10_000, 1, 20).forEach(num -> cache.get(num));
      Instant finish = Instant.now();
      long timeElapsed = Duration.between(start, finish).toMillis();
      System.out.println(String.format("Time to complete with cache %s is %d milliseconds", cache.getName(), timeElapsed));
   }

Step 4: Run the Tutorial

mvn package exec:java

You should see that reads on the near-cache complete significantly faster than reads on the regular cache.

What’s Next