简体   繁体   中英

Sticky Sessions: Good or Bad?

I have a web application that seems to be having intermittent race conditions when an identical request is sent to two of my load balanced servers. Obviously neither has completed the transaction at this point so both actions on each server is valid.

Would sticky sessions fix this issue? Is the use of sticky sessions frowned upon? Also what might be some other solutions?

I'm hosting right now in Amazon using their load balancer.

If a single request is sent to load balanced set of servers only one of the servers should get the request, typically allocated via round robin. If you are issuing a single request and it hits both your servers something else is wrong.

Otherwise I will assume that you are issuing 2 rapid request and they hit both of your load balanced servers (as round robin would), that your transaction does not complete before the second request hits the server and that you believe sticky sessions would solve this issue.

A sticky session would send all requests in this session to the same server. In your example both requests would now hit the same server and if you did nothing else, the transaction for the first request would not have been committed before the second request started, so you would get the same result ie sticky sessions alone would not help.

If the transaction were something like placing an order then you could craft your code so that upon successful commit the contents of the cart were deleted. The first request to complete would delete the cart, the second request would fail and you could message the user that the order had already been placed.

Sticky sessions can make it more complicated to have high availability and scalability. For the former, consider the case where one server goes down - all sessions on that server will also go down and you will have to write code to fail them over to the other server.

For the latter case, assuming your sessions last some interval eg 1/2 hour, if you have a N new users come to the site they will initially be evenly divided between both of your servers. If before 1/2 hour all of the users from server 1 leave and another M users come in, then you will have more load on server 2 which has original N/2 user plus new M/2 users while serer 1 only has M/2 users ie you will have wasted capacity and will need to code to fix.

There are times when sticky sessions may be useful, but unless you have a good reason to use them I would avoid them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM