Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load balancer and session management

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.

like image 250
Praveen Kumar Avatar asked Dec 06 '14 13:12

Praveen Kumar


People also ask

Does session work in load balancer?

Session stickiness, a.k.a., session persistence, is a process in which a load balancer creates an affinity between a client and a specific network server for the duration of a session, (i.e., the time a specific IP spends on a website).

What is load balancer and its use?

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications.

What is session affinity in a load balancer?

Session affinity, also known as “sticky sessions”, is the function of the load balancer that directs subsequent requests from each unique session to the same Dgraph in the load balancer pool.

Which load balancer supports sticky sessions?

To use sticky sessions, the client must support cookies. Application Load Balancers support both duration-based cookies and application-based cookies. Sticky sessions are enabled at the target group level.


2 Answers

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.

like image 91
KeatsKelleher Avatar answered Oct 04 '22 11:10

KeatsKelleher


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.

like image 25
ccuenca Avatar answered Oct 04 '22 11:10

ccuenca