Conceptual Overview of Java Sessions
The Java Server Page session provides the mechanism to identify a user across multiple page requests. For this purpose, various servlet containers provide Java 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, making failover recovery impossible if that server 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. These problems become more concerning when your Java servlet application runs in a load-balanced web server farm.
Java Session Persistence in NCache
NCache provides a "no code change" option for Java applications to persist Java sessions. For reliable and scalable session persistence in a load-balanced web server farm, NCache offers benefits such as:
Performance: NCache provides faster response times resulting in improved performance.
Scalability: NCache is an in-memory distributed data grid that offers linear scalability. By adding more nodes to your cache cluster, you can effortlessly handle high transaction loads and achieve the required level of scalability.
Session replication topologies: NCache offers various caching topologies to suit your Java session persistence needs. For example, you can use the Replicated Topology for enhanced failover support or the Partition-Replica Topology for greater reliability and scalability in your cache clusters.
Reliability: NCache lets you intelligently replicate Java sessions across multiple servers as per 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 offers robust distributed cache clusters with no single point of failure, allowing you to add or remove cache servers at runtime without interrupting 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. 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
Parallel HTTPS requests for the same session in the case of AJAX (Asynchronous JavaScript and XML) can cause a session to mutate independently. NCache Session Persistence uses a Locking mechanism to prevent this from happening and ensure that no two parallel requests can change the same session. The first request will acquire a lock on the session, and subsequent requests will wait until the first request completes and releases the lock.
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 its objects must employ regular Java serialization. This serialization uses reflection at runtime and is, therefore, slow. NCache provides an approximately 10 times faster serialization called Compact Serialization. With this, your Java application becomes much faster.
With Compact Serialization, NCache enables you to register your Java classes and generates the serialization source code upon your application's connection to the cache. This source code is then compiled in-memory, significantly speeding up the serialization process compared to standard Java serialization.
See Also
Multi-site Java Session Support
Configuring Applications to Use Java Session Module
Error Logging