11 tips to improve WCF RESTful Services performance

Sheonarayan
Posted by in WCF category on for Advance level | Points: 250 | Views : 1922 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article, we are going to learn most useful 11 tips to improve the performance of WCF RESTful services.
Recommendation
Read Creating Custom Tag Helper in ASP.NET Core before this article.

Introduction

In the world of gadgets and inter connectivity, Services/APIs plays major role. The most easy and scalable way of transmitting data from one place to another place is using RESTful (REpresentational State Transfer) services.

RESTful services can also be used to modify the data apart from just getting the data. Like any other services, a RESTful services consists of Client and Server. Client makes a HTTP request to the server using URI (URL) and server responds.

Basically, there are four types (Verbs) used while using RESTful services depending on what action to perform
  1. GET - to receive the data
  2. POST - to add/insert data
  3. PUT - to modify the data
  4. DELETE - to delete the data
Developing the RESTful services is as simple as creating any WCF service and specifying HTTP binding in the web.config file. Let's skip this part as this is out of scope of this article.

WCF RESTful services Performance tips

After developing a service project and basic testing, it is important to optimize the performance of the service if you are expecting huge volume. Below are most of the configuration that should be enough to optimize the performance of the service that requires most throughput.

1. Correct URL settings at client side
RESTful services has a thing that it's URL must ends with the "/" (forward slash). So if your client is sending a request, that request URL must ends with "/".

For example, if your url is http://something.com/myservice, it should be called as http://something.com/myservice/

In case your client doesn't consider this, their request does 307 redirect first and then reaches to the real service URL. So at server side, some resources will be always wasted in 307 redirect. This does impact the performance of the service.
2. Specifying ServiceBehavior attribute
To get maximum throughput from a service, your ServiceBehavior of the service class should be changed to below.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
         ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyRestService : IMyRestService 
{ }
  • InstanceContextModel.PerCall - creates the instance of the service at every request
  • ConcurrencyMode.Multiple - each thread will serve a separate request
3. Service throttling settings in web.config file
Service throttling settings helps us to limit the maximum number concurrent call, sessions and instances creation of this service.

<configuration>  
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceThrottling maxConcurrentCalls="16" maxConcurrentSessions="100" maxConcurrentInstances="320" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
The recommended settings of these values are
  • MaxConcurrentSessions - 100 * Processor Count
  • MaxConcurrentCalls - 16 * Processor Count
  • MaxConcurrentInstances - int.MaxValue if not specified, otherwise 116 * Processor Count
4. Specifying Pool in the database connection string
If your service is using Database to insert the data received from client, ensure that your database connection string is using Pooling settings.

Data Source=111.111.11.11;Initial Catalog=dotnetfunda;User ID=snarayan; Password=something;Min Pool Size=5;Max Pool Size=300;Pooling=true;"
Here we have specified 
  • Min Pool Size - minimum number of connection to open for the pool
  • Max Pool Size - maximum number of connection a pool can contain
  • Pooling true - (optional)
5. connectionManagement settings in web.config file
In case your service is internally making any HTTP request to third party services, you should also set connectionManagement settings in web.config file.

<configuration>  
  <system.net>  
    <connectionManagement>  
	<add address="*" maxconnection="51" />  
    </connectionManagement>  
  </system.net>  
</configuration>  
The maxconnection count is 2 by default. Here we have set to 201.
6. Remove unnecessary modules for the service in the web.config file
By default ASP.NET application request pipeline has many modules in the pipeline that gets called when you hit the IIS. We should remove those that are not used in the service.

<system.webServer>
    <modules>
<!--Remove below modules as they are not needed in WCF, this will improve the performance a bit-->
        <remove name="OutputCache" />
        <remove name="Session" />
        <remove name="WindowsAuthentication" />
        <remove name="FormsAuthentication" />
        <remove name="PassportAuthentication" />
        <remove name="RoleManager" />
        <remove name="UrlAuthorization" />
        <remove name="FileAuthorization" />
        <remove name="AnonymousIdentification" />
        <remove name="Profile" />
</modules>

Here we are instructing the IIS to remove these modules from the application request pipeline so that we can save time to load these module that helps in increasing the performance of the service.

7. processModel settings change in machine.config file
Locate your machine.config file and modify below settings. The ideal location of the web.config file is C:\Windows\Microsoft.NET\Framework64\<version>\Config\Machine.config

<processModel autoConfig="false" minIoThreads="201" minWorkerThreads="201" 
                       maxIoThreads="401" maxWorkerThreads="401" />
Set above settings depending on the your server configurations

  • maxWorkerThreads - maximum number of worker threads available in the thread pool (default is 20 per CPU, recommended is 100)
  • minWorkerThreads - minimum number of worker threads available in the thread pool (default 1 per CPU, recommended is half of maxWorkerThreads value)
  • maxIOThreads - maximum number of threads that can be allotted to perform input/output (I/O) operations (default is 20 per CPU, recommended 100).
  • minIOThreads - minimum number of threads that can be allotted to to perform input/output (I/O) operations (default 1 per CPU, recommended is half of maxIOThreads value)
8. applicationPool settings in aspnet.config file

We can also set the applicationPool settings from the asp.net file that can be located at C:\Windows\Microsoft.NET\Framework64\<version>/aspnet.config

   <system.web>
       <applicationPool 
        maxConcurrentRequestsPerCPU="5000"
        maxConcurrentThreadsPerCPU="0" 
        requestQueueLimit="5000" />
  </system.web>

Here we set 
  • maxConcurrentRequestsPerCPU - max concurrent request per CPU
  • maxConcurrentThreadsPerCPU, 0 means unlimited
  • requestQueueLimit - number of request application pool queue can contain
9. Setting Request Queue Timeout in IIS
Setting Request Queue Timeout can also help. To set these settings, please go to IIS > Website > ASP > Limits Properties = Request Queue time-out - it can be changed to 00:00:05. This is required only when you think that you are getting too many request at a time.

10. Setting the Threads Per Processor Limit in IIS
The default value of Threads Per Processor Limit is 25. It can go up to 100 depending on processor speed.

To set this, Open Information Services (IIS) Manager. In the Connections pane, select the web server, click to select Features View, and then double-click the ASP feature. Expand Limits Properties under Behavior, click Threads Per Processor Limit. Do not forget to apply the changes.

11. Increase Maximum Worker Processes in IIS application pool
Go to IIS, open application pool attached to the service and locate Maximum Worker Processes. Increase the count. Default is 1, we can make it 4 or 5 depending on server configurations. This is also called "Web Garden" in IIS terminology.

Important
It is always important to write optimized code following best practices. Above settings values can be tweaked to see what settings are working well on a specific service configurations.

Conclusion
Hope above things will help coming up with the most salable WCF RESTful services. 

Thanks for reading, do let me know your if you come across any other settings that might have helped increasing the performance of the WCF RESTful services or APIs.

Do let me know your comments or feedback.

Reference

MSDN 

Recommendation
Read 6 Steps to host ASP.NET MVC/Core application on server after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Sheonarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)