Friday, September 24, 2010

WCF Service Throttling Behavior

WCF Service Throttling is a way for you to limit (“throttle”) the throughput of your service so that resources (memory, CPU, network, etc.) are kept at healthy levels.

While it is not a direct instance management technique, throttling enables you to restrain client connections and the load they place on your service. Throttling enables you to avoid maxing out your service and the underlying resources it allocates and uses. When throttling is engaged, if the settings you configure are exceeded, WCF will automatically place the pending callers in a queue and serve them out of the queue in order. If the client's call timeout expires while pending in the queue, the client will get a TimeoutException.

Using the behaviorConfiguration tag, you add to your service a custom behavior that sets throttled values.
<system.serviceModel>
   <services>
      <service name = "MyService" behaviorConfiguration = "ThrottledBehavior">
         ...
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name = "ThrottledBehavior">
            <serviceThrottling
               maxConcurrentCalls     = "12"
               maxConcurrentSessions  = "34"
               maxConcurrentInstances = "56"
            />
         </behavior>
      </serviceBehaviors>
   </behaviors>
</system.serviceModel>




 I have done following test on wcf service for service throttling in different scenarios:
1.        
If maxConcurrentCalls =2 then I made 3 calls to service it will execute only two calls at a time remaining call put into the queue execute after first two calls. See following program

<serviceThrottling
               maxConcurrentCalls="2"
               maxConcurrentSessions="34"
               maxConcurrentInstances="56"
            />


static void Main(string[] args)
        {
            System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(test1));
            th.Start();
            System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ThreadStart(test2));
            th1.Start();
            System.Threading.Thread th2 = new System.Threading.Thread(new System.Threading.ThreadStart(test3));
            th2.Start();

            Console.ReadLine();
        }
        static void test1()
        {
            ServiceReference1.Service1Client o = newConsoleApplication1.ServiceReference1.Service1Client();
           Console.WriteLine("Istance1  " + o.GetData(1));
       }
       static void test2()
        {
            ServiceReference1.Service1Client o = new ConsoleApplication1.ServiceReference1.Service1Client();
            Console.WriteLine("Istance2  " + o.GetData(2));
        }
      static void test3()
        {
            ServiceReference1.Service1Client o = new ConsoleApplication1.ServiceReference1.Service1Client();
            Console.WriteLine("Istance3  " + o.GetData(3));
        }

Result is:




 Each call is taking 5 second to complete.In this sample first two calls execute simaultiniiously and remaining one call executes after 5 seconds.

1. Iif maxConcurrentCalls =3 then I made 3 calls to service it will execute simultaneously
<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="34"
               maxConcurrentInstances="56"
            />
Result is:




3. If maxConcurrentCalls =3 and maxConcurrentSessions="2" then only two calls will be executed rest one call is discarded and any exception will not be raised

<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="2"
               maxConcurrentInstances="56"
            />
Result is:

 
4. If maxConcurrentInstances="2" then same result found
<serviceThrottling
               maxConcurrentCalls="3"
               maxConcurrentSessions="12"
               maxConcurrentInstances="2"
            />
Result is:



Note: Above result is found with default InstanceContextMode and ConcurrencyMode
That is [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Single)]

1 comment:

raman said...

like it giving good knowledge. thanks