JavaEE – Clustering support in JSF, Wicket, ZK, etc.

When a seasoned MVC framework developer starts writing code in component based framework like JSF, Wicket, ZK, etc. they fail to understand the basic differences between MVC and component based framework because they could write code which can work in non-clustered environment i.e. in single JVM deployment. When the same application is deployed in a clustered environment they start realizing the difference which will require lots of effort revisit all the code and make it compatible for clustering. I am going to talk about a few guidelines on making development (on top of component based framework ) cluster aware.

MVC vs. Component framework

MVC frameworks are stateless which means framework doesn’t store any information into HttpSession object, for example if you want to pass a value from one page to an another page you have to either use hidden tag or url query string. On the other hand component based frameworks are event driven programming which require components’ state to be preserved in HttpSession object. For example if you are using JSF, managed beans instance (in case of ZK it is ViewModel, in case of Wicket it is Page) are stored in HttpSession to re-use within the same session for further http request processing. A good part is you do not have to use hidden or query string to pass values instead same managed bean instance available across multiple http request but this good feature comes with some hidden issues.

Servlet Container Clustering

To make Java based web application work on clustered environment servlet containers have to replicate HttpSession object state across multiple server (i.e. multiple JVM). To transfer HttpSession object from one JVM to an another JVM servlet container have to serialize all the attributes stored inside the HttpSession. What happens if your managed bean does not implement Serializable interface or one of its member variables are not serializable? HttpSession replication will fail which you will notice only on clustered environment, during development (with single JVM) you will not notice this issue. Obviously then you will start making changes in your code to make sure all the managed beans are serializable to get it work in clustering environment.

Once you resolve the serialization issue you will start noticing performance and memory issues when you support high volume of concurrent users. To avoid memory issue you need to make sure all managed beans are small i.e. only required objects are declared as member variable in other word keep only mandatory values as member variables which are absolutely required to retain the state of Bean or Model. Wherever is possible use temporary variables within methods which will not be serialized.

Checklist

All beans and models should be serializable.

Size of the each bean and model should be as minimal as possible.

I used the code below to capture serializable issue during development.