0
0
Elasticsearchquery~10 mins

Replica management in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Replica management
Create Index
Set Number of Replicas
Index Data
Elasticsearch Copies Data to Replicas
Search Queries Use Primary or Replica
If Node Fails, Replicas Promote to Primary
This flow shows how Elasticsearch manages replicas: creating an index, setting replicas, indexing data, copying to replicas, using replicas for search, and handling node failures.
Execution Sample
Elasticsearch
PUT /my_index
{
  "settings": {
    "number_of_replicas": 1
  }
}
This code creates an index named 'my_index' with 1 replica shard.
Execution Table
StepActionPrimary Shard StateReplica Shard StateResult
1Create index with 1 replicaInitializedInitialized but emptyIndex created with 1 primary and 1 replica shard
2Index document into primaryDocument storedReplica emptyData stored on primary only
3Elasticsearch copies data to replicaDocument storedDocument copiedReplica now has same data
4Search query sentPrimary or Replica usedPrimary or Replica usedSearch results returned from any shard
5Primary node failsUnavailableReplica promoted to primaryCluster recovers with replica as new primary
6Add new replicaPrimary activeNew replica initializingReplica shard created and syncing
7Replica sync completePrimary activeReplica syncedReplica ready for queries
💡 Execution stops after replica sync completes and cluster is stable
Variable Tracker
ShardStartAfter Step 2After Step 3After Step 5Final
Primary ShardInitializedDocument storedDocument storedUnavailable (failed)Recovered (promoted replica)
Replica ShardInitialized but emptyEmptyDocument copiedPromoted to primarySynced and active
Key Moments - 3 Insights
Why is data first stored only on the primary shard before copying to replicas?
Because Elasticsearch writes data to the primary shard first to ensure consistency, then asynchronously copies it to replicas as shown in step 2 and 3 of the execution_table.
What happens to replicas when the primary shard's node fails?
As shown in step 5, a replica shard is promoted to primary to keep the cluster available without data loss.
Can search queries use replica shards?
Yes, step 4 shows that search queries can be served by either primary or replica shards to balance load.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the replica first get the document data?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Replica Shard State' column in execution_table rows for step 3
According to variable_tracker, what is the state of the primary shard after step 5?
ADocument stored
BRecovered (promoted replica)
CUnavailable (failed)
DInitialized
💡 Hint
Look at the 'Primary Shard' row under 'After Step 5' in variable_tracker
If the number_of_replicas is set to 2 instead of 1, how would the execution_table change?
ASearch queries would only use primary shards
BThere would be two replica shard states tracked and synced
CPrimary shard would store twice the data
DReplica shards would not be created
💡 Hint
Think about how replicas are managed and synced as shown in the execution_table and variable_tracker
Concept Snapshot
Replica management in Elasticsearch:
- Create index with 'number_of_replicas' setting
- Data writes go to primary shard first
- Data asynchronously copied to replica shards
- Search queries can use primary or replicas
- If primary fails, replica promoted to primary
- Replicas improve availability and search performance
Full Transcript
Replica management in Elasticsearch involves creating an index with a specified number of replicas. When data is indexed, it is first stored on the primary shard. Then Elasticsearch copies this data to replica shards asynchronously. Search queries can be served by either primary or replica shards to balance load. If the primary shard's node fails, a replica shard is promoted to primary to maintain availability. This process ensures data redundancy and fault tolerance in the cluster.