Advanced Elasticsearch configuration

Advanced Elasticsearch configuration

If you are running into issues with Elasticsearch this guide provides some useful configurations you can add to your server to help with stability.

As Elasticsearch is Java based it can be very memory intensive. We would therefore recommend as bigger package as possible for sites using Elasticsearch due to its memory requirements. For this reason our recommendation for running Magento 2 and Elasticsearch is our Oxygen package and above.

Setting max memory for Elasticsearch

If you are running into issues with the Elasticsearch service stopping due to memory then it is possible to add memory limits to the Elasticsearch config file to see if it helps.

To do this, first create the following file via SSH on the server:

nano /etc/elasticsearch/jvm.options.d/custom.options

Then add the below to the file which will set the limit to 4gb:

-Xms4g
-Xmx4g

For example:

Xms represents the initial size of total heap space

Xmx represents the maximum size of total heap space

-Xms4g
-Xmx4g

the settings shipped with ES 5 were: -Xms2g

the settings shipped with ES 5 were: -Xmx2g

Then finally restart Elasticsearch with:

systemctl restart elasticsearch

Please note you will need to be logged in as the servers root SSH user to add the config.

Enabling Elasticsearch auto restart

It's possible to configure Elasticsearch to auto restart in the event that the service is killed on the server. It's recommended to use this setting with caution as Elasticsearch being killed could be a sign the server does now have enough memory.

You can edit Elasticsearch service unit file by using the following command:

systemctl edit elasticsearch.service

This command will create a file located in the following location on the server:

/etc/systemd/system/elasticsearch.service.d/override.conf

Add the following lines in the unit file to enable auto restart:

[Service]
Restart=always

Refresh the unit file using command:

systemctl daemon-reload

Finally check the changes using command:

systemctl cat elasticsearch.service