What is the difference between a primary shard and a replica shard in Elasticsearch?
Understand the difference between primary and replica shards in Elasticsearch: writes, high availability, read scaling, failover, and configuration examples.
Expected Interview Answer
A primary shard is the original shard that owns and indexes a portion of an index's documents, while a replica shard is a copy of a primary that provides redundancy and extra read capacity. Every write goes to the primary first and is then replicated to its replicas.
The number of primary shards is fixed at index creation because document routing depends on it, but the number of replicas can be changed at any time. Elasticsearch never places a replica on the same node as its primary, so a node failure does not lose data — a replica is promoted to primary. Both primaries and replicas can serve search requests, so adding replicas increases read throughput and availability, though it also increases storage and indexing overhead.
- Replicas provide high availability if a node fails
- Replicas add read and search throughput
- Primary shard count is fixed; replica count is adjustable
- Automatic replica-to-primary promotion on failure
- Data safety through copies on different nodes
AI Mentor Explanation
The primary shard is like the official scorer whose scorebook is the authoritative record of an innings. A replica shard is a second scorer copying every entry as it happens, seated in a different part of the ground. If the official scorer is unavailable, the backup's book is promoted to official so nothing is lost, and either scorer can answer a query about the score.
Step-by-Step Explanation
Step 1
Set primaries and replicas
At index creation choose number_of_shards (primaries, fixed) and number_of_replicas (copies per primary, adjustable later).
Step 2
Write to the primary
An indexing request is routed to the correct primary shard, which validates and writes the document first.
Step 3
Replicate the write
The primary forwards the operation to each of its replica shards, which apply it to stay in sync before the write is acknowledged.
Step 4
Distribute replicas safely
Elasticsearch places each replica on a different node than its primary so a single node loss never removes all copies of a shard.
Step 5
Serve reads and handle failure
Both primaries and replicas answer searches; if a primary's node fails, a replica is promoted to primary automatically.
What Interviewer Expects
- Clear distinction between primary and replica roles
- Knowledge that writes go to the primary first
- Understanding replicas provide HA and read scaling
- Awareness that primary count is fixed, replica count is not
- Knowledge that replicas live on different nodes and can be promoted
Common Mistakes
- Thinking writes can go directly to a replica
- Believing the number of primary shards can be changed live
- Placing replicas on the same node as their primary
- Assuming replicas do not help search performance
- Confusing replica shards with snapshots or backups
Best Answer (HR Friendly)
“A primary shard is the original copy of a slice of data that all new writes go to, and a replica shard is a duplicate of it kept on another server. Replicas keep the system running if a machine fails and help handle more search traffic, which is why replicas can be added or removed while the primaries stay fixed.”
Code Example
PUT /orders
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
PUT /orders/_settings
{
"number_of_replicas": 2
}Follow-up Questions
- What happens when the node holding a primary shard fails?
- Can you change the number of replica shards after index creation?
- Why can't a replica be placed on the same node as its primary?
- How do replicas affect indexing performance?
- How is a replica shard different from a snapshot backup?
MCQ Practice
1. Where does an indexing (write) request go first?
Writes are routed to the primary shard, which applies the change and then replicates it to its replica shards.
2. Which shard count can be changed after an index is created?
Replica count is adjustable at any time via settings, while primary shard count is fixed because routing depends on it.
3. What happens when a node holding a primary shard fails?
Elasticsearch promotes an in-sync replica on another node to primary, so the data remains available with no loss.
Flash Cards
What is a primary shard? — The original shard that owns a portion of documents and receives all writes first.
What is a replica shard? — A copy of a primary shard providing redundancy and extra read capacity, placed on a different node.
Which shard count is fixed after creation? — Primary shard count is fixed; replica count can be changed anytime.
What happens on primary node failure? — An in-sync replica is automatically promoted to primary, avoiding data loss.