JSP session provides the mechanism by which a user is identified across more than one page request. For this purpose, various servlet containers provide session replication mechanisms. For instance, Tomcat web server provides sessions handling through sticky sessions or session replication over tomcat web servers cluster. Sticky sessions persist on a server, which receives the web request, which means you can't guarantee failover recovery if that server node goes down. Also session replication over multiple server instances may cause memory and data transfer overhead. Session clustering replicates the session but is slow. And, both options have serious scalability issues. Similarly Web logics provides in memory session replication (replicates session state from one instance to another) or JDBC-based persistence which may cause performance issues. In short, available options in different servlet containers are either slow in performance, low or no scalability, or lack of reliability. And, these problems become more severe when your Java servlet application runs in a load-balanced web server farm.
Session Persistence in NCache
NCache provides you with a "no code change" option for java applications to persist sessions in NCache distributed caching. If your application is running in a web server farm with load balanced configured and you need a reliable and scalable storage for your session persistence, then NCache provides you with advantages like:
Performance: NCache has high performance as it provides extremely fast response time.
Scalability: It's an in-memory distributed data grid which provides linear scalability. As you increase your cache cluster nodes to tackle extreme transaction load, performance also improves and hence you can achieve as much scalability as needed.
Session replication topologies: NCache provides different caching topologies through which you persist your sessions according to your needs. For instance you can use replicated topology for cache clusters which provide more failover support or partitioned-replicated for more reliability and scalability.
Reliability: NCache lets you intelligently replicate java servlet sessions across multiple servers according to used topology without compromising reliability and performance. So, you won't lose any session data even if a cache server goes down.
Availability: NCache provides strong distributed cache clusters with no single point of failure. In this way, you can change your cluster by adding or removing cache servers at runtime without stopping your application or the cache.
Using Java Session through NCache with No Code Change
You can maintain session persistence without any code change in your application. All you need to do is modify your application's configuration file and add NCache session storage support as a Filter to your Java servlet application.
NCache session provider will be configured as a filter in your web application. You also need to change session persistence settings in session.xml file for cache ID and session mode settings. NCache session provider filter will intercept http request/response for sessions’ management and eventually store this session in configured cache.
NCache handles session expiration and removes sessions from cache when specified session timeout has elapsed. You can specify session timeout value through servlet containers and NCache will internally use that interval to expire sessions from cache. NCache uses sliding expiration, one of its own features, to invalidate sessions. Sessions are inserted in cache with a sliding expiration of specified <session time out> interval. This interval will be reset every time that session is accessed. Sessions will be removed from cache if it will remain inactive for the specified <session time out> interval.
Handling Multiple Session Requests and Data Integrity
Parallel HTTP request for the same session in case of AJAX can cause session to be mutated independently. To avoid such a situation, NCache session persistence uses locking mechanism so that no two parallel requests can change the same session. First requests will acquire lock on the session and other requests will wait, until the first request is completed and it releases the lock.
Handling Session Data Serialization
When you put your java objects in java servlet sessions and store it out-of-process, the entire java servlet session must be serialized along with your Java objects with regular java serialization. And this regular java serialization uses reflection at runtime and is therefore quite slow. NCache provides almost 10 times faster serialization called Dynamic Compact Serialization. With this, your java application speed increases noticeably. And, the best thing is that you don’t have to write any code for it.
With Dynamic Compact Serialization, NCache provides a way for you to register your Java classes with NCache and NCache generates the serialization source code at runtime when your application connects to the cache. NCache then compiles this source code in memory and uses it for serialization. That is why this is so much faster than regular Java serialization.