Elasticsearch high CPU usage caused by frequent refresh operations - Common Causes & Fixes

Brief Explanation

This error occurs when Elasticsearch experiences high CPU usage due to an excessive number of refresh operations. Refresh operations in Elasticsearch make recent changes to the index visible for search, but they can be resource-intensive if performed too frequently.

Impact

High CPU usage caused by frequent refresh operations can significantly impact the overall performance of your Elasticsearch cluster. It may lead to:

  • Slower query response times
  • Reduced indexing throughput
  • Potential instability of the cluster
  • Increased resource consumption, potentially leading to higher operational costs

Common Causes

  1. Low refresh_interval setting
  2. High indexing rate with default refresh settings
  3. Too many indices with default refresh settings
  4. Poorly optimized search queries triggering frequent refreshes
  5. Misconfigurated index templates or index settings

Troubleshooting and Resolution Steps

  1. Identify the affected indices: Use the _cat/indices API to list indices and their refresh intervals:

    GET /_cat/indices?v&h=index,refresh.interval
    
  2. Adjust the refresh_interval setting: Increase the refresh interval for affected indices:

    PUT /your_index/_settings
    {
      "index": {
        "refresh_interval": "30s"
      }
    }
    
  3. Monitor CPU usage: Use Elasticsearch's monitoring features or external monitoring tools to track CPU usage before and after changes.

  4. Optimize indexing:

    • Use bulk indexing operations
    • Increase the index.translog.flush_threshold_size setting
    • Consider using the ?refresh=false parameter for non-time-critical indexing operations
  5. Review and optimize search queries: Ensure that search queries are not unnecessarily triggering refresh operations.

  6. Adjust index templates: Update index templates to include optimized refresh settings for new indices:

    PUT _template/my_template
    {
      "index_patterns": ["*"],
      "settings": {
        "index": {
          "refresh_interval": "30s"
        }
      }
    }
    
  7. Consider using force-merge: For read-heavy indices, use the force-merge API to reduce segment count:

    POST /your_index/_forcemerge
    

Best Practices

  • Regularly monitor your cluster's performance and resource usage
  • Balance refresh rate with your application's real-time requirements
  • Use index lifecycle management (ILM) to automate index optimization
  • Implement a robust monitoring and alerting system for early detection of performance issues

Frequently Asked Questions

Q: How does changing the refresh interval affect search results?
A: Increasing the refresh interval means that new documents or updates will take longer to become visible in search results. This trade-off can significantly improve performance for write-heavy workloads.

Q: Can I set different refresh intervals for different indices?
A: Yes, you can set different refresh intervals for each index based on its specific requirements and usage patterns.

Q: How do I determine the optimal refresh interval for my use case?
A: The optimal refresh interval depends on your specific use case. Start with a higher value (e.g., 30s) and gradually decrease it while monitoring performance until you find the right balance between real-time visibility and CPU usage.

Q: Are there any downsides to setting a very high refresh interval?
A: Setting a very high refresh interval can lead to delayed visibility of new data in search results and potentially larger refresh operations when they do occur. It may also increase memory usage as more data accumulates between refreshes.

Q: How can I temporarily disable refreshes during bulk indexing operations?
A: You can set refresh_interval to -1 to disable automatic refreshes, perform your bulk indexing, and then restore the original refresh interval. Remember to manually refresh the index after bulk indexing if needed.

Pulse - Elasticsearch Operations Done Right
Free Health Assessment

Need more help with your cluster?

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.