# How should I wrap and reuse the RestClient behind in a web service facade?

**URL:** https://discuss.elastic.co/t/how-should-i-wrap-and-reuse-the-restclient-behind-in-a-web-service-facade/234806
**Category:** Elasticsearch
**Created:** [May 28, 2020, 7:35pm UTC](https://discuss.elastic.co/t/how-should-i-wrap-and-reuse-the-restclient-behind-in-a-web-service-facade/234806 "2020-05-28T19:35:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mmu](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@mmu](https://discuss.elastic.co/u/mmu)
#### Post date: [May 28, 2020, 7:35pm UTC](https://discuss.elastic.co/t/how-should-i-wrap-and-reuse-the-restclient-behind-in-a-web-service-facade/234806/1 "2020-05-28T19:35:44Z")

</div>

I have a JAVA service that wraps ES for specific queries behind an API. This means all requests are inspected by the facade and translated into ES queries via the JAVA API.

Creating a fresh RestClient for each incoming request seems a bit of an overhead to me. On the other hand I am not sure whether it is wise to channel all incoming requests through the same RestClient instance. On the other hand the implementation seems to be quite robust, thread-safe and provides some failover logic.

So what would be the recommended approach for this kind of setup? Channel all requests through a single instance? Create a pool of RestClients (Like an HTTP connection pool) or do something else? Should I do some kind of manual health check? Or is that already performed by the underlying HttpClient?

p.s.: Load on the facade is usually not very heavy and I don't assume more than 50 concurrent requests.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 29, 2020, 4:43am UTC](https://discuss.elastic.co/t/how-should-i-wrap-and-reuse-the-restclient-behind-in-a-web-service-facade/234806/2 "2020-05-29T04:43:03Z")

</div>

You should use one client only. It's threadsafe.

---

<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: [June 26, 2020, 4:43am UTC](https://discuss.elastic.co/t/how-should-i-wrap-and-reuse-the-restclient-behind-in-a-web-service-facade/234806/3 "2020-06-26T04:43:05Z")

</div>

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