# About the immutableOpenmap in elasticsearch

**URL:** <https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121>\
**Category:** Elasticsearch\
**Created:** [May 19, 2022, 3:41am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121 "2022-05-19T03:41:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xingzhao](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xingzhao](https://discuss.elastic.co/u/xingzhao)\
**Post date:** [May 19, 2022, 3:41am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/1 "2022-05-19T03:41:17Z")

</div>

I would like to ask what consideration is the immutableOpenmap in Elasticsearch designed for? What kind of utility does it have? It is used in both aliasmetadata and indexmetadata. My personal guess is that it has something to do with atomicity, but I don't get the full understanding from the code.

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [May 19, 2022, 7:04am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/2 "2022-05-19T07:04:32Z")

</div>

Hi @xingzhao ,

it's key property is that it is immutable. This makes it easy to share among multiple threads. Since it can never mutate, there is no need for any inter-thread synchronization when accessing the map.

Immutability is used widely in Elasticsearch (and Lucene) to avoid having to synchronize access to such shared resources.

---

<div class="post-metadata">

**Author:** ![xingzhao](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xingzhao](https://discuss.elastic.co/u/xingzhao)\
**Post date:** [May 19, 2022, 7:08am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/3 "2022-05-19T07:08:25Z")

</div>

got it, thank you very much!

---

<div class="post-metadata">

**Author:** ![xingzhao](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xingzhao](https://discuss.elastic.co/u/xingzhao)\
**Post date:** [May 19, 2022, 7:28am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/4 "2022-05-19T07:28:00Z")

</div>

I have a related question. If two threads add a alias into openmap and use the build function to build a new immutableopenmap, how to ensure the final result is correct and it won't happened that one add action override the other?

---

<div class="post-metadata">

**Author:** ![HenningAndersen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henningandersen/32/48188_2.png) [@HenningAndersen](https://discuss.elastic.co/u/HenningAndersen)\
**Post date:** [May 19, 2022, 7:43am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/5 "2022-05-19T07:43:12Z")

</div>

First of all, `ImmutableOpenMap.Builder` can only be used by one thread at a time, it is not thread-safe.

The way Elasticsearch handles this is that every change to cluster state goes through a single master thread that applies the changes. So if two users add an alias, this will (in principle) be two cluster state updates, each executing one strictly after the other.

We do use batching of cluster state updates, but that is for performance more than conceptual.

---

<div class="post-metadata">

**Author:** ![xingzhao](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xingzhao](https://discuss.elastic.co/u/xingzhao)\
**Post date:** [May 19, 2022, 7:45am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/6 "2022-05-19T07:45:46Z")

</div>

thank you very much

---

<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:** [June 16, 2022, 7:46am UTC](https://discuss.elastic.co/t/about-the-immutableopenmap-in-elasticsearch/305121/7 "2022-06-16T07:46:29Z")

</div>

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