Problem:
The client faced significant delays in executing Elasticsearch queries within their production environment. A particular query on a simple numeric identifier took over a minute to execute, despite returning only a few hits and a small result set. The Elasticsearch profiler indicated that almost all of this time was spent in the “HighlightPhase.”
One of the documents returned by the query was notably large, with its full JSON data measuring well over a hundred megabytes. Despite this, the query did not involve highlighting or searching this field. The client was operating Elasticsearch version 7.17 on a Linux environment.
The logs, outputs, and configurations revealed no meaningful differences in node configurations and no active monitoring of the cluster.
Process:
Upon reviewing the issue, the expert team initiated a series of investigative steps and recommendations to identify the root cause and potential solutions:
Initial Query Analysis:
The expert team first requested the client to run the same query without highlighting to compare execution times. This step was crucial to determine whether highlighting was indeed the primary bottleneck.
Evaluation of Environment and Configuration:
The expert team reviewed the client’s Elasticsearch setup, including the node configurations and document mappings. They identified that the cluster nodes had varying CPU allocations, which was suboptimal. The cluster also utilized a max heap size of 32GB per node, exceeding the recommended 30GB limit for Elasticsearch to maintain compressed ordinary object pointers (OOP).
Monitoring Recommendations:
When reviewing potential optimization strategies, the team emphasized the importance of monitoring tools such as APM or profiling to collect data on disk usage, network traffic, and other relevant metrics.
Hypotheses Testing:
The query was tested without the highlighting phase. Additionally, the expert team suggested increasing the CPU and RAM allocations on the Elasticsearch nodes.
Test Results:
The tests produced two key results:
- Highlighting Disabled: When the highlighting was disabled, the query execution time dropped significantly to just 1-2 seconds, depending on the node from which it was run.
- Document Retrieval: Fetching the entire contents of the large document took only 3 seconds, indicating that data retrieval and network transmission were not the bottlenecks.
Deep Dive into Shard Configuration:
The team discovered that the index had far more primary shards than its size required. The expert team recommended reducing the number of shards to optimize performance.
Examination of Document Size and Highlighting:
The very large attachment field was identified as a critical factor in the slow query performance. The team suggested chunking this field into smaller pieces or considering other storage solutions, although they recognized that this would involve substantial changes to the application.
Solution:
After thorough analysis and testing, the expert team provided the following solutions:
- Heap Size Adjustment: The team recommended reducing the heap size to 30GB to ensure proper memory management and avoid potential performance degradation due to incorrect heap size allocation.
- Node Scaling and Resource Allocation: The expert team advised doubling the CPU and RAM resources for the Elasticsearch nodes to handle the large documents more efficiently.
- Optimizing Shard Configuration: The team suggested reducing the number of primary shards to a more optimal number to balance the load and improve query performance.
- Disabling Highlighting: The team confirmed that disabling highlighting significantly reduced query execution times, and they recommended continuing this practice for queries that did not require highlighting.
- Document Chunking: For long-term optimization, the team recommended chunking large documents into smaller pieces, although this solution was recognized as potentially complex and requiring significant application modifications.
- Implementation of Monitoring Tools: The expert team emphasized the importance of implementing monitoring tools such as APM to gain insights into disk usage, network traffic, and other performance metrics, which would help in proactively identifying and resolving issues.
Conclusion:
The client’s Elasticsearch query performance issues were primarily attributed to the large size of certain document fields and the inefficient configuration of the Elasticsearch cluster. By disabling highlighting, adjusting the heap size, optimizing the shard configuration, and considering future infrastructure scaling, the client was able to significantly improve query performance. Moving forward, the implementation of monitoring tools and potential application refactoring were recommended to maintain and further enhance system performance.
The expert team’s involvement provided the client with actionable insights and a clear path to resolving their performance challenges, ensuring that their Elasticsearch environment could better handle large datasets and complex queries in the future.