Axis2 Clustering Support

Are you interested in improving Scalability and High Availability of your Web Services?

Axis2 1.4 provides experimental clustering support to add Scalability, Failover and High Availability to your Web Services. This guide will explain the extent of clustering support and it's the current limitations. It also highlights the recommended approaches using examples.

Axis2 clustering support can be used in several scenarios. However it is important to understand the current limitations and the risks/impacts associated with each scenario.

Content

Introduction

In the context of Axis2 clustering, a node is defined as a separate process with a unique port number where it listens for requests on a given transport . A physical machine can contain more than one node.

Scalability

In order to maintain the same level of serviceability (QoS) during an increase in load you need the ability to scale. Axis2 provides replication support to scale horizontally. That is, you can deploy the same service in more than one node to share the work load, thereby increasing or maintaining the same level of serviceability (throughput etc).

Failover

Axis2 provides excellent support for Failover by replicating to backup node(s). If you deploy your Stateful Web Services in this mode, you can designate 1-2 backups and replicate state. In the event the primary node fails, the clients can switch to one of the backups. If you use Synapse with the Failover mediator you can provide transparent Failover.

High Availability

You can improve the availability of your Web Service by using the following Axis2 functionality.

  • Failover support will ensure that a client will continued be served, without any interruption due to a node failure.
  • Scalability support will ensure that your services can maintain the same level of serviceability/availability (QoS) in increased load conditions.
  • Hot Deploy feature ensures that you could deploy new services without shutting down your existing services.

Clustering for Stateless Web Services

This is the simplest use case. If your Web Service does not store any state in the context hierarchy then you could deploy your service in "n" number of nodes. To ensure identical configuration for your services, you can load from a central repository using the URLBasedAxisConfigurator. This is not a must, but it makes management of the cluster easy and less error prone.

Since it is stateless no explicit replication is needed. If a node fails any other node in the cluster can take over. You can use a load balancer to direct requests based on a particular algorithm (Ex: Round Robin, Weight based, Affinity based). You can increase the no of nodes to handle scalability (to scale vertically) without worrying about the overhead of replication as the services are stateless

Clustering for Stateful Web Services

This is a more complicated use case where your Web Service needs to store state in the context hierarchy. Each Web Service instance (deployed in separate nodes) will need to share state among themselves. Axis2 provides replication to support sharing of state among services.

However, if more than one node tries to update the same state in the context hierarchy, conflicts will arise and the integrity of your data will be compromised. Now your cluster will have inconsistent state. This can be avoided using a locking mechanism. However Axis2 currently does not support it yet.

If this shared state is read more frequently and updated rarely the probability of conflicts decrease. You may use Axis2 in the above use case for Stateful Web Services based on your discretion. However it's important to remember that there can be conflicts.If you have frequent writes it is not advisable to use Axis2 until we introduce locking support

Please note this warning is only applicable to the following use cases.

  • Your Service is deployed in Application Scope
  • You store information in the ServiceGroupContext (irrespective of your scope)

You may safely use services in "soapsession" scope provided you don't modify (or modify at all) state in ServiceGroupContext frequently. In soap-session the service context is exclusive to the client who owns the session. Therefore only that client can modify state. A conflict might arise if the same client tries to access the same service in two different nodes simultaneously which happens to modify the same state. However this is rare, but might arise due to an error in the load balancer or the client. If you use Sticky sessions, it will ensure that state will be changed in one node only by directing all requests by the same client to the same node. This is the safest way to use Axis2 clustering support for Stateful Web Services to acheive scalability.

Configuring Axis2 to add Clustering Support

You need to add the following snippet to your axis2.xml

   <cluster class="org.apache.axis2.clustering.tribes.TribesClusterManager">
     <contextManager class="org.apache.axis2.clustering.context.DefaultContextManager">
        <listener class="org.apache.axis2.clustering.context.DefaultContextManagerListener"/>
        <replication>
            <defaults>
                <exclude name="local_*"/>
                <exclude name="LOCAL_*"/>
            </defaults>
            <context class="org.apache.axis2.context.ConfigurationContext">
                <exclude name="SequencePropertyBeanMap"/>
                <exclude name="NextMsgBeanMap"/>
                <exclude name="RetransmitterBeanMap"/>
                <exclude name="StorageMapBeanMap"/>
                <exclude name="CreateSequenceBeanMap"/>
                <exclude name="ConfigContextTimeoutInterval"/>
                <exclude name="ContainerManaged"/>
            </context>
            <context class="org.apache.axis2.context.ServiceGroupContext">
                <exclude name="my.sandesha.*"/>
            </context>
            <context class="org.apache.axis2.context.ServiceContext">
                <exclude name="my.sandesha.*"/>
            </context>
        </replication>
     </contextManager>
   </cluster>

