# Routing Logic in ElasticSearch

**URL:** <https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248>\
**Category:** Elasticsearch\
**Created:** [January 7, 2013, 10:37am UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248 "2013-01-07T10:37:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaidik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vaidik/32/1134_2.png) [@vaidik](https://discuss.elastic.co/u/vaidik)\
**Post date:** [January 7, 2013, 10:37am UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248/1 "2013-01-07T10:37:16Z")

</div>

Routing seems to be a pretty interesting feature for it can give quite a  
performance boost. But, what hashing algorithm is used for deciding the  
correct shard? I am asking this because in my use-case, we have multiple  
shards per index with 0 replicas. We really don't want replicas because HA  
is not an issue in our case. So, if one shard goes down, then what happens  
at the time of performing operations issued with the routing key associated  
with the shard that failed? I guess while performing search, ES will return  
0 counts with 1 failed shard. But, more specifically what happens while  
inserting against the failed shard?

Thanks,  
Vaidik

--

---

<div class="post-metadata">

**Author:** ![drewr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drewr/32/7803_2.png) [@drewr](https://discuss.elastic.co/u/drewr)\
**Post date:** [January 7, 2013, 5:06pm UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248/2 "2013-01-07T17:06:50Z")

</div>

Vaidik Kapoor wrote:

> Routing seems to be a pretty interesting feature for it can give  
> quite a performance boost. But, what hashing algorithm is used for  
> deciding the correct shard?

It's the DjbHashFunction [[http://git.io/4rL8JQ](http://git.io/4rL8JQ)].

> I am asking this because in my use-case, we have multiple shards  
> per index with 0 replicas. We really don't want replicas because HA  
> is not an issue in our case. So, if one shard goes down, then what  
> happens at the time of performing operations issued with the  
> routing key associated with the shard that failed? I guess while  
> performing search, ES will return 0 counts with 1 failed shard.  
> But, more specifically what happens while inserting against the  
> failed shard?

This is relatively easy to test. Start three instances of ES (can be  
in the same extract directory). Create your index:

curl -s -XPOST localhost:9200/kapoor -d '  
{  
"settings" : {  
"number\_of\_replicas" : 0,  
"number\_of\_shards" : 3  
}  
}  
'

This will create an index with a single primary shard on each node.  
Then kill one of the nodes (Ctrl-C in the shell) and try indexing a  
document:

curl -s -XPUT localhost:9200/kapoor/t/1 -d '  
{  
"name" : "Vaidik Kapoor"  
}  
'

It should succeed. ES will choose one of the shards that's available  
and hash into that. But when you search you'll see one hit and:

```
 [...]
 "_shards" : {
    "failures" : [
       {
          "index" : "kapoor",
          "status" : 500,
          "reason" : "No active shards",
          "shard" : 0
       }
    ],
    "failed" : 1,
    "successful" : 2,
    "total" : 3
 },
 [...]

```

So, no data should be lost when indexing into a degraded index,  
however, you won't see any hits from the failed shard(s) when  
searching.

-Drew

--

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [January 7, 2013, 7:15pm UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248/3 "2013-01-07T19:15:16Z")

</div>

Hi Drew

> This will create an index with a single primary shard on each node.  
> Then kill one of the nodes (Ctrl-C in the shell) and try indexing a  
> document:
> 
> curl -s -XPUT localhost:9200/kapoor/t/1 -d '  
> {  
> "name" : "Vaidik Kapoor"  
> }  
> '
> 
> It should succeed. ES will choose one of the shards that's available  
> and hash into that. But when you search you'll see one hit and:

That's not quite correct. It will hash to the appropriate shard then  
try to index to that shard. If the shard exists and is running, then  
indexing will succeed. If it is missing, then indexing will fail. It  
doesn't hash based on the number of live shards which exist - it uses  
just the number of shards which SHOULD exist. Otherwise it wouldn't know  
which shard to look at retrieve a doc when the previously missing shard  
springs into existence.

clint

--

---

<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:** [January 7, 2013, 8:29pm UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248/4 "2013-01-07T20:29:28Z")

</div>

Just wondering if it could be stored in the transaction log and indexed later when the shard comes up again?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 7 janv. 2013 à 20:15, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) a écrit :

Hi Drew

> This will create an index with a single primary shard on each node.  
> Then kill one of the nodes (Ctrl-C in the shell) and try indexing a  
> document:
> 
> curl -s -XPUT localhost:9200/kapoor/t/1 -d '  
> {  
> "name" : "Vaidik Kapoor"  
> }  
> '
> 
> It should succeed. ES will choose one of the shards that's available  
> and hash into that. But when you search you'll see one hit and:

That's not quite correct. It will hash to the appropriate shard then  
try to index to that shard. If the shard exists and is running, then  
indexing will succeed. If it is missing, then indexing will fail. It  
doesn't hash based on the number of live shards which exist - it uses  
just the number of shards which SHOULD exist. Otherwise it wouldn't know  
which shard to look at retrieve a doc when the previously missing shard  
springs into existence.

clint

--

--

---

<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:57am UTC](https://discuss.elastic.co/t/routing-logic-in-elasticsearch/10248/5 "2017-07-06T02:57:31Z")

</div>


