Saturday, March 20, 2010

ASP.NET and Load balancing.

In one of our project the site usage of site was very heavy and we need to migrate it to load balancing server. I have never configured the sites in the load balancing server but it was quite interspersing experience Here are the some points which we need to take care while we move sites into the load balancing environments. So first we will see what is load balancing.

Following is a load balancing definition from the Google.

In computer networking, load balancing is a technique to distribute workload evenly across two or more computers, network links, CPUs, hard drives, or other resources, in order to get optimal resource utilization, maximize throughput, minimize response time, and avoid overload.

Following are the points which you need to take care when you are deploying your sites into load balancing server environments.

Machine Key Should be same for both servers: View state and session both are depends on the machine key. If you machine key is not same then you will have problems related to session and view state you may loose your session and view state in between request during post backs. If machine key will not be same then its possible that you can get strange result in Ajax requests. There is a machine key section in web.config where you can specify machine key.

<machineKey validationKey='C44B8B7C521CB5BC7E602BAE6118AA44CD690C7304817129DA27C17E800132A1BD946C6D9AD12F0A5B342840C7D130564195428160B7466146938CA9E3A62686'   decryptionKey='0E9DF2DA7F210B84087690FF0BF25C905182AD81E16A5FA9'   validation='SHA1'/>
Session Configuration: Any web application is not possible without having session as web pages are stateless so you can configure session in load balancing also. There are two configuration which can be used in load balancing environments. One is state service and another is Storing session in SQL Server.

Following is a good link to learn how you can configure sessions state in application.

And here is a good link to configure session on SQL Server.

You can configure session state mode in your web.config like following.

<sessionState mode="SQLServer" StateConnectionString="tcpip="

SqlConnectionString = "data source=SERVERNAME; user id=sa; password=sa"

cookieless="false" timeout="20" />

enableViewStateMac="false": This is a alternative approach to machine key. This will tell engine that whether it should check Machine authentication check or not and if you made it false then it will not check for machine authentication. You can define that in your web.config pages section like following.
<pages enableViewStateMac="false" />
Caspol Utility: You can use caspol utility to share some resources on between load balancing servers. Like we have file base cache dependency in our application so we have created a central location(A shared folder for cache files) for the both the server and used Caspol utility to give full trust share on that folder for load balancing servers. Following are some of the good links to which will teach how you can use CasPol utility to for application

File Replication:File Replication is also an important features of load balancing you should have replication enabled on the folders of web application so if you upload anything on one server it should replicated to other sites. Following is good link to understand file replication.

Sticky Sessions:
In some scenario Sticky session is very useful. In one page our application we have used extensive Ajax and we need that for each request and partial post back it should stay on one server till request completes.To achieve that we can used sticky session. Following are some good links to know about sticky sessions.

Hope this will help you on deploying your on load balancing sites. Following are some good links for understanding load balancing in more details

Shout it

kick it on


  1. Great article. may be yuo could help me....

    I am a newbie in web farms. In the current state we have only one webserver and one database. Application is in .net 3.5. Database is oracle 10g.
    I want to use a load balancer for webservers. Database will still be a single database (at least for the first release).
    Session and cache will not be shared, among the web servers(at least for this release).
    In other words, the most simplist case, assume we have 3 web servers, single database for all these and a load balancer. If one web server is down, while user is logged in, user will get session timeout message, and will need to log-in again.
    For this scenario, do I need to change anything in the application (or just load balancer rules?). Any pointers or case studies related to this? Any specific places (webservice calls, session state (in-proc), machine key etc), in the code I need to modify?
    Thanks in advance.

  2. @Sean Perry - I think you can use to store session state in database. Search it for google and then you can use that in database.

    Another options is to use appfabric session state provider. Which is also good options.

  3. Thanks. I will certainly consider appfabric for the next release. For the first release, I only wanted to create a simple web farm, with no session or cache sharing among the servers.
    Here's a scenairo, I need your hep with :
    User logs in, load balancer takes user to webserver1, since session and cache are not shared by the web farm, how will I make sure, user always lands on web server1 for all the post backs?
    Thanks again...

  4. Hello Sir, This is Sujeet From India I have develop a online exam web application in with VB 2005, ms sql 2005.. My issue is that i am using 40 to 50 users and its run perfect but when i increase no of users my Server IP is not responding. I dont able to understood load balancing.. If you can help me then its great for me.. Thanks Sujeet 
    [email protected]


Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews