# Routing - Massive injection with bulk API

**URL:** <https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095>\
**Category:** Elasticsearch\
**Created:** [March 1, 2016, 9:34am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095 "2016-03-01T09:34:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xam](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xam](https://discuss.elastic.co/u/xam)\
**Post date:** [March 1, 2016, 9:34am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/1 "2016-03-01T09:34:27Z")

</div>

Hi,

I was wondering how the bulk API works for massive indexing job on a multi node cluster.  
For me they are two possibilities:

- The Java client sends documents on a round robin basis to each node of the cluster. Then each node check the ID of each documents and reroute them if necessary to the correct shard.
- The java client computes the shard id from the document id for each document and directly sends the document to the correct node.  
If I have a look to the source code, I think the first approach is implemented, but it is kind of weird for me because I naively think that the first approach is more efficient...

Thank you for your answer

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [March 1, 2016, 9:45am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/2 "2016-03-01T09:45:54Z")

</div>

> [@xam](#):
>
> - The java client computes the shard id from the document id for each document and directly sends the document to the correct node.

This method, but see [Routing a Document to a Shard | Elasticsearch: The Definitive Guide [2.x] | Elastic](https://www.elastic.co/guide/en/elasticsearch/guide/current/routing-value.html) for the exact way the shard ID is calculated.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 1, 2016, 9:56am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/3 "2016-03-01T09:56:28Z")

</div>

You can also place a client node next to the client, and this will keep track of where shards are located and do the routing for you, avoiding unnecessary network hops.

---

<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:** [March 1, 2016, 9:58am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/4 "2016-03-01T09:58:02Z")

</div>

This is how bulk requests work:

1. Java client sends off bulk request in round robin manner to connected cluster nodes
2. connected cluster node receives bulk request
3. location of primary shards of all bulk items are calculated from cluster state
4. bulk request is partitioned into new bulk requests for nodes of primary shard locations
5. connected cluster node forwards bulk request parts to these nodes

---

<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:** [March 1, 2016, 9:58am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/5 "2016-03-01T09:58:11Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> You can also place a client node next to the client, and this will keep track of where shards are located and do the routing for you, avoiding unnecessary network hops.

+1

We just modified the Java documentation to describe this a bit: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.x/client-connected-to-client-node.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.x/client-connected-to-client-node.html)

---

<div class="post-metadata">

**Author:** ![xam](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xam](https://discuss.elastic.co/u/xam)\
**Post date:** [March 1, 2016, 10:11am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/6 "2016-03-01T10:11:57Z")

</div>

Hi,

Thank you for all your answers.  
If I understood well (please tell me if I am wrong 🙂 ):

- If I use a "simple" bulk request from the java client, the documents will be sent from my java process on a round robin basis to data nodes and then nodes will create sub bulk-requests to forward documents to node they belongs to (based on routing rules [https://www.elastic.co/guide/en/elasticsearch/guide/current/routing-value.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/routing-value.html)).
- If I instanciate a client node in my ingesting process, then a client node will run locally on my ingesting server and create itself the sub bulk-requests to the correct data nodes, avoiding the transmission of documents over the network more than one time.

Thank you

---

<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:** [March 1, 2016, 10:19am UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/7 "2016-03-01T10:19:04Z")

</div>

Correct.

TransportClient does not hold cluster state, and uses one more hop to transmit bulk requests, to a cluster node that holds cluster state. This node works like a proxy node.

NodeClient already holds cluster state, it does not need a proxy node, and can use the state to find out the nodes where documents should be indexed.

---

<div class="post-metadata">

**Author:** ![xam](https://avatars.discourse-cdn.com/v4/letter/x/bb73d2/32.png) [@xam](https://discuss.elastic.co/u/xam)\
**Post date:** [March 1, 2016, 3:09pm UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/8 "2016-03-01T15:09:54Z")

</div>

Ok thanks :-). It is clear now.

---

<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 5, 2017, 11:12pm UTC](https://discuss.elastic.co/t/routing-massive-injection-with-bulk-api/43095/9 "2017-07-05T23:12:27Z")

</div>


