Search code examples
.netasp.net-coredotnet-httpclient

Which is correct way of using MaxConnectionsPerServer in HttpClientHandler?


I'm trying to find the correct value for HttpClientHandler.MaxConnectionsPerServer.

My services conditions:

  • I use ASP.NET Core 3.1 - 6.0
  • I have many services on a server.
  • Services responses for many requests per second.
  • Services make requests to other REST services via HttpClient.
  • Usually HttpClient setups via HttpClientFactory in Startup.cs:
services
    .AddHttpClient<IOtherService, OtherService>()
    .ConfigureHttpClient(httpClient => httpClient.Timeout = TimeSpan.FromSeconds(3))
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { MaxConnectionsPerServer = 10 });

Usually, it works pretty well, but sometimes (in a moment of high load) I get many TaskCanceledException and OtherService continues to respond as fast as it can. I understand that HttpClientHandler reaches the limit of MaxConnectionsPerServer.

My questions:

  • Is it the correct way of using MaxConnectionsPerServer? Maybe I should not use MaxConnectionsPerServer anymore?
  • How can I find the correct value for MaxConnectionsPerServer?

Solution

  • Questions asked by me do not have short answers. The approach described in my question is probably a superficial implementation of the Bulkhead pattern.

    My answers:

    Is it the correct way of using MaxConnectionsPerServer? Maybe I should not use MaxConnectionsPerServer anymore?

    It is the correct way if I want to reduce a load on called service (IOtherService)

    How can I find the correct value for MaxConnectionsPerServer?

    I have to collect monitoring data and analyze actual cases if I want to find the correct value for MaxConnectionsPerServer. Also, it can change after changing a load on my service or IOtherService