Elasticsearch Elasticsearch Compression

By Opster Expert Team - May 2023

Updated: May 22, 2023

| 2 min read

Elasticsearch Compression: Best Practices and Techniques

Elasticsearch, as a distributed search and analytics engine, handles large volumes of data. To optimize storage and network usage, Elasticsearch provides various compression techniques.

This article will discuss the best practices and techniques for Elasticsearch compression, focusing on index compression, source compression, and response compression.

1. Index Compression

Index compression is essential for reducing the storage space required for your indices. Elasticsearch offers two index compression algorithms: LZ4 and DEFLATE. Both algorithms have their pros and cons, and choosing the right one depends on your use case.

LZ4:

  • Faster compression and decompression
  • Lower compression ratio
  • Suitable for use cases where query performance is critical

DEFLATE:

  • Slower compression and decompression
  • Higher compression ratio
  • Suitable for use cases where storage space is a priority

To set the index compression algorithm, use the following index setting:

PUT /my_index/_settings
{
"index.codec": "best_compression"
}

The “best_compression” setting uses the DEFLATE algorithm. To use LZ4, set the value to “default”.

2. Response Compression

Response compression reduces the size of the data sent over the network when querying Elasticsearch. This can help improve query performance, especially in high-latency environments. Elasticsearch supports HTTP compression using the gzip algorithm.

To enable response compression, add the following lines to your Elasticsearch configuration file (elasticsearch.yml):

http.compression: true
http.compression_level: 3

The “http.compression_level” setting controls the compression level, with a range of 1 (fastest, least compression) to 9 (slowest, most compression). The default value is 3, which provides a good balance between compression and performance.

In addition to enabling response compression on the Elasticsearch server, you also need to include the “Accept-Encoding” header in your client requests:

curl -XGET https://localhost:9200/my_index/_search -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/json' -d '{
"query": {
"match_all": {}
}
}'

By implementing these compression techniques, you can optimize your Elasticsearch cluster’s storage and network usage, leading to improved query performance and reduced costs. Always consider your specific use case and requirements when choosing the appropriate compression settings.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?