Search code examples
.netazure-functionssocketexceptionhttpclientfactory

Why I have too many established socket connection during high traffic with using HttpClientFactory in Azure function


I have Azure function app on dedicated premium App service plan. Function in one execution has to delivery two times some data to one api's endpoint. For sending data, I use http client which is provided from IHttpClientFactory. So, httpClientFactory.CreateClient is invoked twice in one function execution. Sometimes, I have a big traffic in small frame, 100k executions in 20min. I noticed that Socket Connections for outbound requests are over 1000, so in my view, it is weird, because I thought that IHttpClientFactory should reuse socket connections better, particular that, it is always the same endpoint. I am afraid that it can cause socket exhaustion.

How to avoid too many established Socket Connections?

Do you have some experience with MaxConnectionsPerServer handler's property? It seems that it can set a established connections limit.

I don't want to use static httpClient instance, because it can cause other issue (Dns refreshing)


Solution

  • When dealing with a large number of outbound requests in a short period of time, it’s important to manage and optimize the usage of HttpClient instances to avoid issues such as socket exhaustion.

    I don’t want to use a static httpClient instance, because it can cause other issues (DNS refreshing)

    1. Reuse HttpClient Instances:
      Although you mentioned that you don’t want to use a static HttpClient instance, it’s generally recommended to reuse instances to take advantage of connection pooling. However, if you’re concerned about issues like DNS refreshing, you can use a named client with a longer lifetime, allowing for better reuse.
    builder.Services.AddHttpClient<ICatalogService, CatalogService>()
     .SetHandlerLifetime(TimeSpan.FromMinutes(5));
    

    You can set how many minutes you want as the HttpMessageHandler objects lifetime.

    Do you have some experience with the MaxConnectionsPerServer handler’s property? It seems that it can set an established connection limit.

    1. You can set the MaxConnectionsPerServer property to limit the number of concurrent connections to a particular server. This can help prevent socket exhaustion.
    services.AddHttpClient("MyApiClient")
        .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
        {
            MaxConnectionsPerServer = 10 // Adjust as needed
        });
    

    Refer to this Microsoft GitHub Article on using the MaxConnectionsPerServer property.
    3. If the default connection pooling behavior isn’t sufficient, you can implement a custom connection pooling strategy which involves managing the lifecycle of HttpClient instances manually, ensuring reuse and proper disposal.
    4. Tools like Azure Application Insights or Azure Monitor can help with performance monitoring to adjust the connection pooling settings based on the observed behaviour of your application under load.