Sharding a search index: scatter, gather and merge
How a search index is split across machines, why queries fan out to every shard, how top-k results are merged, and what goes wrong with deep paging and uneven shards.
On the roadmap:Staff Engineer · step 1: Search and relevance
Lesson 5 of 10 · 11 min read · quiz · exercise
Why shard
One machine cannot hold or search a very large index within the latency budget. Sharding splits the index into pieces, each on its own machine, so storage and work are spread out. Replicas copy each shard for availability and extra read capacity.
Before: one big index
After: sharded, scatter and gather
What changed
- Storage and work are spread over machines
- Each shard returns its own top results to be merged
- The slowest shard decides how fast the query is
Two ways to partition
- Document-partitioned (local index): each shard indexes a subset of the documents and holds a complete inverted index for them. Writes touch one shard. Every query must visit every shard. This is what Elasticsearch and Solr do.
- Term-partitioned (global index): each shard holds the postings for a subset of terms. A query touches only the shards for its terms, but a write touches many shards and long posting lists must be combined across machines. It is rare in practice.
Figure SDA-SAG-05-1 · Scatter, gather and merge · © forcsguy.com
Decisions that matter
- Shard size and count. Too many tiny shards waste memory and add fan-out; too few huge shards recover slowly and cannot spread load. Many teams aim for tens of gigabytes per shard. The primary shard count is hard to change later, so plan for growth or use time-based indexes.
- Routing. By default a document goes to a shard by hashing its id, which spreads data evenly. Routing by tenant or user keeps one customer's data together so queries can hit a single shard, at the risk of uneven shards.
- Tail latency. A query is as slow as its slowest shard. Use replicas, timeouts, and hedged requests (send to a second replica if the first is slow).
- Deep pagination. Page N needs the first N x pageSize results from every shard. Use cursors (search-after) instead of large offsets.
- Scoring across shards. Each shard computes statistics such as IDF from its own documents. Even, large shards make the difference small; otherwise gather global statistics first.
- Rebalancing and reindexing. Plan how to add capacity without downtime, usually by indexing into a new index and switching an alias.
Figure SDA-SAG-05-2 · Time-based indexes · © forcsguy.com
Keeping the index fresh
Documents usually live in a primary database and are copied to the search index. Use a change stream or an outbox so updates are not lost, accept that search is eventually consistent, and keep a way to rebuild the whole index from the source of truth.
Real-world reading
Related reading
Check your understanding
Question 1 of 3
0 correct so far
In a document-partitioned index, what happens to a query?
Go deeper
- Scalability and resilience: clusters, nodes and shards by Elastic: how a production engine thinks about shards and replicas
- Shards and indexing data in SolrCloud by Apache Solr: routing documents to shards
- Designing Data-Intensive Applications, chapter on partitioning by Martin Kleppmann: document versus term partitioning
Topics covered in this lesson
Practise it: One shard is not enough
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
The search tier is saturated: 60% of 3,000 requests per second are queries, and one search instance cannot hold them.
Goal: Under 1% errors and a slowest 5% under 300 ms.
Select the Search cluster and look at its instances.
Try it yourself
You must index 2 billion documents of 2 KB each (about 4 TB of text) and serve 500 queries per second with a 100 ms target. Sketch the shard and replica plan and name two risks.
Click what a strong design here would need:
More in Real systems
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.