Cross-Site Replication
What You Will Learn
How to configure a Hot Rod client with backup clusters, perform cache operations across two sites (LON and NYC) with active-passive replication, and manually switch between clusters.
Prerequisites
-
Java 17+
-
Two Infinispan clusters running with cross-site replication enabled (use the provided
docker-composeorminikubesetup in this module, then run thecreate-data.shscript)
Step 1: Connect to the Primary Cluster and Add a Backup
Configure the client to connect to the default LON cluster and add NYC as a backup cluster:
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
builder.addCluster("NYC").addClusterNodes("localhost:31223");
client = TutorialsConnectorHelper.connect(builder);
Step 2: Create or Get the Cross-Site Cache
Create the cross-site cache on both clusters:
cache = client.administration()
// this cache should exist if you start with docker-compose and run the create-data.sh script
.getOrCreateCache(XSITE_CACHE, new StringConfiguration("<distributed-cache/>"));
client.switchToCluster("NYC");
client.administration().getOrCreateCache(XSITE_CACHE, new StringConfiguration("<distributed-cache/>"));
client.switchToDefaultCluster();
Step 3: Put Data and Switch Clusters
Put data on LON, switch to NYC to read it (replicated via active-passive), then put data on NYC:
cache.put("hello", "world");
printCluster("LON", cache);
System.out.println("hello " + cache.get("hello") + " from LON");
printCluster("NYC", cache);
System.out.println("hello " + cache.get("hello") + " from NYC");
cache.put("hello-nyc", "world");
System.out.println("hello-nyc " + cache.get("hello-nyc") + " from NYC");
client.switchToDefaultCluster();
printCluster("LON", cache);
Data put on LON replicates to NYC, but data put directly on NYC does not replicate back to LON (active-passive).
Step 4: Run the Tutorial
mvn package exec:java
After running, you can verify:
-
LON (
localhost:11222/console): containshellobut nothello-nyc -
NYC (
localhost:31222/console): contains bothhello(replicated) andhello-nyc
What’s Next
-
Get started with basic remote cache operations
-
Use near-caching for faster reads


