# How to achieve Long live http connections using an elasticsearch client (e.g Tire gem)

**URL:** https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139
**Category:** Elasticsearch
**Created:** [August 9, 2013, 2:39am UTC](https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139 "2013-08-09T02:39:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hrishikesh\_prabhune](https://avatars.discourse-cdn.com/v4/letter/h/6f9a4e/32.png) [@hrishikesh\_prabhune](https://discuss.elastic.co/u/hrishikesh_prabhune)
#### Post date: [August 9, 2013, 2:39am UTC](https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139/1 "2013-08-09T02:39:47Z")

</div>

I am doing bulk indexing using Tire Gem as the client for Elasticsearch

index = Tire::Index.new('oldskool')  
index.bulk\_store(bulk\_values)

I monitor the HTTP connections on my Elasticsearch cluster by using the  
http monitor API,

￼￼￼￼curl 'localhost:9200/\_nodes/http/stats'

In the JSON response that I get ,

..."http":{"current\_open" : 10, "total\_opened" : 18345}

I observed that the "total\_opened" field value goes on increasing rapidly.  
I think this means that the Tire gem is not using persistent connections  
while bulk indexing( Please correct me if I am wrong ).

How can I use Tire Gem to make persistent connections with Elasticsearch  
while doing bulk indexing?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [August 14, 2013, 3:32pm UTC](https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139/2 "2013-08-14T15:32:37Z")

</div>

I see your question here, and on StackOverflow. And the more I think about  
it, the more I agree that you have asked an excellent question.

Even though Elasticsearch's HTTP interface is "RESTful", that does not mean  
it's not able to support persistent connections. ES uses Netty under the  
covers, and Netty supports HTTP 1.1 persistent connections by default.

So this is likely a Tire Gem issue, but it would be good to know how  
Elasticsearch sets up Netty for its HTTP interface. For example, I added  
session timeouts to my own Netty-based RESTful server, but with Netty this  
is a bit of a problem. Netty handles requests and responses on different  
streams, and so consider the case where the response takes a LONG TIME to  
complete. The client is, of course, waiting for this long response before  
it sends the next request. But the Netty server's reader doesn't know if  
the client is idle or just waiting for the response. In other words, in a  
Netty server, the left hand (reading client requests) has no idea what the  
right hand (handling client requests and generating the response) is doing.

So we had to very carefully adjust our client and server timeouts based on  
the behavior of HTTP persistent connections, the Netty server and my  
handler's response times, and our network firewall idle session timeout.

This is over and above your question. But, if you want to get Tire Gem to  
use persistent connections, then you'll suddenly need to worry about all or  
most of this. Maybe the Tire Gem developers decided that creating a new  
HTTP session for each bulk request (not document, but clump of documents)  
was minimal overhead and avoided the need to properly handle all of the  
nuances of HTTP 1.1 persistent connections.

Brian

On Thursday, August 8, 2013 10:39:47 PM UTC-4, hrishikesh prabhune wrote:

> I am doing bulk indexing using Tire Gem as the client for Elasticsearch
> 
> index = Tire::Index.new('oldskool')  
> index.bulk\_store(bulk\_values)
> 
> I monitor the HTTP connections on my Elasticsearch cluster by using the  
> http monitor API,
> 
> ￼￼￼￼curl 'localhost:9200/\_nodes/http/stats'
> 
> In the JSON response that I get ,
> 
> ..."http":{"current\_open" : 10, "total\_opened" : 18345}
> 
> I observed that the "total\_opened" field value goes on increasing rapidly.  
> I think this means that the Tire gem is not using persistent connections  
> while bulk indexing( Please correct me if I am wrong ).
> 
> How can I use Tire Gem to make persistent connections with Elasticsearch  
> while doing bulk indexing?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)
#### Post date: [August 14, 2013, 4:53pm UTC](https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139/3 "2013-08-14T16:53:20Z")

</div>

ES is counting Netty upstream ChannelEvents "channelOpen" [1] and provides  
them as value in HTTP stats in a channel handler [2]. It is not counting  
HTTP connections. ES is counting how Netty creates channels on TCP  
connections, which may be persistent/keepalive or not.

ES HTTP server is also providing TCP keepalive by default [3], and also  
socket reuse (except on Windows). The settings in [4] are also valid for  
the HTTP server in ES.

Check your system network status with netstat if there are suspicious long  
lasting socket connections and the number of CLOSE\_WAIT status is not  
acceptable.

Jörg

[1]  
[http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelEvent.html](http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelEvent.html)

[2]  
[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/netty/OpenChannelsHandler.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/common/netty/OpenChannelsHandler.java)  
[3]  
[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java)  
[4] [http://www.elasticsearch.org/guide/reference/modules/network/](http://www.elasticsearch.org/guide/reference/modules/network/)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:21am UTC](https://discuss.elastic.co/t/how-to-achieve-long-live-http-connections-using-an-elasticsearch-client-e-g-tire-gem/13139/4 "2017-07-06T02:21:10Z")

</div>


