Elastic Search how to move a primary shard?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Elasticsearch is a powerful open-source search and analytics engine built on Apache Lucene that is widely used for full-text search, structured search, analytics, and all types of business intelligence needs. Managing Elasticsearch involves various operations including handling shards of an index. Shards are the horizontal partitions in an Elasticsearch index, and they come in two types: primary and replica. Each document within an index belongs to a single primary shard.
Understanding Shards in Elasticsearch
Before discussing the movement of a primary shard in Elasticsearch, it's important to understand what shards are and why they are crucial. An index can be divided into multiple shards to enhance performance and ensure distributed data processing. Each primary shard can have zero or more replicas (copies), which provide data redundancy and capacity to handle read requests.
How to Move a Primary Shard in Elasticsearch
In Elasticsearch, moving a primary shard from one node to another might be necessary for various reasons like proactive maintenance, handling server failure, or balancing the cluster load. Such operations are typically managed by Elasticsearch itself, based on the cluster configuration and policies. However, administrators can manually intervene when specific, custom changes are required.
Cluster Settings
Elasticsearch uses a process called "shard allocation" to control where shards are placed within the cluster. Shard allocation is influenced by several settings in elasticsearch.yml or dynamically updated via cluster API.
Disable Shard Allocation
Initially, to prevent Elasticsearch from performing its own shard rebalancing during your manual relocation process, disable shard allocation using the Cluster Update Settings API:
Moving the Primary Shard
To explicitly move a primary shard, you first need to identify the shard and its current location. You can find this information using the _cat/shards API:
After identifying the shard, use the Cluster Reroute API to move it to a different node. Suppose you want to move the primary shard of index myindex from its current node to a target node:
Enable Shard Allocation
Once the move is complete, re-enable shard routing to allow Elasticsearch to perform automatic balancing again:
Monitoring and Considerations
After moving a shard, monitor the cluster health and shard relocation progress. Use the Cluster Health and Shard Allocation APIs:
Properly monitor the node's load and ensure there are no unforeseen impacts on the cluster's performance.
Summary Table: Key Points on Moving Primary Shards
| Aspect | Details |
| Disabling Shard Allocation | Ensures no automatic rebalancing during manual intervention. |
| Cluster Reroute API | Used to manually move a shard between nodes. |
| Shard Identification | Check _cat/shards for current shard status and location. |
| Enabling Shard Allocation | Allows the cluster to resume normal balancing after intervention. |
Additional Tips
- Always plan shard movements during low-traffic periods to mitigate potential performance impacts.
- Consider the underlying physical hardware and network capacity when moving shards to prevent overloading nodes.
- Regularly update and review your Elasticsearch version and plugins for compatibility and performance optimizations.
Conclusion
Moving a primary shard in Elasticsearch is a significant manual intervention that should be handled carefully to maintain cluster stability and performance. By following proper procedures and monitoring the affected systems closely, administrators can ensure a smooth transition and continued service reliability.
Related reading
- Elasticsearch and CAP Theorem
- ElasticSearch constant_score query vs function_score query
- Elasticsearch Dynamic Field Mapping and JSON Dot Notation
- ElasticSearch Java API asynchronous writing
- elasticsearch v.s. MongoDB for filtering application
- Embedded Postgres for Spring Boot Tests
- Embedded Redis for Spring Boot
- EmbeddedCassandra Cannot run unit tests

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.