Search and Elasticsearch learning path Level 3 This level · 9 of 15
Hard search elasticsearch

How do routing and shard count affect how many shards a query touches?

The 30 second answer

By default a search goes to one copy of every shard, so each query fans out to all shards and each runs on one thread. Custom routing sends documents with the same key to one shard, so a query that knows the key hits one shard instead of all; the risk is hot or uneven shards, which routing_partition_size softens. Splitting by market into separate indices or clusters removes a filter and isolates load.

Could you say this in an interview?

From the basics. A document goes to a shard by shard_num = hash(_routing) % num_primary_shards, and _routing defaults to the document ID. So any query that isn't by ID must ask every shard. The docs: "Each shard runs the search on a single CPU thread", and "searches across a large number of shards can deplete a node's search thread pool". Every shard also has fixed memory and CPU overhead, so "a small set of large shards uses fewer resources than many small shards". The docs give general targets of shard sizes between 10GB and 50GB and fewer than 200 million documents per shard.

Custom routing. If most queries include a key, route by it. The docs: "Instead of having to fan out a search request to all the shards in an index, the request can be sent to just the shard that matches the specific routing value". eBay's tuning post gives the example of orders routed by buyer ID, so nearly all queries "can be completed within the shard matching the routing".

Badly written. A seller dashboard that asks every shard for one seller's products on every page view:

GET offers/_search
{ "query": { "bool": { "filter": [ { "term": { "merchant_id": "m-4711" } } ] } } }

Better. Index and search with the merchant as the routing key; keep the filter, since other merchants share the shard:

PUT offers/_doc/o-1?routing=m-4711
{ "merchant_id": "m-4711", "sku": "SKU-1", "price": 4995 }

GET offers/_search?routing=m-4711
{ "query": { "bool": { "filter": [ { "term": { "merchant_id": "m-4711" } } ] } } }

Hot spots. One very large merchant makes its shard much bigger and busier than others. index.routing_partition_size spreads one routing value over several shards; the docs say that as it grows, data becomes more evenly distributed "at the expense of having to search more shards per request". It's set at index creation.

Splitting by market. A filter on market that every query carries is a candidate for separate indices or clusters. eBay's post recommends splitting by region so the filter clause can be removed. Zalando's outage write-up describes market grouping across clusters, and that "market grouping isolation worked as designed": the overloaded cluster served two markets and the others were fine.

Trade-offs.

  • Routing fixes fan-out for queries that know the key and does nothing for the others; cross-key queries still hit every shard.
  • Shard count is fixed at creation (changing it needs split, shrink or reindex). Choose it from data size and query concurrency, not by habit.
  • Replicas add read throughput because each search uses one copy of each shard, chosen by adaptive replica selection by default.

Alternate interview scenarios

"We have 200 small shards per index and search latency is high under load."

Oversharding: each query creates 200 shard-level tasks. Shrink or reindex into fewer, larger shards and measure.

"Search requests for one market slow down another market."

Isolate by index and node allocation, or by cluster, so a hot market's load can't starve the other's thread pools.

Sources (5)
  1. Elasticsearch docs: _routing field (custom routing and routing_partition_size)elastic.co
  2. Elasticsearch docs: Size your shardselastic.co
  3. Elasticsearch docs: Search shard routing (adaptive replica selection)elastic.co
  4. eBay Tech Blog: Elasticsearch Performance Tuning Practice at eBayinnovation.ebayinc.com
  5. Zalando Engineering: The Day Our Own Queries DoS'ed Us: Inside Zalando Search (December 2025)engineering.zalando.com
Previous
Next