Showing posts with label WCF 4.0. Show all posts
Showing posts with label WCF 4.0. Show all posts

Wednesday, May 18, 2011

How to add a ServiceThrottlingBehavior to a WCF Service?

When working with WCF especially when middle-tier client applications uses Windows Communication Foundation, you should always think about performance and take some major design decisions and tuning parameters.

By adding ServiceThrottlingbehavior in web.config we can achieve high performance using WCF. Below is the sample serivceThrottleconfiguration settings in web.config in .NET 4.0 Framework.

 <behaviors>
      <serviceBehaviors>
        <behavior name="CommonService">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceThrottling maxConcurrentCalls="16" 
                             maxConcurrentInstances="116"   
                             maxConcurrentSessions="100"   />
        </behavior>
      </serviceBehaviors>
    </behaviors>

The main purpose for the throttling settings can be classified into the following two aspects:


  1. Controlled resource usage: With the throttling of concurrent execution, the usage of resources such as memory or threads can be limited to a reasonable level so that the system works well without hitting reliability issues.

  2. Balanced performance load: Systems always work in a balanced way when the load is controlled. If there are too much concurrent execution happening, a lot of contention and bookkeeping would happen and thus it would hurt the performance of the system.

In WCF 4, the default values of these settings are revised so that people don’t have to change the defaults in most cases. Here are the main changes:


  • MaxConcurrentSessions: default is 100 * ProcessorCount

  • MaxConcurrentCalls: default is 16 * ProcessorCount

  • MaxConcurrentInstances: default is the total of the above two, which follows the same pattern as before.

“ProcessorCount” is used as multiplier for the settings. So on a 4-proc server, you would get the default of MaxConcurrentCalls as 16 * 4 = 64. Thus the consideration is that, when you write a WCF service and you use the default settings, the service can be deployed to any system from low-end one-proc server to high-end such as 24-way server without having to change the settings. So CPU uses count as the multiplier.

Please note, these changes are for the default settings only. If you explicitly set these settings in either configuration or in code, the system would use the settings that you provided. No “ProcessCount” multiplier would be applied.