locked
Azure Search SDK - SearchIndexClient and SearchServiceClient lifecycle management RRS feed

  • Question

  • Hi,

    I'm currently involved in a project consisting of an ASP.NET MVC SPA application in which Azure Search services are extensively used. Recently we've migrated from RedDog.Search library to Azure Search SDK v.1.1.1. At the beginning we based our implementation on sample code mentioned in the documentation (https://github.com/Azure-Samples/search-dotnet-getting-started) in which SearchIndexClient and SearchServiceClient are implemented as static references. Because this week we encountered some performance issues related to extensive memory usage we suspected that it might have something to do with inappropriate management of connections to Azure Search. That's why we decided to change life style of those services to transient and see what would happen. It hasn't had any positive impact on our memory usage and it also turned out that the requests are much slower. Additionally from time to time we get System.Net.Sockets.SocketException. To understand better what's happening under the hood and why static references are performing better we would like to ask what are the best practices for managing the lifecycles of those services and why?

    Kind Regards,

    Agnieszka.

    Thursday, June 30, 2016 11:47 AM

Answers

  • Hi Agnieszka,

    SearchIndexClient and SearchServiceClient each wrap an HttpClient, which can be an expensive resource if you create too many of them. Specifically, it's really easy to exhaust the TCP connection pool if you're not careful. There are two things you can do to avoid this:

    1. Make sure you're sharing HttpClients by keeping the Search clients static, so that connection demand can't grow out of control.
    2. Increase System.Net.ServicePointManager.DefaultConnectionLimit (which defaults to 2) to something like 12 * Environment.ProcessorCount so that you have enough connections to service your workload.

    You can find more documentation on the default connection limit here:
    https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit(v=vs.110).aspx

    The memory usage issue sounds unrelated, there is one thing tangentially related to the Search SDK that you can look at: Are you calling the Async methods of the SDK? Calling the synchronous methods from a web application is not recommended since they block the calling thread. When your app handles many requests, this can exhaust the thread pool and consume a lot of stack space while the threads handling those requests are blocked on Search requests. Use async everywhere if you can.

    Hope this helps,

    -Bruce

    Thursday, June 30, 2016 7:41 PM
    Moderator