ASP.Net - Handling session data in a load balanced environment?

放肆的年华 提交于 2019-12-03 12:37:11

问题


How does ASP.Net deal with session data in a load balanced environment? Let's say a user makes multiple requests while navigating through a couple of pages? Do all requests go to the same server in the load balancer?

What can I do if they don't all point to the same server? how do I deal with my session data?


回答1:


You need to ensure that your NLB has the ability to enable "Sticky" sessions.

If this isn't an option, then you need to look at using the ASP.Net Session Modes to configure either a Session Server, or some backing store like SQL Server.




回答2:


In general most load balancers try to have persistent connections but for your purposes you can't guarantee it. You're best off either using something like an SQL backend to maintain state or create a separate class to manage your session data. In either case you can then rebuild the session data if it happens to be null when you expect it to be otherwise. I tend to use the getters to check for null and rebuild when necessary. In practice I haven't done any metrics to see how often it is rebuilt but I'd guess not very often. Better to be safe than sorry, as they say.




回答3:


Load balancers usually can be configured with something the infrastructure guys call sticky session or sticky bit. On this type of environment, once a request has been assigned to a server; it stays with that server throughout the session.

When Load Balancers are not configured in such a way, ASP.NET offers several session state providers. One very popular is the SQL Server State Provider, which allows session information to be stored in a database. In order for this to work, all objects placed in the Session object must be Serializable so that they can be stored in a table.

As I said, there are several more alternatives; for example, some people prefer to use a different approach such as memcached or similar products.




回答4:


You can use NLB from Windows or other products such as BIG-IP F5,HA Proxy, there tons of options.

Use Out-of-Proc (State Server) and make sure your machinekey settings are the same on all your servers. Also make sure you are doing IO on a shared disk drive such as a NAS.

Here is a link with some more tips: Load Balancing and ASP.NET



来源:https://stackoverflow.com/questions/7577677/asp-net-handling-session-data-in-a-load-balanced-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!