# How to lock all data files into memory?

**URL:** <https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460>\
**Category:** Elasticsearch\
**Created:** [February 7, 2019, 1:37pm UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460 "2019-02-07T13:37:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.elastic.co/u/John16)\
**Post date:** [February 7, 2019, 1:37pm UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/1 "2019-02-07T13:37:38Z")

</div>

Hello,

I want all index data files to be locked into RAM so queries execute fast regardless of HDD usage.

I added  
index.store.preload: ["\*"]  
bootstrap.memory\_lock: true

to elasticsearch.yml.

But when I execute queries, I still see spikes of HDD iops during requests (index is in read only state) and queries execute slow for the first time. Running the same query 2nd time is fast, because data were recently read from disk.

curl -X GET "localhost:9200/\_nodes?filter\_path=\*\*.mlockall"  
reports that "mlockall":true

What's wrong? Is it possible to setup elastic so that _all_ data files are always in RAM?

Thanks in advance!

---

<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 7, 2019, 2:37pm UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/2 "2019-02-07T14:37:37Z")

</div>

Elasticsearch (i.e. Lucene) relies on the OS page cache to keep a copy of the on-disk index data in RAM if possible. It sounds like your operating system is paging that data out because it needs the RAM for other purposes.

`mlockall` is about swapping, which is basically the opposite process.

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.elastic.co/u/John16)\
**Post date:** [February 7, 2019, 4:35pm UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/3 "2019-02-07T16:35:02Z")

</div>

Well, mmap+mlock ensures that whole mmaped region is always available in RAM and it is not swapped out or paging out.

Regardless of reasons, is there any way to ensure that all data is wired in RAM?

---

<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 7, 2019, 10:17pm UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/4 "2019-02-07T22:17:18Z")

</div>

The short answer is "no", sorry. You are correct that `mlockall` can lock `mmap`ped pages (and what I said was incorrect) but I think this would be quite bad if your indices exceeded your physical memory. There's a detailed explanation of the situation here:

> <https://github.com/elastic/elasticsearch/issues/17605#issuecomment-207149090>

---

<div class="post-metadata">

**Author:** ![John16](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@John16](https://discuss.elastic.co/u/John16)\
**Post date:** [February 8, 2019, 10:38am UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/5 "2019-02-08T10:38:22Z")

</div>

I see. It's a sad, because sometimes you have more RAM than index data files, and search performance sucks just because VM paged out some data and search leads to excessive slow reads...

Following your login "rm" command should not exist too, because one could do "rm -rf /" and be in trouble 🙂

---

<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 8, 2019, 10:38am UTC](https://discuss.elastic.co/t/how-to-lock-all-data-files-into-memory/167460/6 "2019-03-08T10:38:24Z")

</div>

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