Discussions

Web tier: servlets, JSP, Web frameworks: How to maintain session information across servers?

  1. Suppose we have more than one server serving the client requests. Say, for a particular client, first time the request goes to one server, but next time the request goes to another server. In such a scenario, how can we maintain the session info created in the first request to be taken to all the other servers as well......

    To put simply, how can the session info be shared across the servers when there are more than 1 server to serve the requests?
  2. Session information is server related, there's no way to do it in an easy way. The only option is communicate the session information from one server to the other by the GET or POST methods, replicating session information
  3. Prashant,

        The answer to your question depends on what application server you are using. Some of the big name vendors provide "session affinity" as a feature. If you are using such an appserver then I'd suspect that it is a matter of setup and configuration.

        If that is not the case, you are faced with the scalability issues surounding "sticky sessions".

        I recently came across an article that talks about rolling your own session management technique. Perhaps this can help you. Please be adviced that the article uses the file system (text files?) to persist sessions. This may not be efficient in my opinion, perhaps the approach can be modified to use an RDMS to persist the sessions.

    http://www.oreillynet.com/pub/a/onjava/2001/03/01/pseudo_sessions.html
  4. Prashant,

      Please take a look at the following threads as well:

    http://www2.theserverside.com/discussion/thread.jsp?thread_id=3422
    http://www2.theserverside.com/discussion/thread.jsp?thread_id=5529

    Hope this helps.

    Regards

    Raj
  5. Some of the appservers save sessions to database. I think
    if the appserver supports Servlet clustering and if your
    severs are setup to support then there is no action
    required from the programmer.
  6. The only interesting thing that arises, if the container is doing the hard work, is how you prevent two frames (which go to two different servers, for the sake of argument) from running one piece of code which you want to ensure single threaded access to.

    In the one server world, you can sync on the HttpSession, or something in it.

    You can't do this in a multi-server world.

    Maybe it's a contrived situation.