There are two main reasons for deploying your web app to a clustered environment:
- Load balancing and scaling.
- High availability and failover.
Load balancing spreads the load over several servers, so you can add more servers to cope with increased load.
High availability means that if a server fails, other servers in the cluster can take up the load, so the system as a whole is still available to your users.
When deploying your web app to a cluster, you will need to do the following:
- Add the <distributable/> tag in web.xml.
- Make sure that all the objects you put in the session implement the Serializable interface.
- When the contents of an oject in the session change, call HttpSession.setAttribute() to make sure that the server knows that the object needs to be replicated to the other members of the cluster.
- Be aware that static variables only contain the same values within one JVM. In a cluster, you cannot rely on this, for example you may cache the results of a database query in a static variable, but you cannot rely on the cache containing the same values on all members of a cluster.
- You will probably want to configure your load-balancer to use session affinity (sticky sessions) so that, apart from when a member of the cluster fails, all requests relating to a session are handled by the same server.