The exclude tag tells the system to avoid replicating that particular property. This is a useful feature as you would need to have properties that is node specific only. The default config in axis2 will have all properties the axis2 system doesn't want to replicate. Web Service developers can also use this to filter out properties that should be local only.

Example 1: Scalability and HA with Stateless Web Services

The following is a good example for deploying a Stateless Web Service for Scalability and High Availability. The following service can be deployed in "application" scope in "n" nodes using a central repository. Once state is loaded by a particular node it will be shared by other nodes as the config context will replicate the data. Even if two nodes load the data at the same time, there want be any conflicts as it is the same set of data. (All nodes should synchronize their clocks using a time server to avoid loading different sets of data)

For the sake of this example we assume replication is cheaper than querying the database. So once queried it will be replicated to the cluster

/**
 * This Service is responsible for providing the top 5
 * stocks for the day, week or quarter
 */
public class Top5StockService
{
	public String[] getTop5StocksForToday()
	{
		// If cache is null or invalid fetch it from data base
		ConfigurationContext configContext =
            MessageContext.getCurrentMessageContext().getConfigurationContext();
		
		String[]  symbols = (String[])configContext.getProperty(TOP5_TODAY);
		if (!checkValidity(configContext.getProperty(TOP5_TODAY_LOAD_TIME)))
                {
		    symbols = loadFromDatabase(TOP5_TODAY);
                    configContext.setProperty(TOP5_TODAY,symbols);
		    configContext.setProperty(TOP5_TODAY_LOAD_TIME,new java.util.Date()); 	 
                } 
		
		return symbols;
	}
	
	public String[] getTop5StocksForTheWeek()
	{
		 // If cache is null or invalid fetch it from data base
		.............
	}
	
	public String[] getTop5StocksForTheQuarter()
	{
		// If cache is null or invalid fetch it from data base
                ............
	}
}

Example 2: Failover for Stateful Web Services

The following example demonstrates Failover support by replicating state in a service deployed in "soapsession" scope. You can deploy the service in 2 nodes. Then point a client to the first node and add a few items to the shopping cart. Assuming the primary node has crashed, point the client to the backup node. You should be able to checkout the cart with the items you added in the first node.

public class ShoppingCart
{	
	public final static String SHOPPING_CART = "SHOPPING_CART";
	public final static String DISCOUNT = "DISCOUNT";
	
	public void createSession()
	{
		List<Item> cart = new ArrayList<Item>();
		ServiceContext serviceContext =
            MessageContext.getCurrentMessageContext().getServiceContext();
		serviceContext.setProperty(SHOPPING_CART, cart);
	}
	
	public void addItem(Item item)
	{
		ServiceContext serviceContext =
            MessageContext.getCurrentMessageContext().getServiceContext();
		List<Item> cart = (List<Item>)serviceContext.getProperty(SHOPPING_CART);
		cart.add(item);
	}
	
	public void removeItem(Item item)
	{
		ServiceContext serviceContext =
            MessageContext.getCurrentMessageContext().getServiceContext();
		List<Item> cart = (List<Item>)serviceContext.getProperty(SHOPPING_CART);
		cart.remove(item);
	}
	
	public double checkout()
	{
		ServiceContext serviceContext =
            MessageContext.getCurrentMessageContext().getServiceContext();
		List<Item> cart = (List<Item>)serviceContext.getProperty(SHOPPING_CART);
		
		double discount = (Double)serviceContext.getServiceGroupContext().getProperty(DISCOUNT);
		
		double total = 0;
		for (Item i : cart)
		{
			total = total + i.getPrice();
		}
		
		total = total - total * (discount/100);
		
		return total;
	}	
}

Example3: Scalability and HA with Stateful Web Services

You can deploy the the above Shopping Cart service in several active nodes (with a backup(s) for each node). You only replicate to your backup nodes for Failover. The load balancer should ensure sticky sessions. The strategy is to partition your load between the active nodes to achieve scalability and replication to the backups to achieve Failover. These in turn will increase the high availability of your services. Since the above example doesn't use Service Group Context to write any state there want be any conflicts.

For the sake of this example we assume that all read only properties for the Service Group Context is loaded at initialization Please note this is the recommended approach for Stateful Web Services due to the current limitations

Summary

Apache Axis2 provides experimental support for clustering to improve the following properties of your Web Services.

  • Scalability
  • Failover
  • High Availability
It is important to understand the current limitations when leveraging clustering support.

For Further Study

Apache Axis2

Axis2 Architecture

Introduction to Apache Axis2-http://www.redhat.com/magazine/021jul06/features/apache_axis2/