Elasticsearch tuning
This guide summarizes the relevant configurations that allow us to optimize Elasticsearch.
Memory locking
Elasticsearch performs poorly when the system is swapping the memory. It is vitally important to the health of your node that none of the JVM is ever swapped out to disk.
We will set the bootstrap.memory_lock setting to true, so Elasticsearch will lock the process address space into RAM, preventing any Elasticsearch memory from being swapped out.
Step 1: Set bootstrap.memory_lock
Uncomment or add this line to the /etc/elasticsearch/elasticsearch.yml
file:
bootstrap.memory_lock: true
Step 2: Edit limit of system resources
Where to configure systems settings depends on which package and operating system you choose to use for Elasticsearch installation.
In a case where systemd is used, system limits need to be specified via systemd. First, create the folder executing the command:
mkdir -p /etc/systemd/system/elasticsearch.service.d/
, add a file calledelasticsearch.conf
and specify any changes in that file:[Service] LimitMEMLOCK=infinity
In other case, edit the proper file
/etc/sysconfig/elasticsearch
for RPM or/etc/default/elasticsearch
for Debian:MAX_LOCKED_MEMORY=unlimited
Step 3: Limit memory
The previous configuration might cause node instability or even node death (with an OutOfMemory exception) if Elasticsearch tries to allocate more memory than is available. JVM heap limits will help us to define the memory usage and prevent this situation.
There are two rules to apply when setting the Elasticsearch heap size:
No more than 50% of available RAM.
No more than 32 GB.
In addition, you must take into account the memory usage by the operating system, services and software running on the host.
By default, Elasticsearch is configured with a 1 GB heap. You can change the heap size via JVM flags using the /etc/elasticsearch/jvm.options
file:
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms4g
-Xmx4g
Warning
Ensure that the min (Xms) and max (Xmx) sizes are the same, this prevents JVM heap resizing at runtime, a very costly process.
Step 4: Restart Elasticsearch
Finally, restart Elasticsearch service:
For Systemd:
systemctl daemon-reload systemctl restart elasticsearch
For SysV Init:
service elasticsearch restart
After starting Elasticsearch, you can see whether this setting was successfully applied by checking the value of mlockall
in the output of the next request:
curl -XGET 'localhost:9200/_nodes?filter_path=**.mlockall&pretty'
{
"nodes" : {
"sRuGbIQRRfC54wzwIHjJWQ" : {
"process" : {
"mlockall" : true
}
}
}
}
The request has failed when you see the above output have "mlockall" : false
field. You will also see a line with more information in the logs (/var/log/elasticsearch/elasticsearch.log) with the words Unable to lock JVM Memory.
Reference: