Sub topic 10
Scaling with Shards and Replicas
This chapter assumes you know the basics of horizontal scaling and replication, and that you have seen an index and its mapping (covered earlier in this tutorial). Here you will learn how a search service spreads one index across machines, and which settings you need to choose.
Why one index gets split
A single machine has limits on disk, memory, and CPU. A large product index may outgrow one node, or a high query rate may saturate it. Search engines such as Elasticsearch and OpenSearch solve both problems with two mechanisms:
- Shards split the index's documents into parts. Each shard is a complete, independent inverted index. This spreads data and indexing work.
- Replicas are copies of a shard. They give failover and extra read capacity.
A primary shard receives writes first. Its replica shards copy those writes and can answer queries.
How a query moves through shards
Documents are assigned to a primary shard by hashing the document ID (or a routing key). A search must check every shard, because any shard might hold matching documents.
For each query, the coordinating node picks one copy (primary or replica) of every shard. Each shard returns its top matches, and the coordinator merges them into a final list. Adding replicas lets more queries run in parallel. Adding shards lets each query use more machines, but it also adds more fan-out work per query.
Choosing the settings
In Elasticsearch you set the shard count when you create the index:
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}This gives 3 primaries and 3 replicas, so 6 shard copies in total. The engine never places a replica on the same node as its primary, so the cluster needs at least two nodes for the replicas to be useful.
The two settings behave differently:
| Setting | Can change after creation? | Affects | Main cost |
|---|---|---|---|
| Number of shards | No, not without reindexing or a split or shrink operation | Data capacity, write throughput | More fan-out and per-shard overhead |
| Number of replicas | Yes, at any time | Read throughput, availability | More disk and more indexing work |
Because the shard count is hard to change, choose it deliberately. As a rough guide, many teams target shards of tens of gigabytes each. Too many tiny shards waste memory and slow queries. Too few large shards make recovery and rebalancing slow. The next chapters on reindexing cover how to change the count later.
Common errors
- Oversharding. Creating 50 shards for a 2 GB index adds overhead and no benefit.
- Zero replicas in production. If a node fails, its primary shards are unavailable, and data may be lost if the disk is gone.
- Hot shards. A custom routing key, such as a tenant ID, can send most traffic to one shard. Check that the key spreads load evenly.
- Expecting replicas to speed up writes. Every write must be applied to the primary and all replicas, so more replicas make indexing slightly more expensive.
- Deep pagination. Asking for page 1,000 forces every shard to return and sort thousands of results. Limit page depth.
Design decision
Start from your needs. If the data fits comfortably on one node and the query rate is moderate, use one or a few shards with at least one replica. Add replicas first when queries are the bottleneck. Add shards only when the data or write volume demands it, and plan for that growth before you create the index.
Exercise
Your search index holds 20 GB and receives many more reads than writes. One node is overloaded by query traffic, and you need to survive the loss of a node. Would you raise the number of shards or the number of replicas, and why?
Show answer
Raise the number of replicas. Replicas add read capacity and survive node loss, and you can change the replica count without rebuilding the index. The data is small, so more shards would only add fan-out overhead and could not be changed later without reindexing.