The above Cache Containers are used internally by the application server to manage Clustering capabilities. If you want to use an embedded Cache configured in WildFly, start at first by adding it to your configuration:


The above CLI commans will create the following Replicated Cache Container definition:

<cache-container name="replicated_cache" default-cache="default" module="org.wildfly.clustering.server" jndi-name="infinispan/replicated_cache">
                <transport lock-timeout="60000"/>
                <replicated-cache name="mycache" jndi-name="infinispan/replicated_cache/mycache" mode="SYNC">
                    <transaction locking="OPTIMISTIC" mode="FULL_XA"/>
                    <eviction strategy="NONE"/>

So, let's see how you can use the above Cache in a clustered application. We will lookup the cache manager from JNDI, normally with the @Resource annotation such as:

private org.infinispan.manager.CacheContainer container;

Now recall our Java EE 6 sample application which we have introduced at the beginning of our journey, and let's change the Managed Beans so that you use the Infinispan Cache:

package com.sample.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;

import org.infinispan.manager.CacheContainer;

import com.sample.model.Property;
public class PropertyManager {
    private CacheContainer container;   

    private org.infinispan.Cache<String, Property> cache; 
    ArrayList cacheList;

    private String key;
    private String value;
    @PostConstruct  public void start() {     
        this.cache = this.container.getCache("mycache");   
        cacheList = new ArrayList<Property>();

    public String getKey() {
        return key;

    public void setKey(String key) {
        this.key = key;

    public String getValue() {
        return value;

    public void setValue(String value) {
        this.value = value;

    public void save() {
        Property p = new Property();

    public void clear() {

    public List getCacheList() {
        List<Property> dataList = new ArrayList<Property>();
        return dataList; 


    public String generateKey() {
        String uuid = UUID.randomUUID().toString(); 
        return uuid;


You can compile the above class by adding the required Maven dependency to your Maven project:


In order to get working with your Infinispan cache on Wildfly is triggering Infinispan dependencies, either using in your META-INF/MANIFEST.MF or jboss-deployment-structure.xml as follows:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2" xmlns:xsi="">
       <module name="org.infinispan" services="export"/>  
       <module name="org.infinispan.commons" services="export"/>  

As you can see from the above jboss-deployment-structure.xml, we needed also to include a dependency to org.infinispan.commons: this is why, since Infinispan 7 some of the core classes have been moved to the module org.infinispan.commons.
In order to start your cluster nodes you need to provide an unique node name and, since you are running multiple servers on the same machine specify a port offset so that you don’t have port conflicts between servers.

So we will start up NodeA with:

standalone -c standalone-ha.xml                    

and NodeB with:

standalone -c standalone-ha.xml -Djboss.socket.binding.port-offset=100                        

Now deploy the application (see at the bottom for full code) and contact the first node:
infinispan cluster jboss as 7 software
Now move to the second node and verify that cache entries have been replicated to the second node. Add some data in it as well:

infinispan cluster jboss as 7 software

Replication vs Distribution

Entries added to any cache instance will be replicated to all other cache instances in the cluster, and can be retrieved locally from any instance.  This clustered mode provides a quick and easy way to share state across a cluster, however replication practically only performs well in small clusters (under 10 servers), due to the number of replication messages that need to happen – as the cluster size increases.
For this reason, it is adviced to use a distributed cache which allows Infinispan to scale linearly by defining a number of cluster-wide replicas for each cache entry.

Defining a cache as distributed just requires using the <distributed-cache> tag. For example here we are defining a number of 3 cluster-wide replicas (parameter owners) for each entry. (Default is 2)

 <distributed-cache name="dist" mode="ASYNC" batching="true" owners="3">

 You can download here the full code for this example .
Eager to learn more about Infinispan?

infinispan book


Configure and develop applications using the Infinispan Data grid platform
Follow a simple ticket booking example to easily learn the features of Infinispan in practice
Draw on the experience of Manik Surtani, the leader, architect and founder of this popular open source project


Related articles available on