简体   繁体   中英

How many elasticsearch client connection should we create in the application

I'm using Golang & elastic client .

Bellow is my client creation logic:

if client, err := elastic.NewClient(elastic.SetURL(ElasticsearchURL)); err != nil {
    // Handle error
    logger.Error.Println(err)
    return nil
} else {
    return client
}

Whats the correct approach to:

  • keep the client object singleton across the application?
  • create and close the clients for each request?

I am kind of confused between counterintuitive answers in below links:

  1. where-to-close-an-elasticsearch-client-connection - suggests one connection per app
  2. how-many-transport-clients-can-a-elasticsearch-cluster-have - suggests one connection per app
  3. elasticsearch-how-to-query-for-number-of-connections -- kind of indicates connections quickly die after serving a request

That depends on the application.

In 99% of the use cases you have a normal, long-running application. Then you should create just one client with elastic.NewClient . You can pass it around in your code and it should always work, even in different goroutines. This will create a long-running client which has several benefits. Eg it will run health checks in the background that will prevent Elastic from sending requests to unhealthy or dead nodes.

However, if you have a short-running application (something like AWS Lambda oder Cloud Functions) you might need a "connection" on a request level. In that specific case you can use elastic.NewSimpleClient . It has a bit more overhead though as you're creating a new client every time. And it won't do any health checks and other things.

DO NOT create a new client with elastic.NewClient for every request, as any call to NewClient will create a set of goroutines and you'll quickly run out of resources if you do that.

Please read the documentation and the wiki for further details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM