Problem created by load balancing in ASP.NET
0 I have an ASP.NET 3.0 application that works fine on one server. It uses application variables to check if a user has checked out a file and locks it for all other users. When I put the application on the load balanced servers, the application did开发者_如何学编程nt work as expected since it multiple users got sent to multiple servers and each user could check out the requred file.
The main point is that, is there any way I can share the application variables in my application even though it is distributed on multiple servers. Or is there a better way to get a global variable?
EDIT: I am checking if a file is checked out by Application["FileLocked"] is true or false
Two things for you to consider:
In a load balanced environment, instance data within one server will not be available to another. While you can use Out-of-process session state, you can't easily make Cache items or static class data available across servers. A user's request will not always be routed to the same server (unless you use something like sticky sessions) - which can cause other problems.
You probably should use a more persistent mechanism (like a lock file) to determine whether a particular user is using a file. Otherwise, you may introduce race conditions in your system - and if a user's sessions ends without them unlocking the file you'll end up with orphaned locked files in your system.
How are you telling the system a file is checked out? I hope you are not using session variables. You should store the state in a database field and then check if the field (bool / bit) is checked / not checked. Whether the system is on multiple servers or one doesn't matter. The connection to the application will use the same connection string, hence the same data.
Application variables are stored within ASP.NET, which means that you'll have duplicate variables between servers.
As JonH suggested, store this in the database. That's where this type of information is supposed to go. Reasons:
- Eliminates load-balancing effects on session and application variables
- Maintains values even if the application resets. Normally, an ASP.NET app is unloaded (?) after 20 minutes of inactivity. With your scheme, even on a single server, this means that if someone checks out a file, goes home, and comes back the next day, it will no longer be checked out. Storing this information in a database eliminates that problem.
I think this is a task for a database. If you don't have one already, create a MySQL or other RDBMS instance somewhere and use that to share information like that. If you use the application memory to store that information, it will get lost whenever the app pool has to be recycled or the webserver is restarted.
精彩评论