Conceptual Overview of Java Web Sessions
The Java Server Page session provides the mechanism to identify a user across multiple page requests. For this purpose, various servlet containers provide session replication mechanisms. For instance, the Tomcat web server provides session handling through sticky sessions or session replication over the Tomcat web server cluster. Sticky sessions persist on a server that receives web requests, which means you can't guarantee failover recovery if the server node goes down. Also, session replication over multiple server instances may cause memory and data transfer overhead. Session clustering replicates the session but slowly. Both these options have serious scalability issues. Similarly, WebLogic provides in-memory session replication (replicates session state from one instance to another) and JDBC-based persistence that may cause performance issues. In short, the available options in different servlet containers are slow, with poor scalability or a lack of reliability. And, these problems become more concerning when your Java servlet application runs in a load-balanced web server farm.
Session Persistence in NCache
NCache provides a "no code change" option for Java applications to persist sessions with NCache distributed caching. If you require reliable and scalable storage for session persistence and your application runs on a web server farm with load-balancing configured, NCache provides you with advantages like:
Performance: NCache provides faster response times resulting in improved performance.
Scalability: It's an in-memory distributed data grid that provides linear scalability. As you increase your cache cluster nodes to tackle extreme transaction load 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 a Replicated Topology for cache clusters which provides increased failover support, or Partition-Replica for more reliability and scalability.
Reliability: NCache lets you intelligently replicate Java servlet sessions across multiple servers according to the topology used without compromising on 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 without a 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 changes to your application. All you need is to modify your application's configuration file and add NCache session storage support as a Filter to your Java servlet application. The NCache session provider will be configured as a filter in your web application. You will also need to change the session persistence settings in the session.xml file for cache ID and session mode settings. The NCache session provider filter will intercept the HTTPS request/response for sessions’ management and eventually store this session in the configured cache.
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. The NCache session provider will be configured as a filter in your web application. You also need to change session persistence settings in the session.xml file for cache ID and session mode settings. The NCache session provider filter will intercept HTTPS requests/responses to manage sessions and eventually store this session in the configured cache.
Handling Multiple Session Requests and Data Integrity
Handling Session Data Serialization
When you put your Java objects in a Java servlet session and store it out-of-process, the entire session and it's objects must employ regular Java serialization. This regular Java serialization uses reflection at runtime and is, therefore, slow. NCache provides an approximately 10 times faster serialization called Dynamic Compact Serialization. With this, your Java application speed increases noticeably. And it doesn't require any code change.
With Dynamic Compact Serialization, NCache allows 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 compiles this source code in-memory and uses it for serialization. That is why it is so much faster than regular Java serialization.