# How does Elasticsearch balance data across nodes in a cluster

**URL:** https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288
**Category:** Elasticsearch
**Created:** [February 20, 2019, 8:27pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288 "2019-02-20T20:27:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [February 20, 2019, 8:27pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288/1 "2019-02-20T20:27:50Z")

</div>

What Algorithm does Elasticsearch use to uniformly distribute data across different nodes in the cluster? How does it deal with new nodes / dead nodes?

Does it use disk space, consistent hashing? Any resources that you can share would be greatly appreciated. Thank you.

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [February 20, 2019, 9:52pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288/2 "2019-02-20T21:52:12Z")

</div>

Data is split across shards by hashing the document ID, dividing the hash by the number of shards and taking the remainder.

Shards are allocated to nodes taking a number of factors into account, including disk space. The [reference manual](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cluster.html) has all the details.

New nodes and dead nodes aren't treated particularly specially by the algorithm. Allocation decisions are made assuming that the membership of the cluster is fixed. When an empty node joins the cluster Elasticsearch will relocate some data onto it so that each node holds roughly the same number of shards. If a node fails then the shards it held are distributed among the remaining nodes, although there's a short delay before doing anything in case it comes back.

---

<div class="post-metadata">

### Author: ![animageofmine](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@animageofmine](https://discuss.elastic.co/u/animageofmine)
#### Post date: [February 21, 2019, 8:08pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288/3 "2019-02-21T20:08:13Z")

</div>

Thank you David.

> [@DavidTurner](#):
>
> Data is split across shards by hashing the document ID, dividing the hash by the number of shards and taking the remainder.

Which documentID is this? The only documentID I am familiar in ES is the actual index document ID. Also, how can you divide the hash (It has hex alphabets and numbers).

> [@DavidTurner](#):
>
> When an empty node joins the cluster Elasticsearch will relocate some data onto it so that each node holds roughly the same number of shards. If a node fails then the shards it held are distributed among the remaining nodes, although there's a short delay before doing anything in case it comes back.

How does ES decide which shards to relocate?

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [February 21, 2019, 9:40pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288/4 "2019-02-21T21:40:23Z")

</div>

> [@animageofmine](#):
>
> The only documentID I am familiar in ES is the actual index document ID

Yes, that's the one.

> [@animageofmine](#):
>
> Also, how can you divide the hash (It has hex alphabets and numbers)

The hash used in Elasticsearch is [a 32-bit number](https://en.wikipedia.org/wiki/MurmurHash). The hashes that you are thinking of, containing digits and letters, are still numbers; they are often written in [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) so they're shorter and because computers can deal with numbers more efficiently when they're written in hexadecimal than when they're in decimal (i.e. only using digits).

> [@animageofmine](#):
>
> How does ES decide which shards to relocate?

It picks some of the shards that satisfy the constraints (disk space, allocation filtering, etc.) measuring approximately [how "balanced" the cluster is](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/shards-allocation.html#_shard_balancing_heuristics) and looking for relocations that improve its balance the most.

---

<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: [March 21, 2019, 9:40pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-balance-data-across-nodes-in-a-cluster/169288/5 "2019-03-21T21:40:27Z")

</div>

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