Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and Cons of Sticky Session / Session Affinity load blancing strategy?

People also ask

What is sticky session session affinity load balancing?

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).

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.

What are the advantages of using cookies persistent in load balancer?

Because load balancing may, by default, send users to unique servers each time they connect, this can mean that complicated or repeated requests are slowed down. Session persistence ensures that, at least for the duration of the session or amount of time, the client will reconnect with the same server.


Pros:

  • It's easy-- no app changes required.
  • Better utilizes local RAM caches (e.g. look up user profile once, cache it, and can re-use it on subsequent visits from same user)

Cons:

  • If the server goes down, session is lost. (note that this is a con of storing session info locally on the web server-- not of sticky sessions per se). if what's in the session is really important to the user (e.g. a draft email) or to the site (e.g. a shopping cart) then losing one of your servers can be very painful.
  • Depending on "sticky" implementation in your load balancer, may direct unequal load to some servers vs. others
  • ringing a new server online doesn't immediately give the new server lots of load-- if you have a dynamic load-balancing system to deal with spikes, stickiness may slow your ability to respond quickly to a spike. That said, this is somewhat of a corner case and really only applies to very large and sophisticated sites.
  • If you have relatively few users but a single user's traffic can swamp one server (e.g. complex pages with SSL, AJAX, dynamically-generated images, dynamic compression, etc.), then stickines may hurt end-user response time since you're not spreading a single user's load evenly across servers. If you have a lot of concurrent users, this is a non-issue since all your servers will be swamped!

But if you must use server-local session state, sticky sessions are definitely the way to go-- and even if you don't use server-local session state, stickiness has benefits when it comes to cache utilization (see above). Your load balancer should be able to look at HTTP cookies (not only IP address) to determine stickiness, since IP addresses can change during a single session (e.g. docking a laptop between a wired and wireless network).

Even better, don't use session state on the web server at all! If session state is very painful to lose (e.g. shopping carts), store it in a central database and clear out old sessions periodically. If session state is not critical (e.g. username/avatar URL), then stick it in a cookie-- just make sure you're not shoving too much data into the cookie.

Modern versions of Rails, by default, store session variables in a cookie for the reasons above. Other web frameworks may have a "store in cookie" and/or "store in DB" option.