High CPU usage caused by using the same HttpClient instance
Published Mar 11 2019 06:41 PM 8,164 Views
Microsoft

High CPU usage is one of the most common issues that affects web servers. There are several root causes related to server hardware, website traffic or application code.

 

If you use the same instance of an object which is not thread-safe multiple times, this may increase the CPU usage. More specifically, using the same HttpClient instance from different threads increases the CPU usage.

 

The issue may surface in many ways. One if them is -obviously- high resource utilization. You can monitor the resource usage in Task Manager, Resource Monitor or Performance Monitor.

 

post15.png

 

The official documentation for HttpClient recommends using only one instance of this object (Source HttpClient Class). However, for the scenarios where requests step on each other, it’s better to create more than one instance.

 

Solution

 

Applications shouldn’t depend on one instance of HttpClient if the requests overlap each other. Instead of calling the same instance of HttpClient, using new instances of it should help IIS to reduce CPU usage.

4 Comments

What about HttpClientFactory? Since Core 2.1 it is the recommended approach, but how does it deal with instance count? 

Copper Contributor

Can you provide further details about what triggers the high CPU? Specifically multi-threaded code, or would the issue be triggered by standard async code? According to your chart, the issue starts with as few as 10 active requests. That's not particularly high .. the official guidance to reuse HttpClient seems questionable.

Copper Contributor

@Nedim, please provide sample code that reproduces the issue.

Microsoft

@michaelaird, unfortunately, I am not able to provide the source code. I figured out this issue by analyzing DebugDiag performance logs. There were multiple threads that are using the same instance of a function X which was using the HttpRequestHeaders that relies on HttpClient.

 

@Riccardo Chiodaroli, never tried HttpClientFactory. Thanks for the recommendation!

 

@RossF, I saw high CPU issues because of deadlocks caused by async functions. The issue I mentioned is caused by using a practically non-thread-safe instance in a multi-threaded way. The official guidance is expected to have some improvements

Version history
Last update:
‎Mar 11 2019 06:41 PM
Updated by: