Hard search elasticsearch

How do the node query cache and the shard request cache work, and how do you write queries that hit them?

The 30 second answer

The node query cache stores which documents match a filter-context clause, per segment, up to 10% of heap by default; term queries and scoring clauses aren't eligible. The shard request cache stores whole responses, by default only for size zero requests like facet counts, keyed by a hash of the entire JSON body and cleared on every refresh. So keep filters in filter context, round dates, build request JSON deterministically, and don't refresh more often than needed.

Could you say this in an interview?

Two different caches inside Elasticsearch.

Node query cacheShard request cache
CachesWhich documents match a filter clauseThe whole shard-level response
ScopePer segment, shared by all shards on the nodePer shard
EligibleClauses in filter context (term queries excluded)By default requests with size=0 (aggregations, counts)
InvalidatedWhen segments change (merges)On every refresh, and on mapping changes
Default size10% of heap, up to 10,000 queriesBounded by a setting, LRU eviction

The docs on the query cache: "The results of queries used in the filter context are cached", "Term queries and queries used outside of a filter context are not eligible for caching", and caching only happens for clauses that recur and for segments that are large enough. On the request cache: "By default, the request cache will only cache the results of search requests where size=0", the cache key is "a hash of the whole JSON body", so "if keys are output in a different order" the key changes, and "requests using now cannot be cached". Results "are invalidated automatically whenever the shard refreshes".

Badly written. Filters in must (not cached), keys in random order per request, now without rounding, and a 1-second refresh on an index that changes constantly:

{ "size": 0,
  "query": { "bool": { "must": [
      { "range": { "created_at": { "gte": "now-7d" } } },
      { "terms": { "category_id": ["dresses", "skirts"] } } ] } },
  "aggs": { "brands": { "terms": { "field": "brand_id" } } } }

Better. Filter context, rounded dates, a stable JSON builder in the search API layer:

{ "size": 0,
  "query": { "bool": { "filter": [
      { "terms": { "category_id": ["dresses", "skirts"] } },
      { "range": { "created_at": { "gte": "now-7d/d" } } } ] } },
  "aggs": { "brands": { "terms": { "field": "brand_id", "size": 30 } } } }

What about application caches? Zalando's outage write-up describes caching popular queries and filter combinations in the query builder and catalog layers, plus dedicated coordinator nodes. It also notes that a longer cache TTL did not save them during the incident. Caches help with normal repeated traffic; unusual or highly varied queries miss every layer.

Trade-offs.

  • A longer refresh_interval keeps request-cache entries valid longer and makes indexing cheaper, at the cost of freshness. eBay's Elasticsearch tuning post makes the same point about refresh and caching.
  • Caching full pages with size > 0 is possible per request (request_cache=true), but per-shopper sorts and personalization make hit rates low.

Alternate interview scenarios

"Facet requests are identical but the request cache hit rate is near zero."

Check refresh frequency (every refresh clears it), whether the JSON is byte-identical (key order, float formatting), and whether now or a per-user parameter sneaks into the body.

"Should price and stock be cached at all?"

Shard caches stay correct because they're cleared on refresh. Application caches in front of search need short TTLs or event-driven invalidation for volatile fields, which the product-search system design page covers.

Sources (4)
  1. Elasticsearch docs: Node query cache settingselastic.co
  2. Elasticsearch docs: Shard request cacheelastic.co
  3. Zalando Engineering: The Day Our Own Queries DoS'ed Us: Inside Zalando Search (December 2025)engineering.zalando.com
  4. eBay Tech Blog: Elasticsearch Performance Tuning Practice at eBayinnovation.ebayinc.com