# K-NN Resource Usage

**URL:** <https://forum.opensearch.org/t/k-nn-resource-usage/3417>\
**Category:** k-NN\
**Created:** [July 30, 2020, 4:07pm UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417 "2020-07-30T16:07:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnock](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@dnock](https://forum.opensearch.org/u/dnock)\
**Post date:** [July 30, 2020, 4:07pm UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/1 "2020-07-30T16:07:05Z")

</div>

Hey! I was wondering if there was guidance that could be provided with regards resource usage for elastic search clusters running this plugin. I’m interesting in exploring given N documents, M graph links, and vectors of length d:

1. How much should we scale our cluster to accommodate efficient queries?
2. What SLAs can we provide with regards to the number of documents we can support while still surfacing performant queries?
3. Are the underlying query graphs just stored on data nodes? And is the **graphMemoryUsage** statistic the best metric for exploring memory consumption increases? When we run **GET \_cat/indices** and look at tm for a given index the measurement reads 1064kb while the node **graph\_memory\_usage** for **GET \_opendistro/\_knn/stats** range from 0 - 3171kb depending on the node. Which value is best for measuring memory consumption?
4. If a cluster has multi indices each with their own knn vectors, how is the **knn.memory.circuit\_breaker.limit** value measured? Is it on a per graph bases?

Thanks in advance any guidance!

---

<div class="post-metadata">

**Author:** ![jmazane](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jmazane](https://forum.opensearch.org/u/jmazane)\
**Post date:** [August 4, 2020, 7:21pm UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/2 "2020-08-04T19:21:05Z")

</div>

Hi @dnock  
_1. How much should we scale our cluster to accommodate efficient queries?_

In order to achieve efficient queries, all of the graphs will need to fit into the available memory.

```auto
Available memory = (RAM - Elasticsearch Max Heap Size) * Circuit Breaker Limit (i.e. 0.5 for 50%)

```

To estimate the amount of memory your graphs will take up, we use the following formula:

```auto
Total graph memory = 1.1 * (8*M + 4*dimension) * number of vectors (including replicas)

```

For efficient queries:

```auto
Total graph memory < Available memory

```

_2. What SLAs can we provide with regards to the number of documents we can support while still surfacing performant queries?_  
It depends on the index set up configuration like number of shards. Here are some numbers from our experiment:  
Data set:- 150M vectors with 128 dimensions across different indices.  
Algo params :- m=16, efSearch=1024, efConstruction=1024,  
No of data nodes :- 6, m5.12xlarge  
Mater nodes :- 3, m5.xlarge

Latencies:-  
tp50: 22ms  
tp90: 40ms  
tp99: 90ms

We have done performance analysis for different vector dimensions and collection. We need to formalize and put in the consumable manner. We are prioritizing the effort to bring this to the [performance tuning doc](https://github.com/opendistro-for-elasticsearch/k-NN/blob/master/PerformanceTuning.md).

_3. Are the underlying query graphs just stored on data nodes? And is the graphMemoryUsage statistic the best metric for exploring memory consumption increases? When we run GET \_cat/indices and look at tm for a given index the measurement reads 1064kb while the node graph\_memory\_usage for GET \_opendistro/\_knn/stats range from 0 - 3171kb depending on the node. Which value is best for measuring memory consumption?_  
Yes, the underlying graphs are just stored on data nodes. No graphs will be stored on dedicated masters.

Yes, graphMemoryUsage statistic is the best metric for evaluating memory consumption increases. Also keep an eye on cache capacity reached metric and circuit breaker triggered. These indicate that the cache has been filled up and higher latencies on search will follow.

`_cat/indices` will not keep track of the memory the graphs use. `GET _opendistro/_knn/stats` should be preferred for measuring memory consumption.

_4. If a cluster has multi indices each with their own knn vectors, how is the knn.memory.circuit\_breaker.limit value measured? Is it on a per graph bases?_  
_knn.memory.circuit\_breaker.limit_ applies to the total memory of all of the k-NN indices, not per index. For example, if you have 10 indices, each with 5 GB of graphs and the circuit breaker limit is set to a value that allows 40 GB of available memory, only 8 of those indices would fit in the cache.

Jack

---

<div class="post-metadata">

**Author:** ![dnock](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@dnock](https://forum.opensearch.org/u/dnock)\
**Post date:** [August 4, 2020, 8:42pm UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/3 "2020-08-04T20:42:59Z")

</div>

Thanks for the response! This is incredibly helpful! I have one more question with regards to the _knn.memory.circuit\_breaker.limit_ value. So if we’ve configured our [heap size setting](https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html#:~:text=By%20default%2C%20Elasticsearch%20tells%20the,entire%20heap%20specified%20in%20jvm.) for our elastic search cluster, _knn.memory.circuit\_breaker.limit_ allows us to configure the remaining memory percentage we’d like to allow our HNSW graphs to consume [(total ram - Xmx) \* _knn.memory.circuit\_breaker.limit_] on our datanodes?

---

<div class="post-metadata">

**Author:** ![jmazane](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jmazane](https://forum.opensearch.org/u/jmazane)\
**Post date:** [August 4, 2020, 10:04pm UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/4 "2020-08-04T22:04:18Z")

</div>

Correct, we set it by [default to 50% of the remaining memory after accounting for the JVM](https://github.com/opendistro-for-elasticsearch/k-NN/blob/master/src/main/java/com/amazon/opendistroforelasticsearch/knn/index/KNNSettings.java#L170) because Lucene and Elasticsearch use off heap memory for other things such as file system caching and we want to leave space for this.

---

<div class="post-metadata">

**Author:** ![glorin](https://avatars.discourse-cdn.com/v4/letter/g/b487fb/32.png) [@glorin](https://forum.opensearch.org/u/glorin)\
**Post date:** [December 10, 2024, 9:26am UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/5 "2024-12-10T09:26:49Z")

</div>

Sorry to reply after 4 years but I wonder whether I can change _knn.memory.circuit\_breaker.limit_ setting to 70% (default is 50%) because I want to handle more vectors.

For example, I have a 32GiB node and I plan to give 12GiB to jvm and set limit to 70% so that I can get 14GiB for knn, is that OK?

---

<div class="post-metadata">

**Author:** ![Navneet](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/navneet/32/3645_2.png) [@Navneet](https://forum.opensearch.org/u/Navneet)\
**Post date:** [December 13, 2024, 8:57am UTC](https://forum.opensearch.org/t/k-nn-resource-usage/3417/6 "2024-12-13T08:57:33Z")

</div>

> [@glorin](#):
>
> I wonder whether I can change _knn.memory.circuit\_breaker.limit_ setting to 70% (default is 50%) because I want to handle more vectors.

Yes you can change this. This is a cluster setting can be changed just like any other cluster setting.

> [@glorin](#):
>
> For example, I have a 32GiB node and I plan to give 12GiB to jvm and set limit to 70% so that I can get 14GiB for knn, is that OK?

Yes this should be okay.
