Is RestHighLevelClient keep connections open? Is RestHighLevelClient keep connections open? elasticsearch elasticsearch

Is RestHighLevelClient keep connections open?


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 usedto perform requests based on the provided builder. That low-levelclient maintains a pool of connections and starts some threads so youshould close the high-level client when you are well and truly donewith it and it will in turn close the internal low-level client tofree 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.