Caching in Elasticsearch

Andrei Baptista
4 min readMar 28, 2023

--

Caching made an important work in improving query performance and different types of caches are used to store frequently accessed data, which helps to speed up search queries and reduce the load on the cluster. Those types of caches are used to optimize query performance and reduce the load on the cluster. Each cache serves a specific purpose, and understanding how they work can help you fine-tune your search queries to achieve better performance.

Fig 1. How the page cache works
  1. Page Cache: This cache is a cache maintained by the operating system. Is used to store frequently accessed data in memory. What happens is that Elastic leverages the page cache to speed up search queries by caching frequently accessed data — When searches are made, Elastic first check the page cache to see if the required data is already available in memory, if are Elastic will try to retrieve it from the page cache, which is much faster than reading it from disk.
  2. Shard-Level Request Cache: That type is used to cache the results of search requests executed on each shard in a cluster and it’s enabled by default. You can configure it store a number of requests or requests made in a time window. The info of this cache is shared between all the nodes, and it stores the results of the most recent requests executed on each shard. When a new request is executed, Elastic checks the cache to see if the same request has been executed recently on the same shard and if found it then will try to retrieve the results from the cache instead of executing the query again, and will help to improve query performance.
  3. Query Cache: This cache is used to maintain and store all the results of frequently executed queries — Here are somethings that happen when a query is executed when we talk about query cache. I can tell you why we have performance benefits with that, Elastic check if this query was executed before in a certain time window, if this query is found Elastic will try to retrieve all the results from the cache instead of executing the query again, this cache is configured to store queries based on a set of parameters, such as the query type and size of the result set, you can even configure to cache based on search filters used.

Pro Tip: Sometimes, it's interesting to increase the number of shards that you have in a Elasticsearch Cluster, however, when you increase the number of shards in Elasticsearch, you are essentially dividing your data into smaller chunks and spreading it across multiple nodes in the cluster. This can have both positive and negative effects on performance and caching.

On the positive side, having more shards can improve search performance, as queries can be executed in parallel across multiple nodes. It can also improve the overall scalability of your cluster, as you can add more nodes to handle the increased workload.

However, having more shards can also increase the overhead on the cluster, as each shard requires its own resources, such as memory and disk space. This can lead to higher resource utilization and potentially slower query response times. In terms of caching, having more shards can make it more difficult to cache data effectively. This is because caches operate on a per-shard basis, so if you have a large number of shards, you may need to allocate more memory to caching to achieve the same level of performance.

Additionally, when you have more shards, you may experience more cache eviction, where older or less frequently accessed data is removed from the cache to make room for new data. This can result in lower cache hit rates, which can negatively impact query performance — Increasing the number of shards in Elasticsearch can have both positive and negative effects on performance and caching. It’s important to carefully consider your requirements and use cases when deciding how many shards to use, and to monitor and optimize your cluster accordingly to achieve the best possible performance.

More details about the implementation of Page Cache

You can found here the implementation of the Page Cache

This class is a type of directory management implementation in that combines both memory-based and disk-based storage. It is used to store Lucene index files and provides a balance between fast memory-based access and the ability to store large amounts of data on disk. Basically this class help us to improve the performance of search queries by caching frequently accessed index files in memory, while still allowing the index to be stored on disk for persistence and to accommodate larger data sets. It does this by using a two-tiered approach, where frequently accessed index files are cached in memory, and less frequently accessed files are stored on disk.

This class also provides fault-tolerance and resilience to Elasticsearch clusters because it is designed to handle failures gracefully and can recover data from disk in the event of a node failure or other issues.

--

--

No responses yet