One of the main features of PaaS services like Azure Web Apps is its ability to run and deliver great performance without any external configuration. Everything is managed out of the box for you by Microsoft Azure and you just reap the benefits of this exceptional service offering.
However, there may be scenarios when you are not getting the desired behavior from your application in terms of performance even after doing all the regular tweaks. If you are convinced that there is nothing left to optimize within the application, but your resources are still underutilized like CPU/Memory, you can increase the minimum thread count available for your application. There are two types of threads available to your web application:
- Worker Thread – These threads are used for Processing your requests or performing background operations.
- I/O Thread – These threads are used whenever you are performing any I/O task like reading from external service or network.
By default, the minimum thread count for these two types of threads in an IIS application is set to the number of processors available in the machine. Whenever any new request comes in, these threads are used to serve the request if available, otherwise, a new thread is spun up based on the availability of system resources. The algorithm to create new threads might involve some delay which can extend up to 500ms. To avoid that, we can upfront set the minimum thread count for our application so that this overhead can be avoided. But make sure you don’t keep it too high which may cause further issues. The ideal number will depend upon the system configuration but you can start with a smaller number, say 50-100 and proceed gradually.
One way to set the minimum thread count is via ThreadPool class. In your application start code, add following line:
int min_worker = min_IO = 50;
You can read more about these settings in this excellent article by Jon Cole.