简体   繁体   English

即时贴:好还是坏?

[英]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. 当一个相同的请求发送到两个负载平衡服务器时,我有一个Web应用程序似乎处于间歇性竞争状态。 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. 我现在正在使用他们的负载平衡器托管在Amazon中。

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. 否则,我将假设您正在发出2个快速请求,并且它们同时击中了两个负载平衡服务器(就像轮询),您的事务未在第二个请求到达服务器之前完成,并且您认为粘性会话可以解决此问题。

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. 对于后一种情况,假定您的会话持续一定的时间间隔,例如1/2小时,如果您有N个新用户来到站点,则最初他们将在两台服务器之间平均分配。 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. 如果在1/2小时之前服务器1的所有用户都离开并且另外M个用户进入,那么服务器2上的负载将增加,该服务器2上具有原始N / 2个用户和新的M / 2个用户,而服务器1上只有M / 2个用户,即您将浪费容量,并且需要进行代码修复。

There are times when sticky sessions may be useful, but unless you have a good reason to use them I would avoid them 有时候粘性会话可能有用,但是除非您有充分的理由使用它们,否则我将避免使用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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