Throttling In WCF: Concurrent Call - Part One

I have chosen a very confusing topic again, at least for me. So, let’s get into this and try to come to some conclusion.

Throttling

When we have achieved an intermediate level in coding, we need to work towards betterment of it, especially when we work in Client-Server environment. Let’s say, I did ample things in the code for a performance boost but I'm still not getting the output that I am looking for. For the same, we need to be aware of a few configurations or settings in the WCF config file for getting more prompts and good results.

I am assuming that readers are aware of “ABC” in WCF and have already developed a few. Without that, it won’t be easy to understand the concept of Throttling.

Throttling is a mix and match of Concurrent Calls, Concurrent Instances, and Concurrent Sessions. In WCF, it’s used to limit the Calls, Instances, and Sessions to increase the throughput. You can leverage throttling to optimize and control things. Throttling can be achieved through configuration in Web.Config file, or programmatically as well.
Throttling
Let’s start with concurrency

Concurrency:
You might have heard about readers and writers problems where one thread cab can be used only for one purpose. Let's say, I have a file and I want to read from it. One more thread is there and it wants to update the file. This is called a concurrency issue. WCF service can handle one request at a time. So, maxconcurrentcall will help WCF to handle multiple requests at a single point of time.

  1. Single
    One request (Thread) will get handled at a given point of time. In a case when WCF gets another request at the same time, it will wait for the first request to get complete. Let's check out the code for the same:

    code

  2. Multiple Concurrency Mode
    Provides permission to multiple threads for accessing a service operation at the same time. Things can be movable if you have managed multiple threaded environments well. Threads can call an operation at any time. It is our responsibility to play with our objects with locks.

    code

  3. Reentrant Concurrency Mode
    Services, configured for Reentrant concurrency mode, behave similarly to Single mode. In that concurrent calls are not supported from clients; however, if an outgoing call is made to a downstream service or to a client callback, the lock on the service instance is released, so that another call is allowed to acquire it.

    code

Configuration in Web.Config file

To increase the throughput of the service, multiple concurrent calls must be allowed to process. So, we have to set the limits of total number of calls that can currently be in progress across all the service instances. For the same, we can use maxconcurrentcall feature in Web.Config file.

 Attribute  Description
 maxConcurrentCalls  The default is 16.

MaxConcurrentCall

The throttle for MaxConcurrentCalls affects the number of concurrent request threads, the service can process to any of its exposed endpoints.

How to set Maxconcurrentcall in Web.Config file

code

Code

  1. <behaviors>  
  2.     <serviceBehaviors>  
  3.         <behavior name="MyThrottling">  
  4.             <serviceThrottling maxConcurrentCalls="100" />  
  5.             <!-- To avoid disclosing metadata information, set the values below to false before deployment -->  
  6.             <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />  
  7.             <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->  
  8.             <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>  
  9.     </serviceBehaviors>  
  10. </behaviors>  
Conclusion

In this article, we learned about different types of concurrency and also, how to implement the same in Web.Config file. You can set the same in code, as well. But, it’s always recommended to set the same in config file, else you need to compile the code and deploy it each time.

In the next article, we will learn about other two features; i.e., concurrent session and instance.

Next Recommended Readings