# Java Elasticsearch REST Client reuse or pooling

**URL:** <https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788>\
**Category:** Elasticsearch\
**Created:** [April 10, 2017, 9:43am UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788 "2017-04-10T09:43:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bican](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Bican](https://discuss.elastic.co/u/Bican)\
**Post date:** [April 10, 2017, 9:43am UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788/1 "2017-04-10T09:43:22Z")

</div>

Hello,  
I can not seem to find any info related to the connection pooling of the java rest client. I found several articles about transport client but nothing about the rest.  
We have a proxy server in front of ES cluster transforming requests. The version of the ES client is 5.2.2 and ES cluster is 2.4.4 . We are close to migrate the cluster to 5.2.2.  
There was a new client built for every request. I changed that to a singleton RestClient. Is this the best approach? In fact method RestClose.close() never gets called. We call synchronous performRequest method.  
Thanks any hints,  
Radovan

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 10, 2017, 4:04pm UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788/2 "2017-04-10T16:04:48Z")

</div>

I believe pooling it is better. Internally it uses an async client that starts a few threads and stuff. If you find contention on those threads you can configure the client differently when you build it, I believe.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [April 10, 2017, 5:39pm UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788/3 "2017-04-10T17:39:18Z")

</div>

Reusing a single instance of RestClient is the way to go, also calling close it important when the application gets stopped. This is mentioned in our docs here: [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_initialization.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_initialization.html).

The connection pooling is done internally in the underlying apache async http client. We increase by default the maxConnPerRoute to 10 and maxConnTotal to 30. That is just a guess on our end. You can always set your own values by providing a custom HttpClientConfigCallback that sets them using RestClientBuilder#setHttpClientConfigCallback.

---

<div class="post-metadata">

**Author:** ![Bican](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Bican](https://discuss.elastic.co/u/Bican)\
**Post date:** [April 11, 2017, 7:15am UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788/4 "2017-04-11T07:15:41Z")

</div>

Thank you very much. I have implemented ServletContextListener that takes care of the client and sniffer close() calls during redeploy.  
Radovan

---

<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:** [May 9, 2017, 7:19am UTC](https://discuss.elastic.co/t/java-elasticsearch-rest-client-reuse-or-pooling/81788/5 "2017-05-09T07:19:06Z")

</div>

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