Monday, August 19, 2013

Server Side Sessions vs Client Cookie Sessions

If we were to strictly follow the REST model, we should not store client state on the server, including sessions.  Since I am starting a new project, I thought I would take a moment to evaluate the pros and cons of server side sessions vs client cookie sessions.

1) Scalability
One the major arguments against server side sessions is that it does not scale.  Using the traditional Java model, session are stored in memory.  Thus, with lots of users and large sessions, one can run out of  memory.    On the other hand, in the PHP world, server side sessions are typically stored in a  file. While this will not run into a memory problem, this will require frequent disk read and writes at each request.    Both of these server side session models (Java and PHP) are thought to not scale well.  Lets take a closer look...

How much memory does a Java session take?  

This is a crucial question.  If Java sessions take lots of memory, then we know this will not scale.  However, as it turns out a Java session takes very little memory, in fact on my machine, a single session only takes 1.67 KB (of course filled with more data, that number will be higher).  That means that we can store sessions for 10K users in a mere 16.3 MB.  If we keep the information in our sessions small, memory is really not too much of an issue.  

But what if we have lots of sessions or larger sessions? 

To solve this problem, Tomcat has a configuration that allows a hybrid appraoch between memory sessions and disk based sessions.  Using this model, sessions are stored in memory, but then serialized to disk after a configurable timeout.  If needed again, the sessions are brought back into memory. This option provides rather good scalability.  The sessions  are released from memory after a period of inactivity, and do not need to be read from disk each time. Using this model, I do not see most web apps suffering from a scalability issue stemming from high memory or frequent disk access.  

2) Security
Using server side sessions, all session data is stored on the server side.  While it is possible to secure sessions in the cookie, it is harder to get right.  Having the session information on server makes it harder for users to manually change data.

3) Performance
Server side sessions are simply faster: not only is there less network traffic, there is also less server side computation.   However, if one were to place session in the cookie,  extra server side computation is required to de-serialize the session, validate the session, and then re-serialize back into the cookie.  Granted, this computation could be considered negligible, it is worth pointing out that expensive cookie encryption could create overhead.

4) Flexibility
Using client cookie sessions, you have strict limitations: only 4K of string data.  In most situations, this will work fine.  In fact, if one is putting more than 4K in a session, they are likely abusing their sessions (which is all to common in many Java frameworks).  However, occasionally you do need to add more than 4K of data, and at times it is convenient to store non string data.  Server side sessions provide the flexibility to do so.

5) Load Balancing
Perhaps the greatest feature of client cookie sessions is that load balancing is a breeze.  Since no state is stored on the server, it does not matter which server handles the request.  

On the other hand, using server side sessions, once a session is created the same server must handle each subsequent request.  However, in most situations, this is really a non issue since every load balancer supports a mechanism to forward requests in a sticky manner, either via an ip table or cookie.  Things do get more tricky when you need to take a server off line, as you might need to "drain" a server of its sessions.  Yes, this is annoying, but should only need to occur occasionally.  (and this annoyance will be only be one small component  within a larger and more complex process of updating apps on the fly, a complication which exists regardless of the type of session).

Conclusion
In this post we briefly looked at some of the pros and cons of storing session data on the server or in client cookies.  While cookies do provide some excellent benefits, particularly in load balancing, I think that server session will still work fine in most types of apps.  To each their own!

For my current project, I am going to continue using the tomcat's hybrid memory-disk option.  It provides a scalable, flexible, and secure solution for sever side sessions.