Quick revision
The short answer
An index in Elasticsearch or OpenSearch is split into primary shards, each a complete Lucene index holding part of the documents, and each primary can have replicas, full copies on other nodes that take over on failure and serve reads. A document goes to the shard given by a hash of its routing value, the id by default, modulo the number of primary shards, so changing that number would send existing documents' lookups to the wrong shards. That is why we use _split, _shrink or a reindex to change primaries, while the replica count can be changed at any time.
What to remember
In this answer, we saw that an index is split into primary shards, each a Lucene index, and replicas are copies that provide failover and read capacity. Documents are routed by a hash of the routing value, the id by default, modulo the number of primaries, so changing that number would break every lookup; we use _split, _shrink or a reindex and switch an alias instead. Replica count is dynamic, and a replica is never placed on the same node as its primary.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An index in Elasticsearch or OpenSearch is split into primary shards, each a complete Lucene index holding part of the documents, and each primary can have replicas, full copies on other nodes that take over on failure and serve reads. A document goes to the shard given by a hash of its routing value, the id by default, modulo the number of primary shards, so changing that number would send existing documents' lookups to the wrong shards. That is why we use _split, _shrink or a reindex to change primaries, while the replica count can be changed at any time.
Why this question is asked
Shards are the unit of everything in a search cluster: storage, parallelism, recovery and failure. Choosing the shard count is one of the few decisions we cannot simply edit later, and many production problems come from getting it wrong. Interviewers ask this question to check that we understand where documents live and why that fixes the number of primaries. In this answer, we will recap primaries and replicas, follow how a document is routed, see why the primary count is fixed, look at _split, _shrink and reindexing, and finish with how replicas are placed.
1A Quick Recap of Primaries and Replicas
When we create an index, we choose a number of primary shards, for example 3. Each primary shard is a self-contained Lucene index holding about a third of the documents. Splitting the data this way lets an index grow beyond one machine and lets a search run on several shards in parallel. Since Elasticsearch 7, and in OpenSearch, the default is 1 primary shard.
Each primary can have replica shards: complete copies of it. With number_of_replicas set to 1, every primary has one copy, so the index has 3 primaries and 3 replicas, 6 shards in total. Replicas do two jobs. If a node holding a primary fails, a replica is promoted to primary and no data is lost. And searches can be served by either copy, so replicas add read capacity. Writes always go to the primary first, which then forwards them to its replicas.
2How a Document Finds Its Shard
When we index a document with id 1001, the cluster must decide which primary shard stores it, and when we later get id 1001 it must find that shard again without asking every shard. It does this with a formula. It takes the routing value, which is the document id unless we provide a custom routing value, hashes it with Murmur3, and takes the result modulo the number of primary shards. In simplified form, shard = hash(routing) % number_of_primary_shards. The real formula also involves the routing_num_shards setting, which exists to make splitting possible, but the idea is the same.
Notice that the get request does not search; it recomputes the same formula and goes straight to one shard. The same is true for updates and deletes by id. Routing is what makes per-id operations cheap.
3Why the Primary Count Cannot Simply Change
Now imagine we could change number_of_shards from 3 to 4 in place. The formula would become hash(routing) % 4. Document 1001, whose hash gives 2 when divided by 3, might give 1 when divided by 4. Its data still sits on shard 2, but every get, update and delete would now look on shard 1 and find nothing. To be correct, the cluster would have to move most documents to new shards, which is the same work as building a new index.
So Elasticsearch and OpenSearch make the primary count fixed at creation and offer explicit operations that do the move in a controlled way. This also tells us something about design: we choose the primary count from the expected index size, aiming for shards of tens of gigabytes, and we plan for changes through new indices and aliases rather than editing.
| Document id | hash % 3 (stored on) | hash % 4 (would look on) | Found? |
|---|---|---|---|
| 1001 | shard 2 | shard 1 | no |
| 1002 | shard 0 | shard 0 | yes, by luck |
| 1003 | shard 1 | shard 3 | no |
| 1004 | shard 2 | shard 2 | yes, by luck |
4Changing Primaries: _split, _shrink and Reindex
The _split API creates a new index with more primaries, where the new count is a multiple of the old one, for example from 3 to 6 or 12. Because each old shard splits cleanly into several new ones, the operation can reuse segment files through hard links instead of re-indexing. The source index must be made read-only first, by setting index.blocks.write to true. The _shrink API goes the other way, to a factor of the old count, for example from 6 to 3 or 1, and needs a copy of every shard on one node and the index read-only.
For any other change, such as 3 to 5 primaries, or when the mapping also changes, we reindex: create a new index with the right settings and copy the documents with the _reindex API or by re-feeding from the source system. In all three cases the result is a new index, so applications should always read through an alias, which we switch to the new index when it is ready.
5Replicas Can Change Any Time, and Never Share a Node With Their Primary
Replicas do not affect routing, because a replica holds exactly the same documents as its primary. So number_of_replicas is a dynamic setting: we can raise it to 2 before a busy sale to add read capacity, or set it to 0 during a bulk load and back to 1 afterwards, and the cluster copies or removes shard copies in the background.
| Node | Shards held |
|---|---|
| node-a | P0, R2 |
| node-b | P1, R0 |
| node-c | P2, R1 |
The cluster never allocates a replica on the same node as its primary, or two copies of the same shard on one node, because a single node failure would then lose both copies. A consequence surprises people on small clusters: on a single-node cluster, replicas can never be assigned, so the index health is yellow. With 3 nodes and 2 replicas, each shard has a copy on every node. Settings such as shard allocation awareness extend the same idea to racks or availability zones, keeping copies in different zones.
6Follow-up questions
How many primary shards should a new index have?
Start from the expected size and aim for shards of roughly 10 to 50 GB, a common guideline that you should check against your vendor's current advice. A 60 GB index fits in 2 or 3 primaries. Avoid creating many tiny shards just in case, because each shard has a fixed overhead; for growing data, use rollover to create new indices instead of over-sharding one.
Do replicas make indexing faster?
No, they make it slower, because each write must also be applied on every replica. They make searches more available and can add read throughput. That is why bulk loads often run with zero replicas and add them afterwards.
What does custom routing give us?
With a custom routing value such as a tenant id, all documents with that value go to one shard, and a search that passes the same routing value only queries that shard instead of all of them. The cost is that a large routing value can make one shard much bigger than the others, and gets by id must pass the routing value too.
7Conclusion
In this answer, we saw that an index is split into primary shards, each a Lucene index, and replicas are copies that provide failover and read capacity. Documents are routed by a hash of the routing value, the id by default, modulo the number of primaries, so changing that number would break every lookup; we use _split, _shrink or a reindex and switch an alias instead. Replica count is dynamic, and a replica is never placed on the same node as its primary.
Open with
An index is divided into primary shards, and each primary shard is a full Lucene index holding a part of the documents. Each primary can have replicas, which are complete copies on other nodes; they take over if the primary's node fails and they also serve searches, so they add read capacity.
Then, if they want more
Writes go to the primary and are forwarded to the replicas. The cluster decides where a document lives with a routing formula: it hashes the routing value, which is the document id by default, and takes it modulo the number of primary shards. A get by id recomputes that and goes straight to one shard.
That is exactly why the primary count cannot just be changed: with a different modulus, most documents would be looked up on a shard that does not hold them, so all of them would have to move.
Instead we use the split API to multiply the primaries, the shrink API to reduce them to a factor, or a reindex into a new index for any other change, after making the source read-only, and we switch an alias so applications do not notice.
Replicas are different: they do not affect routing, so the replica count is a dynamic setting we can change at any time. And the cluster never puts a replica on the same node as its primary, so one node failure cannot lose both copies, which is why a single-node cluster stays yellow with replicas configured.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Elasticsearch documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are primary shards and replicas in Elasticsearch or OpenSearch, and why can you not...": https://scrivio-interview-library.netlify.app/databases/search-engines/43cc8804b7/