Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage session variables in a web cluster?

Session variables are normally keept in the web server RAM memory.

In a cluster, each request made by a client can be handled by a different cluster node. right?!

So, in this case...

  • What happens with session variables? Aren't they stored in the nodes RAM memory?
  • How the other nodes will handled my request correctly if it doesn't have my session variables, or at least all of it?
  • This issue is treated by the web server (Apache, IIS) or by the language runtime (PHP, ASP.NET, Ruby, JSP)?

EDIT: Is there some solution for Classic ASP?

like image 886
Daniel Silveira Avatar asked Oct 22 '08 00:10

Daniel Silveira


3 Answers

To extend @yogman's answer.

Memcached is pure awesomeness! It's a high performance and distributed object cache.

And even though I mentioned distributed it's basically as simple as starting one instance on one of your spare/idle servers, you configure it as in ip, port and how much ram to use and you're done.

memcached -d -u www -m 2048 -l 10.0.0.8 -p 11211

(Runs memcached in daemon mode, as user www, 2048 MB (2 GB) of RAM on IP 10.0.0.8 with port 11211.)

From then on, you ask memcached for data and if the data is not yet cached you pull it from the original source and store it in memcached. I'm sure you are familiar with cache basics.

In a cluster environment you can link up your memcached's into a cluster and replicate the cache across your nodes. Memcached runs on Linux, Unix and Windows, start it anywhere you have spare RAM and start using your resources.

APIs for memcached should be generally available. I'm saying should because I only know of Perl, Java and PHP. But I am sure that e.g. in Python people have means to leverage it as well. There is a memcached wiki, in case you need pointers, or let me know in the comments if I was raving too much. ;)

like image 113
Till Avatar answered Nov 02 '22 11:11

Till


There are 3 ways to store session state in ASP.NET. The first is in process, where the variables are stored in memory. The second is to use a session state service by putting the following in your web.config file:

<sessionState
    mode="StateServer"
    stateConnectionString="tcpip=127.0.0.1:42424"
    sqlConnectionString="data source=127.0.0.1;user id=sa;password="
    cookieless="false"
    timeout="20" />

As you can see in the stateConnectionString attribute, the session state service can be located on a different computer.

The third option is to use a centralized SQL database. To do that, you put the following in your web.config:

<sessionState
    mode="SQLServer"
    stateConnectionString="tcpip=127.0.0.1:42424"
    sqlConnectionString=
     "data source=SERVERHAME;user id=sa;password="
    cookieless="false"
    timeout="20"
/>

More details on all of these options are written up here: http://www.ondotnet.com/pub/a/dotnet/2003/03/24/sessionstate.html

like image 40
ine Avatar answered Nov 02 '22 11:11

ine


Get a Linux machine and set up http://www.danga.com/memcached . Its speed is unbeatable compared to other approaches. (for example, cookies, form hidden variables, databases)

like image 21
yogman Avatar answered Nov 02 '22 10:11

yogman