6

When I set up a connection to my ElasticSearch cluster using ElasticSearch.NET, I am using a code block like the following:

var uris = settingsProvider.ElasticSearchUri.Split(';').Select(x => new Uri(x));
var sniffingConnectionPool = new SniffingConnectionPool(uris);
var connectionConfiguration =
    new ConnectionConfiguration(sniffingConnectionPool)
        .SniffOnConnectionFault()
        .SniffOnStartup();
var client = new ElasticsearchClient(settings: connectionConfiguration);

Is it recommended that I memoize/make static/make a singleton wrapper for the ElasticsearchClient, the ConnectionConfiguration, or the SniffingConnectionPool so that they don't have to be reconstructed each time I search?

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
davidadsit
  • 163
  • 1
  • 6

3 Answers3

0

I've not seen anything in the documentation to advise otherwise, but generally I would err on the side of caution and avoid the singleton as the docs also make no promises about thread-safety. Be sure to dispose anything IDisposable, and if you're worried about performance or memory usage use a profiler to determine where to target your efforts.

batwad
  • 3,588
  • 1
  • 24
  • 38
0

We noticed that when using the Sniffing takes between 800ms and 1.2s longer for the search in a 5 node cluster. We thought we'd do a singleton so that we only sniff once. and set .SniffOnConnectionFault(true) then if the Node it Sniffed goes away it will pick another if I'm understanding that correctly.

Has anyone used this pattern and is there a better way since sniffing is so slow?

  • Is it 800 ms on the first request only or is it constantly 800 ms through out multiple requests ? I've read posts of people mentioning that the client needs to warm up for the first request which takes significantly more time. – TchiYuan Jun 18 '16 at 23:59
0

This is an old post, but this info might help someone. The Elasticsearch documentation says:

In general we advise folks to register their ElasticClient instances as singletons. The client is thread safe so sharing an instance between threads is fine.

https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/lifetimes.html