elasticsearchresthighlevelclientelasticsearch-rest-client

Is RestHighLevelClient keep connections open?


I want to use RestHighLevelClient on different clusters with commands which are not supported by Cross Cluster mechanizem (for example close and open index).
My question is if I use more than one instance of RestHighLevelClient for every cluster it will keep connections open for every cluster? (to be ensure I didn't choke the application)


Solution

  • by looking at various resources, it seems RestHighLevelClient keeps the connection open unless you explicitly call client.close(); on it.

    From the official RestHighLevelClient initialization

    The high-level client will internally create the low-level client used to perform requests based on the provided builder. That low-level client maintains a pool of connections and starts some threads so you should close the high-level client when you are well and truly done with it and it will in turn close the internal low-level client to free those resources. This can be done through the close method:

    In your case, if you having a lot of ES clusters and creating multiple RestHighLevelClient than as you are guessing, it might choke your application due to the hold of threads and its resources so you should explicitly call the close which would require more time when you again create it but would not choke your application in most of the cases.

    I would suggest you do some resource benchmarking on your application and based on your trade-off choose the best possible approach.

    1. Create multiple clients and don't close them but allocate more resources so that the application is fast and don't choke.
    2. close clients frequently, this would not require over-allocating resources but when you create a new client for your request, latency will be more.