# Number of connections keep increasing for every search

**URL:** <https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425>\
**Category:** Elasticsearch\
**Created:** [September 5, 2017, 12:23pm UTC](https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425 "2017-09-05T12:23:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnugt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishnugt/32/22026_2.png) [@vishnugt](https://discuss.elastic.co/u/vishnugt)\
**Post date:** [September 5, 2017, 12:23pm UTC](https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425/1 "2017-09-05T12:23:07Z")

</div>

```
	String clusterName = GridContactManager.getClusterName(ClusterType.ElasticSearch, clusterID);
	String esHosts = LogConf.getESUnicastHosts(clusterID);
	String[] masterNodes = esHosts.split(StringUtil.COMMA);
	HttpHost[] hostsArr = new HttpHost[masterNodes.length];
	for (int i = 0; i < masterNodes.length; i++)
	{
		String masterIp = masterNodes[i];
		HttpHost host = new HttpHost(masterIp, 9200, "http");//No I18N
		hostsArr[i] = host;
	}
	RestClient restClient = RestClient.builder(hostsArr).build();
	return restClient;

```

When I check the number of requests periodically via `netstat`, the number of requests keeps increasing. I am initializing the restClient with the above code, and this code runs only once, so am not creating multiple RestClients. I use restClient.performRequest for all the search operations and also I am not closing the restClient anywhere.

Result of this `netstat | grep "9200" | wc -l` is as follows

After initializing the server - 7  
After 50 searches - 103  
After 100 searches - 202

For now, I am running this on my development machine with a single node.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 6, 2017, 4:57pm UTC](https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425/2 "2017-09-06T16:57:44Z")

</div>

You should create the client once and then reuse it all the time.

---

<div class="post-metadata">

**Author:** ![vishnugt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishnugt/32/22026_2.png) [@vishnugt](https://discuss.elastic.co/u/vishnugt)\
**Post date:** [September 7, 2017, 6:57am UTC](https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425/3 "2017-09-07T06:57:50Z")

</div>

Thanks @jpountz !!  
My bad, I was creating multiple rest clients in another part of the code.

Although now I am running rest client, comparing it with node client, the results aren't up to the par.

I have two clusters with 6 data nodes each and a dedicated master node for each cluster. Both cluster's data size comes to around 10GB with approx 10 million records.

While searching, node client takes 7 seconds while rest client takes 17 seconds. At all the times, node client returns the results at least 5 seconds earlier than the rest client.  
I am adding sniffer to both the clients as well.

Any optimization insights would be helpful.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 5, 2017, 6:58am UTC](https://discuss.elastic.co/t/number-of-connections-keep-increasing-for-every-search/99425/4 "2017-10-05T06:58:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
