« Fun with DSO and Cyclic Barrier | Main | Object Identity, Tradition and DSO - Part 2 »
August 30, 2005
Drop-in WLS Replicated Session Replacement
posted by teck
The problem of managing conversational state (i.e. HttpSession) within a clustered web application is fairly well known. A clustered application that relies on the use of session data must have a robust and efficient session layer underneath it. The standard approach is to provide some form of data replication to another node(s) or to a database (gasp). All of the major application server vendors (and a few 3rd party products) provide mechanisms to enable data replication, BUT developers need to keep a fair number of constraints in mind when designing with these solutions (in no particular order):
- If the state of any object referenced by a session is modified, a call to setAttribute() is required to inform the system that replication is necessary
-
Objects placed into session must implement
java.io.Serializableand thus inherit the standard issues associated with serialization (performance overhead, versioning, duplicate instances upon de-serialization, etc) - Session data may not be globally available within the cluster (depending on the replication model)
- Replicating data to other nodes has a significant impact on performance and memory usage
- Session replication is usually only available in higher priced “enterprise” or “cluster” editions of certain products
A Perfect Fit
Before I explain Terracotta’s solution, it’s worth pointing about some key characteristics of session data that make it a splendid use case for Distributed Shared Objects (DSO)
- Session data is almost always transient in nature, usually only existing for the lifetime of the user’s interaction with the application
- The volume of session data grows 1:1 with number of concurrent users
- The concurrency of updates is extremely focused (the scope is the session itself)
- The use of load balancing (with session affinity) makes for very good locality of reference
- The servlet request is a natural and easy to understand transaction/lock boundary
Terracotta provides a drop-in (zero code change) session layer replacement for WebLogic™ server (other app servers coming soon). When using Terracotta enabled sessions, developers are freed from the constraints of the existing solutions. Additionally, since our implementation is based on DSO technology, you get a number of other significant benefits as well.
-
First of all, there is no more requirement to call
setAttribute()to force data replication. Mandating the use ofsetAttribute()is error prone (forgetting to use it), and it is inefficient (leading to performance problems) if needlessly invoked. DSO is built from the ground up to record and distribute fine grained (field level) changes automatically. Furthermore, those fine grained updates are only sent to the set of nodes that actually hold instances of the affected object. In the case of sticky sessions, that set will almost always be empty. - Session data only exists in memory on those nodes that have actually accessed a particular session. Furthermore, since DSO takes care of managing memory, only the minimum working set of session data will stay resident on the heap (leaving more for your application). You no longer have to allocate precious heap space just to store replicated session data that may never be referenced. Additionally, since the heap is no longer the bottleneck, the idle/timeout period before a session is invalidated can be virtually unlimited.
- DSO is not based on java serialization, thus the problems with object identity, 3rd party libraries, etc are likewise not present with objects placed into session. (see: Object Identity and Tradition).
So all this sounds great, how do I go about using Terracotta underneath my application? Good question! In a nutshell, there are 4 high level steps required:
- Modify your Weblogic startup script to introduce Terracotta’s libraries and class loader. This step enables generic DSO functionality in the app server and is only currently supported under the Sun java runtime
- Modify your weblogic.xml descriptor to indicate your preference for Terracotta backed sessions
-
Indicate the set of classes that comprise your session attributes in the Terracotta client configuration file (
L1Config.xml), as well as enable DSO for your particular application(s) - Install and startup a Terracotta server instance
Startup Script Modifications
The Terracotta software distribution includes a jar named tc-weblogic-boot.jar present underneath the “lib” directory. This jar must be present in the system CLASSPATH used to start the server. This jar may be placed anywhere in the CLASSPATH.
The following arguments must also be passed to the “java” invocation:
-Dtc.install=-Djava.system.class.loader=com.tc.weblogic.SystemClassLoader -Xbootclasspath/p: /lib/dso-boot.jar
Web App Descriptor Modifications
To enable Terracotta distributed Servlet Sessions, you must add/modify the PersistentStoreType configuration value in the web application deployment descriptor. For each application for which you want to enable Terracotta sessions, modify the descriptor file (weblogic.xml) to include a value for the PersistentStoreType setting. The following example illustrates a complete descriptor file:
<?xml version=”1.0” encoding=”ISO-8851-1”?>
<weblogic-web-app>
<session-descriptor>
<session-param>
<param-name>PersistentStoreType</param-name>
<param-value>terracotta</param-value>
</session-param>
</session-descriptor>
</weblogic-web-app>
A complete reference for the relevant portion of weblogic.xml file can be found here:
http://e-docs.bea.com/wls/docs81/webapp/weblogic_xml.html#1038173
Install and start Terracotta server
Simply run the tvs-start-L2[.bat] script that can be found under the “bin” directory of the Terracotta distribution. No other configuration is required.
L1Config.xml Setup
In general, only three portions of L1Config.xml need to be configured. The first is obvious; you’ll need to indicate the hostname/port of the Terracotta server.
Next you need to make sure your application is included in the <j2ee-applications> section. The value to include here is the unique name used when you deployed your application to the server. If you do not know the name of your application, use the WebLogic console and look under “Deployments”.
Finally, as with all DSO applications, the <included-classes> section needs to be configured. The basic rule here is that any class that you are going to place in your session needs to be included. Fortunately you can use a regular expression like syntax to make this process a lot simpler (thanks to our friends at AspectWerkz). For most applications, a few simple patterns that match package names in your application will do. Additionally, Terracotta provides (upon request) stock configuration sets for frameworks (e.g. Struts) that heavily utilize the session layer. Please refer to the ProductGuide.pdf included with the Terracotta software (under the “docs” directory) for a full overview of the <included-classes> configuration section.
Conclusion
The available solutions for session management within a clustered web application leave a lot to be desired. They impose too many constraints on developers, can be quite fragile, and introduce too much overhead. Terracotta’s solution is both simpler and more powerful at same time. To try out Terracotta’s session management feature for Weblogic, just head over to http://www.terracottatech.com and download the software.