Cache Aliases with Embedded Infinispan

What You Will Learn

How to create a distributed cache with an alias in embedded Infinispan, access the same cache data through different names, and dynamically add new aliases at runtime.

Prerequisites

  • Java 17+

Step 1: Add the Embedded Dependency

Add Infinispan core to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-core</artifactId>
</dependency>

Step 2: Create a Cache with an Alias

Configure a distributed cache and assign an alias using the aliases() method on the configuration builder:

      //Create cache configuration
      ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.clustering().cacheMode(CacheMode.DIST_SYNC);
      builder.aliases(ALIAS_1);

      // Create a cache
      cache = cm1.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
              .getOrCreateCache(DIST_CACHE_NAME, builder.build());

The cache is now accessible by both its original name and the alias. Any data stored through one name is visible through the other.

Step 3: Access the Cache by Alias

Retrieve the cache using the alias name. It returns the same underlying cache:

      cm1.getCache(cacheName)
              .entrySet()
              .forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));

Step 4: Add a New Alias at Runtime

Use updateConfigurationAttribute to dynamically add another alias to an existing cache:

      EmbeddedCacheManagerAdmin administration = cm1.administration();
      administration.updateConfigurationAttribute(DIST_CACHE_NAME, "aliases", ALIAS_2);

After this update, the cache is accessible by the original name, the first alias, and the new alias.

Step 5: Run the Tutorial

mvn package exec:java

The output shows the same cache entries when accessed by the cache name or any of its aliases.

What’s Next