JSP Session Persistence and Replication with Distributed Cache

As you know, JSP applications have the concept of a Session object in order to handle multiple HTTP requests. This is because HTTP protocol is stateless and Session is used in maintaining user’s state across multiple HTTP requests.

In a single web server configuration, there is no issue but as soon as you have a multi-server load balanced web farm running a JSP application, you immediately run into the issue of where to keep the Session. This is because a load balancer ideally likes to route each HTTP request to the most appropriate web server. But, if the Session is kept only on one web server then you’re forced to use the sticky session feature of the load balancer where it sends requests related to a given Session only to the web server where the Session resides.

This means one web server might get overwhelmed even when you have other web server sitting idle because the Session for that user resides on that web server. This of course hampers scalability. Additionally, keeping the Session object only on one web server also means loss of Session data if that web server goes down for any reason.

To avoid this data loss problem, you must have a mechanism where Session is replicated to more than one web server. Here, the leading Servlet Engines (Tomcat, WebLogic, WebSphere, and JBoss) all provide some form of Session persistence. They even include some form of Session replication but all of them have issues. For example, file based and JDBC persistence are slow and cause performance and scalability issues. Session replication is also very weak because it replicates all sessions to all web servers thereby creating unnecessary copies of the Session when you have more than two web servers even though fault tolerance can be achieved with only two copies.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

In such situations, a Java distributed cache like NCache is your best bet to ensure the session persistence across multiple servers in web cluster is done very intelligently and without hampering your scalability.  NCache has a caching topology called “Partition-Replica” that not only provides you high availability and failover through replication but also provides you large in-memory session storage through data partitioning. Data partitioning enables you to cache large amounts of data by breaking up the cache into partitions and storing each partition on different cache servers in the cache cluster.

NCache Partition-Replica topology replicates the session data of each node to only one other node in cluster this approach eradicates the performance implications of replicating session data on all of the server nodes without compromising the reliability.

In addition, session persistence provided by the web servers (Apache, Tomcat, Weblogic and WebSphere) uses the resource and memory of web cluster. This approach hinders your application performance because the web cluster nodes which are responsible to process the client request are also handling the extra work of session replication and its in-memory storage.  Whereas, you can run NCache on separate boxes other the one part of your web cluster. This way you can free the web cluster resources and web cluster can use those resources to handle more and more client requests.

For JSP session persistence, NCache has implemented a session module NCacheSessionProvider as Java filter. NCache JSP Servlet Filter dynamically intercepts requests and responses and handles session persistence behind the scenes. And, you don’t have to change any of your JSP application code.

Here is a sample NCache JSP Servlet Filter configuration you need to define in your application deployment descriptor to use NCache for JSP Session persistence:

<filter-class> com.alachisoft.ncache.web.sessionstate.NSessionStoreProvider



Hence, NCache provide you a much better mechanism to achieve session persistence in web cluster along with performance boost and scalability.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

Avatar for Iqbal Khan

Leave a Reply

Your email address will not be published. Required fields are marked *