DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Session and Clustered Java Web Apps

Session and Clustered Java Web Apps

Nick Watts user avatar by
Nick Watts
·
Jul. 26, 11 · Interview
Like (0)
Save
Tweet
Share
30.05K Views

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

http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-nd-mp&topic=cprs_best_practice

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

http://www.ibm.com/developerworks/java/library/j-jtp07294/index.html

this is an older article by brian goetz that contains similar knowledge to the previous link.

websphere application server v7 administration and configuration guide

http://www.redbooks.ibm.com/redbooks/pdfs/sg247615.pdf

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

http://www.redbooks.ibm.com/redbooks/pdfs/sg247497.pdf

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

http://www14.software.ibm.com/webapp/wsbroker/redirect?version=compass&product=was-nd-mp&topic=tprs_serializing_access

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.

from http://thewonggei.wordpress.com/2011/06/23/session-and-clustered-java-web-apps/

Session (web analytics) app Java (programming language) clustered Web apps application clustering Object (computer science) Application server

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Effective Jira Test Management
  • Low-Code and No-Code Are the Future of Work — For IT and Beyond
  • Testing Your Monitoring Configurations
  • Web Testing Tutorial: Comprehensive Guide With Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: