Introduction
Today many serious Web-sites work using load balancing. In this case you have several instances of your site running on several machines (e.g. A, B, C). Clients do not send requests to these machines directly. Instead they send requests to load balancer (LB) - another machine. LB then sends these requests to machines A, B or C where your site runs. Thus we get separation of load on each single site, improvement of quality of work under heavy load.
Although load balancing gives us great advantages it also complicates development of sites to be used with it. Such sites can't store session state in memory, they should use distributed caches, they should generate HTML links pointing to load balancer (not to current machine), etc. All these things must be carefully tested if we want our site to work with load balancer.
But how can we do it in our development environment? How can we also debug issues connected with load balancing? Usually enterprise-level load balancer is rather complex and expensive piece of software and hardware. It is definetely can't be installed for every developer machine. Big companies can install some testing environment with load balancer but usually it is not very convenient to use it for debugging. And what should small companies do?
Here I'd like to suggest two variants that can help you to look how your site works with load balancing.
Variant 1
You may use IIS out-of-the-box capability to simulate load balancing.
- Open
IIS Manager
(Start - Control Panel - Administrative Tools - Internet Information Services (IIS) Manager). - In the left panel under
Sites
node find the site you want to test under load balancing.
- Select this site and in the right panel click on
Basic Settings...
- In this dialog you are interested in application pool:
- Now close the dialog, in the left panel of
IIS Manager
click on Application Pools
node and click on the pool in the list:
- In the right panel click on
Advanced Settings...
In the opened dialog you are interested in Maximum Worker Processes
setting in the Process Model
section. Set its value to 2
:
- That's it. Click Ok in the dialog and restart IIS.
Now when you contact your site from browser IIS will start 2 processes each of which will host separate copy of your site. On each request IIS will choose one process to send the request to. So we have some simple load balancing.
Be aware of some negative sides of this solution:
- As you have 2 processes now they can consume twice as much of resources of your machine.
- The most important drawback is that you can't decide which request will go to which instance of your site. Only IIS decides.
If you want to be able to decide which request should go to wich instance of your site there is another variant for you.
Variant 2
First of all you should create a copy of your site on IIS. It is easier than it may look like.
- Go to
"c:\windows\system32\inetsrv\config"
. You may need administrator rights to do it. - Open
applicationHost.config
file in any XML editor. - MAKE A BACKUP OF THIS FILE (just in case).
- Find element
<site name="name of your site" ...
(e.g. "<site name="LoadBalancing" ....) - Copy this element and paste it after the current element.
- For this new element change
'name'
and 'id'
attributes so that they don't overlap with existing sites. - Inside this new element find
<bindings>
elements and modify port of http binding so it doesn't overlap with existing sites. - Save the file and restart IIS.
If everything is Ok now you should have 2 copies of your site.
Now Fiddler comes to play.
- Install Fiddler (http://www.telerik.com/fiddler)
- In Visual Studio create a Class Library project.
- Add reference to Fiddler.exe to this project.
- Create the following class:
using System;
using Fiddler;
[assembly: RequiredVersion("2.2.8.6")]
namespace FiddlerLoadBalancer
{
public class Balancer : IAutoTamper
{
private string[] _hosts;
private Random _rnd;
public void OnLoad()
{
_rnd = new Random((int)DateTime.UtcNow.Ticks);
_hosts = new[]
{
"your-host-name:80",
"your-host-name:9092",
};
}
public void AutoTamperRequestBefore(Session oSession)
{
if (_hosts != null && (oSession.host == "your-host-name" || oSession.host == "your-host-name:80"))
{
oSession.host = _hosts[_rnd.Next(0, _hosts.Length)];
}
}
}
}
Here your-host-name
- name of your machine, 80 - port of initial site, 9092 - port of your copy of the site.
- Now build your project.
- Copy your dll into
<your-user-directory>\Documents\Fiddler2\Scripts
. - In IIS run your initial site and its copy.
- Start Fiddler.
- Open browser and connect to your site using address of your INITIAL site.
If everything is OK you will work with your site while your requests will be sent to two copies of your site at random. I had some troubles during warming up of sites. But then I was able to work with them.
You may modify Balancer
class and define any rules about where each request should go.
History
Revision | Date | Comment |
1.0 | 10.03.2015 | Initial revision |