Clarify disabling swap in docs

Our strong recommendation is disabling swap over any other alternative
to avoid the JVM from landing on disk. This commit clarifies the docs in
this regard.
This commit is contained in:
Jason Tedor 2017-05-12 16:09:52 -04:00
parent 5da940532d
commit 4e21a33689

View File

@ -2,32 +2,64 @@
=== Disable swapping === Disable swapping
Most operating systems try to use as much memory as possible for file system Most operating systems try to use as much memory as possible for file system
caches and eagerly swap out unused application memory. This can result in caches and eagerly swap out unused application memory. This can result in parts
parts of the JVM heap being swapped out to disk. of the JVM heap or even its executable pages being swapped out to disk.
Swapping is very bad for performance and for node stability and should be Swapping is very bad for performance, for node stability, and should be avoided
avoided at all costs. It can cause garbage collections to last for **minutes** at all costs. It can cause garbage collections to last for **minutes** instead
instead of milliseconds and can cause nodes to respond slowly or even to of milliseconds and can cause nodes to respond slowly or even to disconnect
disconnect from the cluster. from the cluster. In a resilient distributed system, it's more effective to let
the operating system kill the node.
There are three approaches to disabling swapping: There are three approaches to disabling swapping. The preferred option is to
completely disable swap. If this is not an option, whether or not to prefer
minimizing swappiness versus memory locking is dependent on your environment.
[[disable-swap-files]]
==== Disable all swap files
Usually Elasticsearch is the only service running on a box, and its memory usage
is controlled by the JVM options. There should be no need to have swap enabled.
On Linux systems, you can disable swap temporarily by running:
[source,sh]
--------------
sudo swapoff -a
--------------
To disable it permanently, you will need to edit the `/etc/fstab` file and
comment out any lines that contain the word `swap`.
On Windows, the equivalent can be achieved by disabling the paging file entirely
via `System Properties → Advanced → Performance → Advanced → Virtual memory`.
[[swappiness]]
==== Configure `swappiness`
Another option available on Linux systems is to ensure that the sysctl value
`vm.swappiness` is set to `1`. This reduces the kernel's tendency to swap and
should not lead to swapping under normal circumstances, while still allowing the
whole system to swap in emergency conditions.
[[mlockall]] [[mlockall]]
==== Enable `bootstrap.memory_lock` ==== Enable `bootstrap.memory_lock`
The first option is to use Another option is to use
http://opengroup.org/onlinepubs/007908799/xsh/mlockall.html[mlockall] on Linux/Unix systems, or https://msdn.microsoft.com/en-us/library/windows/desktop/aa366895%28v=vs.85%29.aspx[VirtualLock] on Windows, to http://opengroup.org/onlinepubs/007908799/xsh/mlockall.html[mlockall] on
try to lock the process address space into RAM, preventing any Elasticsearch Linux/Unix systems, or
memory from being swapped out. This can be done, by adding this line https://msdn.microsoft.com/en-us/library/windows/desktop/aa366895%28v=vs.85%29.aspx[VirtualLock]
to the `config/elasticsearch.yml` file: on Windows, to try to lock the process address space into RAM, preventing any
Elasticsearch memory from being swapped out. This can be done, by adding this
line to the `config/elasticsearch.yml` file:
[source,yaml] [source,yaml]
-------------- --------------
bootstrap.memory_lock: true bootstrap.memory_lock: true
-------------- --------------
WARNING: `mlockall` might cause the JVM or shell session to exit if it tries WARNING: `mlockall` might cause the JVM or shell session to exit if it tries to
to allocate more memory than is available! allocate more memory than is available!
After starting Elasticsearch, you can see whether this setting was applied After starting Elasticsearch, you can see whether this setting was applied
successfully by checking the value of `mlockall` in the output from this successfully by checking the value of `mlockall` in the output from this
@ -40,11 +72,12 @@ GET _nodes?filter_path=**.mlockall
// CONSOLE // CONSOLE
If you see that `mlockall` is `false`, then it means that the `mlockall` If you see that `mlockall` is `false`, then it means that the `mlockall`
request has failed. You will also see a line with more information in the request has failed. You will also see a line with more information in the logs
logs with the words `Unable to lock JVM Memory`. with the words `Unable to lock JVM Memory`.
The most probable reason, on Linux/Unix systems, is that the user running The most probable reason, on Linux/Unix systems, is that the user running
Elasticsearch doesn't have permission to lock memory. This can be granted as follows: Elasticsearch doesn't have permission to lock memory. This can be granted as
follows:
`.zip` and `.tar.gz`:: `.zip` and `.tar.gz`::
@ -55,13 +88,13 @@ Elasticsearch doesn't have permission to lock memory. This can be granted as fo
RPM and Debian:: RPM and Debian::
Set `MAX_LOCKED_MEMORY` to `unlimited` in the Set `MAX_LOCKED_MEMORY` to `unlimited` in the
<<sysconfig,system configuration file>> (or see below for systems using `systemd`). <<sysconfig,system configuration file>> (or see below for systems using
`systemd`).
Systems using `systemd`:: Systems using `systemd`::
Set `LimitMEMLOCK` to `infinity` in the <<systemd,systemd configuration>>. Set `LimitMEMLOCK` to `infinity` in the <<systemd,systemd configuration>>.
Another possible reason why `mlockall` can fail is that the temporary directory Another possible reason why `mlockall` can fail is that the temporary directory
(usually `/tmp`) is mounted with the `noexec` option. This can be solved by (usually `/tmp`) is mounted with the `noexec` option. This can be solved by
specifying a new temp directory using the `ES_JAVA_OPTS` environment variable: specifying a new temp directory using the `ES_JAVA_OPTS` environment variable:
@ -73,26 +106,3 @@ export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
-------------- --------------
or setting this JVM flag in the jvm.options configuration file. or setting this JVM flag in the jvm.options configuration file.
[[disable-swap-files]]
==== Disable all swap files
The second option is to completely disable swap. Usually Elasticsearch
is the only service running on a box, and its memory usage is controlled
by the JVM options. There should be no need to have swap enabled.
On Linux systems, you can disable swap temporarily
by running: `sudo swapoff -a`. To disable it permanently, you will need
to edit the `/etc/fstab` file and comment out any lines that contain the
word `swap`.
On Windows, the equivalent can be achieved by disabling the paging file entirely
via `System Properties → Advanced → Performance → Advanced → Virtual memory`.
[[swappiness]]
==== Configure `swappiness`
Another option available on Linux systems is to ensure that the sysctl value
`vm.swappiness` is set to `1`. This reduces the kernel's tendency to swap and
should not lead to swapping under normal circumstances, while still allowing
the whole system to swap in emergency conditions.