# Routing to a specific shard

**URL:** <https://discuss.elastic.co/t/routing-to-a-specific-shard/143688>\
**Category:** Elasticsearch\
**Created:** [August 9, 2018, 12:30pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688 "2018-08-09T12:30:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 9, 2018, 12:30pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/1 "2018-08-09T12:30:51Z")

</div>

Hi, i have an index with 10 shards.  
My application has 10 users.  
I'd like to assign each shard to a specific user, avoiding the possible collisions in the hash function used in the formula that computes the shard number from the \_routing parameter

[shard\_num = hash(\_routing) % num\_primary\_shards](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html)

Is there something similar to the routing mechanism that redirects my requests to a specific shard number?

Thanks

---

<div class="post-metadata">

**Author:** ![tdasch](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@tdasch](https://discuss.elastic.co/u/tdasch)\
**Post date:** [August 9, 2018, 1:50pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/2 "2018-08-09T13:50:18Z")

</div>

Andrea,

I think [this doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html) might be helpful for you. There is also an [Elastic blog post](https://www.elastic.co/blog/customizing-your-document-routing) about custom routing that could be helpful.

---

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 9, 2018, 2:23pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/3 "2018-08-09T14:23:10Z")

</div>

Hi Thomas, thanks for your reply.  
I also thought to use the routing(userId); the problem in this scenario is that the shard number is computed using a hash function, so i cannot be sure that the same shard is not assigned to 2 (or more) different users.

The [preference parameter in the SearchAPI](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html) ,  
specifying the value **\_shards:_shardnumber_** , retrieves the documents from the shard _shardnumber_, but i cannot find anything similar in the DocumentAPI that inserts the document in the shard _shardnumber_

---

<div class="post-metadata">

**Author:** ![tdasch](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@tdasch](https://discuss.elastic.co/u/tdasch)\
**Post date:** [August 9, 2018, 3:39pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/4 "2018-08-09T15:39:25Z")

</div>

Andrea,

I see (\_routing) uses the hash function either way like you pointed out.

What would be the intention of this use case? Maybe their is an alternative solution that would work based off the use case.

---

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 10, 2018, 8:04am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/5 "2018-08-10T08:04:18Z")

</div>

Given that an index with N shards will be used by exactly N users, I'd like to have a one to one correspondence between the users and the shards:

- index(document, uid1) --\> document is stored in shard n1 = getshard(uid1)

- index(document, uid2) --\> document is stored in shard n2 = getshard(uid2)

- index(document, uidN) --\> document is stored in shard nN = getshard(uidN)

and for each i, j s.t. (1 \<= i \<= N and 1 \<= j \<= N and i != j) , ni != nj

And of course there should be a way to search in the correct shard using the uid:

search(query, uid1) will search only in the shard n1 = getshard(uid1)

---

<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:** [August 10, 2018, 8:23am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/6 "2018-08-10T08:23:59Z")

</div>

Why not set them up as separate indices? Why can multiple users not share a shard?

---

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 10, 2018, 8:29am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/7 "2018-08-10T08:29:07Z")

</div>

The biggest problem is the number of users and its growth in the future, and consequently the amount of resources that the separate indexes strategy would require

---

<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:** [August 10, 2018, 8:38am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/8 "2018-08-10T08:38:05Z")

</div>

Why can users not share shards? If each user needs a separate shards (or two) this is still going to scale badly if users have small amounts of data.

---

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 10, 2018, 8:52am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/9 "2018-08-10T08:52:44Z")

</div>

It's a requirement: we want to separate user's data in the index

---

<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:** [August 10, 2018, 9:30am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/10 "2018-08-10T09:30:00Z")

</div>

Having lots of small shards dedicated to specific users is unlikely to scale well whether they are directly linked to an index or part of larger indices the way you describe. If you have a small number of users I would recommend having separate indices per user. If you want this to scale to large number of users, I would recommend you reconsider having dedicated shards per customer.

---

<div class="post-metadata">

**Author:** ![tdasch](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@tdasch](https://discuss.elastic.co/u/tdasch)\
**Post date:** [August 10, 2018, 3:40pm UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/11 "2018-08-10T15:40:32Z")

</div>

Andrea,

I haven't found a way to send docs to a specific shard unfortunately. You pointed out two concerns, `The biggest problem is the number of users and its growth in the future, and consequently the amount of resources that the separate indexes strategy would require` One of the solutions @Christian_Dahlqvist mentioned may work well for you and solve both problems. If you used a shared index, you could have an index capable of supporting a scaling number of users with each users documents residing on a single shard. You could then use aliases to give the perception of a single index per user. You have scalability as well as less resources being used since Elasticsearch would not be sending requests out to all the shards in the cluster, just the one shard for your user. You can read the Definitive Guide section dedicated to [shared indexes here](https://www.elastic.co/guide/en/elasticsearch/guide/current/shared-index.html).

---

<div class="post-metadata">

**Author:** ![Andrea\_Ranieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_ranieri/32/34335_2.png) [@Andrea\_Ranieri](https://discuss.elastic.co/u/Andrea_Ranieri)\
**Post date:** [August 11, 2018, 9:18am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/12 "2018-08-11T09:18:19Z")

</div>

We'll explore the shared index solution with aliases. Thomas and Christian, thanks for the support.

---

<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:** [August 11, 2018, 9:33am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/13 "2018-08-11T09:33:31Z")

</div>

I am not sure having hundreds of thousands of aliases will scale well either as they are all kept in the cluster state.

---

<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:** [September 8, 2018, 9:46am UTC](https://discuss.elastic.co/t/routing-to-a-specific-shard/143688/14 "2018-09-08T09:46:12Z")

</div>

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