Java Web Sessions with NCache
Sessions are a way to store data for distinct users. Java session objects, made on the application server, called servlet containers which hosts the web application, inside a Java Servlet or a Java Server Page (JSP) include Apache Tomcat, WildFly, Web Logic and Web Sphere.
For instance, Tomcat web server provides session handling through session replication over Tomcat web server clusters. Session replication may cause data and memory overhead, becoming slow and lacking performance and scalability.
Therefore, NCache provides a better option where users can store their Java sessions inside their application without any problem in a load balanced web farm.
Different Servlet Containers Associated with NCache
NCache provides its users with the options of using different types of servlet containers which can host their web application with ease as discussed below.
- Apache Tomcat: It is an open-source implementation of Java Servlet and Java Server pages and also provides “pure Java” HTTP web server environment in which Java code can run. It provides session handling through sticky sessions.
- WildFly: It is an application server written in Java which deploys and manages Java applications in a variety of environments.
- Web Logic: It is a Java Enterprise Edition (EE) compliant application which deploys, runs and distributes the Java based applications.
- Web Sphere: It is the most advanced application server for building and running Java applications.
Configuring Java Session Module for NCache
For configuring Java sessions, one needs to modify the “session.xml” file located at %NC_Home%/config in the NCache folder. Following is the code that does it for NCache. For more details regarding adding libraries, defining filters and deploying the application using servlet containers, see Adding Libraries and Defining Filter for help.
<?xml version="1.0" encoding="UTF-8"?>
<!--for single regional session provider-->
<servlet-session-config>
<cache id="myPartitionedCache"/>
<log log-props="log4j.properties"/>
<locking enable-session-locking="true" retries-count="2" retry-interval="500ms" lock-timeout="36000ms" empty-session-when-locked="false"/>
</servlet-session-config>
Benefits of Java Session Module for NCache
NCache provides the user with a “no code change” option for Java Applications to persist sessions in NCache distributed caching. If your application is running in a load balanced web server farm and you need a reliable and scalable storage for your session persistence, NCache gives the following advantages:
- Best Performance: NCache provides fastest response time, hence improved performance.
- High Availability: NCache provides strong distributed cache clusters with no single point of failure which are highly available meaning you can add or remove cache without stopping your application.
- Extreme Reliability: NCache lets the user replicate Java servlet sessions across multiple servers according to their specified topology without compromising the reliability and performance. Which is why the user won’t lose any session data even if the cache server goes down for any reason.
- Brilliant Session Replication through Topologies: NCache provides various caching topologies through which you persist your sessions according to your needs. For instance, one can use partitioned-replicated topology for more reliability and scalability.
- Linear Scalability: NCache, an in-memory distributed linearly scalable grid, has the ability to handle high loads which is why session applications can achieve as much scalability as possible.
Support for Multiple Regions in Java Web Sessions
To manage Java servlet sessions across multiple web farms, which are geographically separately located, NCache provides multi-site session persistence in the following ways:
- Java Web Sessions WAN Replication: NCache lets a user replicate all the Java sessions across the WAN to another region (data center) which is done through the Bridge Topology feature of NCache. This ensures that your Java sessions are never lost even if a region goes down.
- Multi-site Java Web Sessions: If a user does not want to replicate the sessions across the WAN mainly because of high bandwidth consumption and possible performance issues, then they can choose to use the concept of Multi-site Java session support, a feature of NCache. In this approach, each region can configure their separate caches and every cache will have its own set of clients (Java servlet). The session will not be replicated across sites but instead will be kept in its location of creation.
Through this we can have two or more active separate regions and a user can keep most of the traffic to its own region. Likewise, if user brings a region down for some reason, it will not cause any problem because that region will be accessible by other regions.
Advanced Features in NCache Java Web Sessions
Java Web Sessions for NCache implements all the standard features provided by Java Web Sessions. Furthermore, it implements some advanced features to handle other important situations.
- Session Expiration: NCache handles session expiration and removes sessions from cache for a specified session timeout. Similarly, we can specify session timeout value through servlet containers and NCache will internally use that interval to expire sessions from the cache.
- Handling Multiple Session Requests and Data Integrity: To avoid the situation where parallel requests can cause sessions to be mutate, NCache provides a locking mechanism so that no two parallel requests can change the same session. You can specify it in “session.xml” file in the following way:
<servlet-session-config>
<locking enable-session-locking="true" retries-count="2" retry-interval="500ms" lock-timeout="36000ms" empty-session-when-locked="false"/>
</servlet-session-config>
- Handling Session Data Serialization: After putting your Java objects in Java servlet sessions and storing them out-of-process, the entire Java servlet session must be serialized. NCache provides faster serialization compared to the regular Java serialization, called Compact Serialization. With this your Java application speed increases a lot.
What to Do Next?