Replicated Cache with Embedded Infinispan
What You Will Learn
How to create a clustered Infinispan cache in replicated synchronous mode, where every entry is copied to all nodes in the cluster.
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 Replicated Cache
Set up a clustered cache manager and configure a cache with REPL_SYNC mode. Every entry is synchronously replicated to all nodes:
// Setup up a clustered cache manager
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
// Initialize the cache manager
cacheManager = new DefaultCacheManager(global.build());
// Create a replicated synchronous configuration
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.clustering().cacheMode(CacheMode.REPL_SYNC);
Configuration cacheConfig = builder.build();
// Create a cache
cache = cacheManager.administration()
.withFlags(CacheContainerAdmin.AdminFlag.VOLATILE)
.getOrCreateCache("cache", cacheConfig);
Step 3: Populate and Query the Cache
Store entries and observe that each node holds all the data. Use SKIP_REMOTE_LOOKUP to see the local view:
// Store the current node address in some random keys
for(int i=0; i < 10; i++) {
cache.put(UUID.randomUUID().toString(), cacheManager.getNodeAddress());
}
// Display the current cache contents for the whole cluster
cache.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
// Display the current cache contents for this node
cache.getAdvancedCache().withFlags(Flag.SKIP_REMOTE_LOOKUP)
.entrySet().forEach(entry -> System.out.printf("%s = %s\n", entry.getKey(), entry.getValue()));
In replicated mode, both views show the same entries because every node has a full copy of the data.
Step 4: Run the Tutorial
mvn package exec:java
The local and cluster-wide views will display the same entries, unlike distributed mode where data is partitioned.
What’s Next
-
Compare with distributed mode where data is partitioned
-
Try invalidation mode for locally-managed data
-
Use distributed streams for parallel processing


