Session and Clustered Java Web Apps
Join the DZone community and get the full member experience.Join For Free
session can be a headache to work with in java web applications. for that reason, most developers now use mvc frameworks, such as java server faces, that hide the use of session and allow you to work with simple java beans and configuration instead. but, that’s not always the case, especially where you have plain servlets that you have to maintain. with servlets, you have the power to put objects into and take objects out of session yourself. alone, this presents thread-safety issues , but manually managing session in an application that is clustered presents more problems.
there is a jump that has to be made when moving from a single server environment to a clustered one. in a clustered environment, session management becomes complex because it has to be shared by all the servers in a cluster. this sharing of session becomes a distribution of objects strategy , which is an advanced topic you should familiarize yourself with before clustering your application. a high-end application server (i.e. ibm websphere) will handle most of this complexity for you, but you have to educate yourself on the myriad configuration options and their affect on your application.
in my recent excursion into running an existing java web app in a clustered environment, i found that there was one major stumbling block from my programmer point-of-view—understanding the mechanism for distributing session objects between servers in a cluster. if you’re about to do the same for your application, the first thing to understand is that distribution of session objects is done through java object serialization. that method is prescribed in the servlet specification in section “srv.7.7.2 distributed environments”, in fact. how application servers actually manage that so session is consistent is left up to the vendors and is one way in which they can compete.
understanding that object serialization is the mechanism for sharing session is important to know. to start, it means that every single object that gets put into session has to do two things: 1.) it has to implement the class java.io.serializable and 2.) it has to actually be serializable. an object is not really serializable unless its entire object graph is also serializable. so think about this, if you didn’t design your application with this requirement in mind, you could be in serious trouble when moving to a cluster. at the very least, you will have to identify all objects you put into session in your servlets and modify them to implement java.io.serializable (which doesn’t require you to override any methods). at the worst, you will have to write custom serialization code for objects that don’t serialize naturally. the transient keyword can help with this by marking fields of objects that should be skipped during serialization.
the bright side to this discussion is that there is ample documentation out there on the topic, although it is spread out. below i have annotated a few sources i found very helpful when i started researching this topic. i am working with an ibm websphere cluster, so most of the documentation is from ibm, however, all of the advice applies to any application server. moving to a clustered environment is probably harder work than you realized, so you’ll be well served (and appreciated by your boss) if you check out these resources before making the jump to a clustered environment.
i’m still actively researching this topic myself. as such, i’ll keep updating this post as i learn more (read: i might be wrong on a detail or two ).
best practices for using http sessions
this is ibm’s advice on the best way to handle various aspects of session. it’s broadly applicable advice that, honestly, i wish i would have seen about 5 years ago when i first started working with web apps
java theory and practice: state replication in the web tier
this is an older article by brian goetz that contains similar knowledge to the previous link.
websphere application server v7 administration and configuration guide
overall, this book is very websphere specific. however, chapter 12 does describe how websphere handles distributed session and it’s very enlightening to read. on a guess, i’d say that other app servers do things similarly. there is actually a good bit of general discussion in chapter 12, much more than there is explanation of what buttons to click to set it up so it’s worth the time to skim.
designing and coding applications for performance and scalability in websphere application server
this book is far less specific to websphere than it sounds. chapter 3 “general coding considerations” is a fantastic overview of how to deal with topics such as garbage collection, synchronization and database access. most of the other chapters deal with issues that are similarly applicable to any application server, not just websphere.
the big bonus for downloading this redbook is that it has a section called “cluster considerations”. this section walks you through all of the things you should consider when you want to run your application on a cluster of servers.
serializing access to session
this link explains how to serialize access to objects in session for ibm websphere application server 7.0. i threw this link in to point out that session is basically a shared memory that multiple threads have access to concurrently. this is true whether you are running on a single server or a cluster of servers. this document is specific to websphere, but i’m guessing the other commercial app servers have similar options.
Opinions expressed by DZone contributors are their own.