# Is NodeClient thread safe?

**URL:** <https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231>\
**Category:** Elasticsearch\
**Created:** [April 13, 2011, 2:23pm UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231 "2011-04-13T14:23:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barsk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barsk/32/13734_2.png) [@Barsk](https://discuss.elastic.co/u/Barsk)\
**Post date:** [April 13, 2011, 2:23pm UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/1 "2011-04-13T14:23:25Z")

</div>

Hi,

I am using the NodeClient in a servlet where I create one instance of  
the node. Then in every web request coming in I create a client from  
that node and start searching etc with it. I.e the node (and client) is  
shared among several sessions. For this to work the client need to be  
thread safe. The question is - is it?

/Kristian

---

<div class="post-metadata">

**Author:** ![Enrique\_Medina\_Monte](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Enrique\_Medina\_Monte](https://discuss.elastic.co/u/Enrique_Medina_Monte)\
**Post date:** [April 13, 2011, 2:25pm UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/2 "2011-04-13T14:25:23Z")

</div>

I wouldn't recommend creating a client on each and every request, as the  
client is sort of a expensive resource. You should create it once for your  
application and probably leave it in the servlet context available for all  
the requests.

On Wed, Apr 13, 2011 at 4:23 PM, Kristian Jörg [krjg@devo.se](mailto:krjg@devo.se) wrote:

> Hi,
> 
> I am using the NodeClient in a servlet where I create one instance of the  
> node. Then in every web request coming in I create a client from that node  
> and start searching etc with it. I.e the node (and client) is shared among  
> several sessions. For this to work the client need to be thread safe. The  
> question is - is it?
> 
> /Kristian

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 13, 2011, 6:45pm UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/3 "2011-04-13T18:45:54Z")

</div>

Yes, the NodeClient is thread safe (any Client is thread safe, also the TransportClient) and its lifecycle should be similar to the application lifecycle.  
On Wednesday, April 13, 2011 at 5:25 PM, Enrique Medina Montenegro wrote:

> I wouldn't recommend creating a client on each and every request, as the client is sort of a expensive resource. You should create it once for your application and probably leave it in the servlet context available for all the requests.
> 
> On Wed, Apr 13, 2011 at 4:23 PM, Kristian JÃ¶rg [krjg@devo.se](mailto:krjg@devo.se) wrote:
> 
> > Hi,
> > 
> > I am using the NodeClient in a servlet where I create one instance of the node. Then in every web request coming in I create a client from that node and start searching etc with it. I.e the node (and client) is shared among several sessions. For this to work the client need to be thread safe. The question is - is it?
> > 
> > /Kristian

---

<div class="post-metadata">

**Author:** ![Barsk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barsk/32/13734_2.png) [@Barsk](https://discuss.elastic.co/u/Barsk)\
**Post date:** [April 14, 2011, 9:26am UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/4 "2011-04-14T09:26:34Z")

</div>

```
Hi Enrique,

yes I do create only one instance of the client in exactly the way
you describe.

/Kristian

Enrique Medina Montenegro skrev 2011-04-13 16:25:
<blockquote cite="mid:BANLkTim06=c8wPn88vsLyB5KrZH-wUukMA@mail.gmail.com" type="cite">

```

 I wouldn't recommend creating a client on each and every request, as the client is sort of a expensive resource. You should create it once for your application and probably leave it in the servlet context available for all the requests.

On Wed, Apr 13, 2011 at 4:23 PM, Kristian JÃ¶rg \<[krjg@devo.se](mailto:krjg@devo.se)\>  
wrote:

> Hi,
> 
> ```
> I am using the NodeClient in a servlet where I create one
> instance of the node. Then in every web request coming in I
> create a client from that node and start searching etc with
> it. I.e the node (and client) is shared among several
> sessions. For this to work the client need to be thread safe.
> The question is - is it?
> 
> /Kristian
> 
> ```

---

<div class="post-metadata">

**Author:** ![Barsk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/barsk/32/13734_2.png) [@Barsk](https://discuss.elastic.co/u/Barsk)\
**Post date:** [April 14, 2011, 9:27am UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/5 "2011-04-14T09:27:53Z")

</div>

```
Ok!

Thanks!

/Kristian

Shay Banon skrev 2011-04-13 20:45:
<blockquote cite="mid:70D42DC561514FE68C4EB6B67D622648@elasticsearch.com" type="cite">

```

Yes, the NodeClient is thread safe (any Client is  
thread safe, also the TransportClient) and its lifecycle  
should be similar to the application lifecycle.

On Wednesday, April 13, 2011 at 5:25 PM, Enrique Medina Montenegro wrote:

> I wouldn't recommend creating a client on each and every request, as the client is sort of a expensive resource. You should create it once for your application and probably leave it in the servlet context available for all the requests.
> 
> On Wed, Apr 13, 2011 at 4:23 PM, Kristian JÃ¶rg \<[krjg@devo.se](mailto:krjg@devo.se)\>  
> wrote:
> 
> > Hi,
> > 
> > ```
> > I am using the NodeClient in a servlet where I
> > create one instance of the node. Then in every
> > web request coming in I create a client from
> > that node and start searching etc with it. I.e
> > the node (and client) is shared among several
> > sessions. For this to work the client need to be
> > thread safe. The question is - is it?
> > 
> > /Kristian
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Xin](https://avatars.discourse-cdn.com/v4/letter/x/bbce88/32.png) [@Xin](https://discuss.elastic.co/u/Xin)\
**Post date:** [January 8, 2015, 10:18pm UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/6 "2015-01-08T22:18:57Z")

</div>

My question is how client can handle multiple requests at same time? Will requests queue up? Or does it have a connection pool implemented?

---

<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:** [July 6, 2017, 12:39am UTC](https://discuss.elastic.co/t/is-nodeclient-thread-safe/4231/7 "2017-07-06T00:39:57Z")

</div>


