A close look inside Infinispan Distribution mode

What you like to have a close look over the Infinispan distribution of data in your JBoss EAP 6 / WildFly cluster? I’ll tell you how to do it.

The default algorithm used by WildFly application server for clustering is based on the Infinispan distribution. This means that cache entries are copied to a fixed number of cluster nodes (2, by default) regardless of the cluster size. Distribution uses a consistent hashing algorithm to determine which nodes will store a given entry.

infinispan distribution cluster


Caches in turn are available to the application server in the form of Cache containers. Some of them are already available in the infinispan configuration such as the web cache and the ejb cache and they can be retrieved in your application as follows:

private CacheContainer container; 

Now we will show how to create a simple Web application which stores the keys in the HTTP Session, then we show in a table where keys are located in the server and the server elected as primary server.

Let’s build a simple EJB which can be used for this purpose:

public class CacheInspector {
	 private CacheContainer container; 
	 private org.infinispan.Cache<String, String> cache;
	 public void init() {
		 this.cache = container.getCache();
	 public String locateServers(String key) {
			List<Address> list = this.cache.getAdvancedCache().getDistributionManager().locate(key);
			if (list != null)
			else return null;
	 public String locatePrimary(String key) {
			Object list = this.cache.getAdvancedCache().getDistributionManager().getPrimaryLocation(key);
			if (list != null)
			else return null;

The locateServers method is used to retrieve the list of Servers where a particular key is stored. The number of elements in the List<Address> corresponds to the owners in the infinispan configuration. To keep it simple we return the ArrayList as a String of servers.

The locatePrimary method, on the other hand, returns the Node which has been elected as primary node in the cluster.

Our application is almost ready, all we need is some “glue” to reach out the EJB:

public class Bean {

	private String key;
	private String value;
	List propertyList = new ArrayList();
    @EJB CacheInspector ejb;
    // Getter/setters here

	public void save() {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) facesContext.getExternalContext()

		session.setAttribute(key, value);
		propertyList = new ArrayList();
		Enumeration e = session.getAttributeNames();
		while (e.hasMoreElements()) {
			String attr = (String) e.nextElement();
			Item item = new Item();
			String location = ejb.locateServers(attr);
			String primary = ejb.locatePrimary(attr);


 And finally a simple view to add some key/values in the HttpSession, through the save method of the Bean class:

<html xmlns="http://www.w3.org/1999/xhtml"
	<style type="text/css">
	<h2>Cache distribution demo</h2>
	<h:form id="jsfexample">
		<h:panelGrid columns="2" styleClass="default">

			<h:outputText value="Enter key:" />
			<h:inputText value="#{bean.key}" />

			<h:outputText value="Enter value:" />
			<h:inputText value="#{bean.value}" />

			<h:commandButton actionListener="#{bean.save}"
				styleClass="buttons" value="Save key/value" />

			<h:messages />


		<h:dataTable value="#{bean.propertyList}" var="item"
			styleClass="table" headerClass="table-header"
				<f:facet name="header">Key</f:facet>
				<h:outputText value="#{item.key}" />
				<f:facet name="header">Location</f:facet>
				<h:outputText value="#{item.servers}" />
				<f:facet name="header">Primary</f:facet>
				<h:outputText value="#{item.primary}" />


 Our application is ready so let’s start a JBoss EAP 6 – WildFly in HA mode with a couple of nodes. Deploy the application and start adding some entries:

infinispan distribution cache mode

As you can see from the above picture, with only two servers and owners=2 the distribution acts like a replication. If we add some more nodes to the cluster you can see that each key is available just on two nodes (out of the four nodes of the cluster):

infinispan distribution cluster

You can use the above code as foundation for learning the algorithms used by Infinispan to distribute cache entries and how they can be influenced by setting server hints in the JGroups transport section. Have fun with distribution!