Punter Vicky
Punter Vicky

Reputation: 17032

Clustered Environment and Session Management (Servlets)

I was reading a book on Java Servlets where I came across HTTPSessionActivationListener. It was specified that in a clustered environment , there can be only one HTTPSession object containing a specific session id. Assume there are 2 nodes A and B in a cluster -

first request goes to node A. Here a HTTPSession S1 is created along with session attributes and response goes back to the client. Same client sends the subsequent request. This request goes to node B. Now the session object S1 is moved from node A to node B (activated in Node B and passivated in node A).

In this case should the session object along with the attributes be serializable? What happens if it is not serializable?

In order to count the number of active sessions , should the sessions in both nodes be added up to get the actual value? How is this usually done?

Also I guess ServletContext is unique for each JVM. Are the attributes set as part of servletcontext copied to servlet context in all nodes of the cluster?

Upvotes: 4

Views: 5565

Answers (3)

Amit P
Amit P

Reputation: 544

Using a caching solution like Infinispan, hazelcast or redis would be the way to go if you want sessions to survive server failure. Application servers provide these function integrated now a days. You can just enable them from admin interface for web/ejb/jms persistance. If you are storing something into session in your code, you can use JCache API to store them on the underlying cache. JCache provides a product independent caching API, makes you code portable across caching solutions.

Upvotes: 0

yathirigan
yathirigan

Reputation: 6069

Spring session seems to be offering a capability called 'Clustered Sessions' and it also has feature to offload the session to a RedIs or GemFire caching solution.

Reference: http://projects.spring.io/spring-session/

Upvotes: 2

Kevin
Kevin

Reputation: 25269

Usually I've seen people use sticky sessions (provided usually by the load balancer, for example ec2 ELB has this feature: http://shlomoswidler.com/2010/04/elastic-load-balancing-with-sticky-sessions.html), OR the session data is stored in a shared repository, such as a database or NoSQL store.

Upvotes: 3

Related Questions