20

There is a website run by 2 servers. These 2 servers balances the load using Load Balancer. So, if 1 session is created on 1 server and say load is shift to another server immediately, then how session is maintained?

I am just interested in the concept at a very high abstract level and not real implementation details.

Dexter
  • 4,036
  • 3
  • 47
  • 55
Praveen Kumar
  • 1,624
  • 5
  • 18
  • 21
  • very broad question. – Pleun Dec 06 '14 at 13:45
  • possible duplicate of [What are the different approaches for Java EE session replication?](http://stackoverflow.com/questions/4997715/what-are-the-different-approaches-for-java-ee-session-replication) – Joe Dec 06 '14 at 13:47
  • Using [JWT](https://en.wikipedia.org/wiki/JSON_Web_Token) world be a perfect solution for this problem – Vivek Jan 21 '20 at 07:18

3 Answers3

16

This is where the concept of "Sticky Sessions" or "Session Affinity" comes into play.

http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html

You can configure many load balancers to route user sessions to the same machine.

Another approach would be to maintain session information in a datastore accessible by both machines behind the load balancer. If the session exists in your database (or even redis/memcached) it's just a primary key query (or get) away from being consistent on both machines.

Self promotion: I actually started a project to implement something like a session at Buzzfeed. http://github.com/buzzfeed/phonon

EDIT: updated link to personal project.

KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52
2

This situation depends of the technology, in my experience with .Net there are two concepts called StateServer or SQLServer which are the recommendations to get the session information out of the execution process in the servers, so that's the idea, isolate the sessions in a different server or process, you can read a little bit here:

State Server modes in .Net

Load Balancing in .Net

So additionally read this Read to get the difference between Load Balancing and Clusters, for some platforms is better to select the best option.

Dexter
  • 4,036
  • 3
  • 47
  • 55
ccuenca
  • 63
  • 1
  • 6
2

There are many approaches used for this work but mainly there are two approaches followed. So these approaches are as follows

  1. There is a concept called sticky sessions. So actually what happens in this concept is that when the first-time user accesses the website the load balancer directs his or her request to one server on the backend and then keeps the record with it that to which backend server it has forwarded the request of this particular IP address or user. Now, whenever the load balancer gets a request from this IP. It always forwards this request to the same server no matter how many servers are present behind. So this helps to maintain user sessions as the request of this particular user is always handled by the same server on the backend but then, as usual, there are some pros and cons of this approach. The following image explains it very well

enter image description here

  1. Now the second approach is that you add one more server to your setup which contains information about sessions, So every time there is a request this request is first consulted with this server keeping a record of all sessions, and then the request is forwarded by LB to any server with some additional data. and even the backend server wants to check some history of data os this new user, It will also consult this extra server in setup containing information about sessions of users but this approach is a bit expensive.
Umair
  • 585
  • 3
  • 9
  • 21