# Query cache gets invalidated

**URL:** <https://discuss.elastic.co/t/query-cache-gets-invalidated/216032>\
**Category:** Elasticsearch\
**Created:** [January 22, 2020, 10:29am UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032 "2020-01-22T10:29:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 22, 2020, 10:29am UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/1 "2020-01-22T10:29:54Z")

</div>

I am making this query with `request_cache=true`

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": "online"
          }
        },
        {
          "term": {
            "status": "available"
          }
        },
        {
          "term": {
            "status": "admin"
          }
        }
      ]
    }
  }
}

```

I have refresh interval of 1 minute. Even if I make the same query repeatedly, the cache hit count increases and sometimes miss\_count increase. There are no evictions yet. Can someone help what is the issue ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 22, 2020, 2:17pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/2 "2020-01-22T14:17:30Z")

</div>

can you share your full query? Keep in mind that such queries are only cached if `size: 0` is specified. See [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/shard-request-cache.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/shard-request-cache.html)

---

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 22, 2020, 2:29pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/3 "2020-01-22T14:29:17Z")

</div>

@spinscale Do I need to specify `size:0` explicitly ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 22, 2020, 2:31pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/4 "2020-01-22T14:31:04Z")

</div>

yes, otherwise you will retrieve 10 hits by default

---

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 22, 2020, 2:39pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/5 "2020-01-22T14:39:18Z")

</div>

@spinscale Thanks for your reply. Can you please guide me. How to get the hits also with caching enabled. I have checked that we have to specify `size: 0` but I need hits as a primary objective of this query.

Can we change the query a little bit to get hits and also enable caching.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 22, 2020, 2:44pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/6 "2020-01-22T14:44:08Z")

</div>

the shard query cache will not help you then.

However there is a second cache named the node query cache, that can speed up queries and does not need to be activated, as it has some heuristics to figure out when it should be used. See [https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-cache.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-cache.html)

Can you explain why you need caching? Maybe there is the possibility to optimize queries and datamodels (for example not requiring an exact hit count might help as well in Elasticsearch 7.x).

---

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 22, 2020, 2:51pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/7 "2020-01-22T14:51:49Z")

</div>

@spinscale I also think shard query cache cannot be used.

I need caching to make queries faster. Since i have a lot of queries which are same so I thought of enabling the cache. I do have `refresh interval: 1m`

Can you help with any resource to optimize queries and datamodels.  
I actually don't need hit count actually. I need hits and want pagination in them.

---

<div class="post-metadata">

**Author:** ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)\
**Post date:** [January 23, 2020, 5:13am UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/8 "2020-01-23T05:13:34Z")

</div>

> [@royal\_man](#):
>
> { "query": { "bool": { "should": [{ "term": { "status": "online" } }, { "term": { "status": "available" } }, { "term": { "status": "admin" } }] } } }

@royal_man to ur use case talking above, only [file-system-cache](https://www.elastic.co/guide/en/elasticsearch/reference/current/preload-data-to-file-system-cache.html) can help.

Just change or append the index setting, with enough free mem in ur data nodes (to [our-tsdb-use-case](https://discuss.elastic.co/t/can-es-not-to-store-original-keyword-content-but-a-mapping-num-value-4-save-space/215664): 31G on-heap + 96G off-heap / per node):

> index.store.preload: ["nvd", "dvd", "tim", "doc", "dim"]

or more aggressive

> index.store.preload: ["\*"]

And reopen ur index, u will see difference!  
Wish it can help u!

---

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 23, 2020, 12:02pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/9 "2020-01-23T12:02:11Z")

</div>

Thanks @DeeeFOX I will try the idea and reply here about the performance

---

<div class="post-metadata">

**Author:** ![royal\_man](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/royal_man/32/67426_2.png) [@royal\_man](https://discuss.elastic.co/u/royal_man)\
**Post date:** [January 24, 2020, 1:00pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/10 "2020-01-24T13:00:52Z")

</div>

@DeeeFOX  
I have tried the filesystem preload and monitored the RAM cache and but there is no change in it.  
I have ES data node in k8s node (60Gi RAM). Below is the RAM usage graph. I have actually allocated 50Gi to ES node. I think `RAM free` includes the 10Gi RAM that is not allocated.  
What do you think ?

 ![Screen Shot 2020-01-24 at 21.57.42](https://us1.discourse-cdn.com/elastic/original/3X/b/f/bf3921fabc6b589bc9787f1a97336986d7158d69.png)

---

<div class="post-metadata">

**Author:** ![DeeeFOX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/deeefox/32/49566_2.png) [@DeeeFOX](https://discuss.elastic.co/u/DeeeFOX)\
**Post date:** [January 24, 2020, 1:41pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/11 "2020-01-24T13:41:43Z")

</div>

Ahhh, sorry for forgetting to remind u, one more thing u should set to enable the preload settings: [the index store type](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-store.html), by default is "hybridfs", and to our tsdb-use-case:

```
{
  "settings": {
    "index": {
      ...
      "store": {
        "type": "mmapfs",
        "preload": [
          "doc",
          "dvd"
        ]
      },
      ...
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 21, 2020, 1:51pm UTC](https://discuss.elastic.co/t/query-cache-gets-invalidated/216032/12 "2020-02-21T13:51:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
