WCF Load Balancing : An End To End Example For NetTcpBinding

Recently, I worked on prototyping WCF load balancing for our product, FirstPoint. I built a simple example to test the configuration and behavior of load balancing. Since there aren’t many end-to-end examples of WCF load balancing on the web, I’m hoping this will be useful (since the Microsoft documentation on this is basically non-existent…well, for NetTcpBindings at least).

I will assume that you are already familiar with network load balancing and configuring the NLB on Windows Server 2003.

Here’s a screenshot of how I’ve configured my network load balancer:

You’ll notice that I’ve configured a specific port range for my service. Also, take note of the cluster address: 192.168.1.220. The two servers that make up the cluster are FPDEV1 (192.168.1.222) and FPDEV2 (192.168.1.223).

I’ve defined a simple service interface which simply echoes the message with a server name:

And here is the implementation:

As you can see, I’ve simply prepended the machine name to each response so that we can track which server we are connecting to. Here is the main program which hosts the service:

This should be deployed on your each of your two (or more) servers in your cluster. The magic comes next in the configuration file:

The most important part of this configuration file is the setting for the lease timeout. The SDK documentation references this setting, but does not go into detail on how it’s configured. Note that it seems that you can only set the least timeout value in configuration using a custom binding.

There is one particularly important thing to note here: I’ve set the timeout to 1 second for demonstration purposes only. The SDK documentation mentions that this value should be set to 30 seconds. As we’ll see later, I have the client creating a new connection every 2.5 seconds (on purpose so we can test connectivity). What I found was that with the default setting (5 minutes, according to the SDK), if you initially connect to server A and server A goes down, it will not automatically connect to server B. I assume this is because of the lease timeout value (it throws an exception).

Also note that the endpoint is configured using the IP address of the node balancing cluster and not the individual servers.

The client side of it is pretty straight forward as well:

The only thing to note is that I used a delegate to spin off a second thread to make the call to the server. Adding a reference to the server generates the following configuration file:

Note that on this side, the binding is generated as a netTcpBinding.

And that’s it! Deploy the service to your clustered servers and start the services and then start the client. If your cluster has a bias towards one server, you will see that the messages will come from that server. To test that it actually is load balancing, simply stop the service on that server and you should see that the output will be coming from the other server. Here’s an example:

You can see that after I stop the service on FPDEV1 at the third echo message, it flips over to FPDEV2! Awesome!

Here’s the full project (VS2008): WcfLoadBalancingSample.zip (16.89 KB)

You may also like...

3 Responses

  1. Ingi says:

    I’m looking into load balancing on nettcpbinding, so this article helps, but I have a question.

    Let say the lease timeout is 10 seconds, and in those 10 seconds 10.000 customers come in, won’t they all go to the same server? If so, then how is that load balancing? Maybe I’m misunderstanding this.

  2. Chuck says:

    Ingi,

    Your request is intercepted by the load balancer first. The issue with the lease timeout is that it creates a server "bias".

    The load balancer will redirect the new incoming requests to the server with the least load.

    – Chuck

  3. Richard says:

    Thanks for the example – but I have with the same configuration but I have to use HTTP. Problems? Also, as in IIS with SessionState that can be stored to a SQL server, does it work the same for WCF. Can you have perSession, and the associated state, stored to a SQL server? Or am I missing something – as the client would recreate the proxies when the web page returns.