Tomcat – Having problems with maintaining sessions across multiple Amazon EC2 instances

amazon ec2memcachedsessiontomcat

One of my sites is hosted via Amazon EC2 service. The problem starts happening when the site faces a heavy load which in turn spawns a new EC2 instance via AutoScaling. This is good but the issue is when these instances are killed (due to lack of load) the session information is also lost, and hence forcing the user to re-login in to the site.

What is the best way to approach this problem?

I have looked in to Memcached Session Manager & it works fine for Sticky sessions but causing problems when I try to use Non-sticky sessions since I make a lot of parallel requests to my server.

My current server is Apache+Tomcat

Any pointers helpful. Thanks.

(cross post from webmasters.stackexchange)

Best Answer

If your autoscaling instances can be terminated at any time and you can't afford to lose session information, then you can't store session information on the autoscaling instances.

If you have a database (e.g., MySQL, RDS) you could store sessions there. This works ok for lower to medium volume web sites, but can break with higher volume.

You could run separate instance(s) with memcached that are not autoscaled.

You might also consider Amazon ElastiCache (just released):

http://aws.amazon.com/elasticache/

This is Memcached protocol compatible, so existing web server session plugin software should be able to work without much problem.