web-servicesweb-applicationswebserver

How does a distributed web server maintain clients state


To maintain user's state, a web server creates a Session object and assigns it to a unique session_id. This makes sense in a single server scenario. But, what happens in a distributed server architecture, where each request can hit different server. How is this Session object shared between different web servers? One way I can think of is to store it in a database, but that would be too inefficient, since each read would incur extra latency.

I want to know what are some best practices being employed to solve this problem?


Solution

  • This very much depends on your application architecture and what sort of session information you wish to share.

    If you only wish to share authentication token that can very much be done by URL rewriting or Cookies.

    If you wish to store larger objects then you will have to look at your load balanced architecture (cluster) and make a decision. Ofcourse I can not explan everything here as they are vast topics but I can give you a few hints from my experience so you can research further:

    1. Sticky session: With this approach client get stuck with one of the load balanced servers and therfore always servered by that server only. Which means you manage all the sessions etc as you would do on a single node server.
    2. Cluster topology: Look at your nodes cluster and see what topologies it supports and whether or not it can share data among eah other. I have previously configured: Ring and Star. See this article for more details.
    3. Database: This is one of the easiest and classic way of sharing objects in load balanced enviornments.
    4. File System: Very much like Database, one can serialise objects and retrieve it later using centralised file system e.g. NFS, AFS.