ElasticSearch replication
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, distributed search and analytics engine. One of the key features that supports its distributed capabilities is replication. Replication in ElasticSearch serves the primary purpose of high availability and fault tolerance. When data is replicated across multiple nodes, it ensures that the system can continue operating even if some nodes fail. This article delves into the intricacies of ElasticSearch replication, highlighting its technical components, benefits, and potential configuration options.
Understanding ElasticSearch Architecture
Before exploring replication specifics, it's essential to understand ElasticSearch's basic architecture. ElasticSearch stores data in JSON document format. The data is organized into indices, which can be thought of as analogous to a table in the world of relational databases. Each index is, in turn, divided into shards, and each shard can have one or more replicas.
Shards
- Primary Shard: This is the original copy of data.
- Replica Shard: A copy of the primary shard, stored on a different node. By default, an index has one replica, which means there is one copy of the primary shard.
Why Replicate?
Replication primarily ensures two things:
- High Availability: If a node goes down, the replica shard can serve the requests in place of the primary.
- Fault Tolerance: Data is protected against loss since it's stored in more than one physical location.
Configuring Replication
Default Replication
By default, an index is allocated with one primary shard and one replica shard per primary shard. To modify the default setting, you can adjust the number of replicas as needed. This can be done using the following REST API command:
In the command above, my_index is the name of the index, and the number of replicas is increased to two.
Replication Process
- Indexing: When a document is indexed, it's first written to the primary shard. Once the primary shard writes the document successfully, the changes are then asynchronously propagated to the replica shards.
- Confirmation: An operation is considered successful when the primary shard confirms the operation and at least one replica acknowledges it.
- Read Operations: By default, read requests like search and GET can be satisfied by any available shard (primary or replica), depending on which node has the least load. This distribution increases the system's overall throughput.
Asynchronous vs. Synchronous Replication
ElasticSearch employs asynchronous replication for replicating changes to replicas. This means that the primary shard immediately returns after writing data, and only then does the data get replicated to the replicas. The benefit is that this process provides low-latency responses for write operations.
Ensuring Consistency
ElasticSearch offers a consistency model that ensures any acknowledged write will persist and be visible to subsequent reads. This is known as the write consistency level. You could configure the system to wait for a certain number of shards (primary and replicas) to respond before considering a write request successful.
Challenges with Replication
- Network Latency: High network latency can delay the replication process.
- Resource Utilization: Additional storage is required for replica shards.
- Eventual Consistency: With asynchronous replication, there's a slight delay between when a document is indexed in a primary shard and when it's available in the replica shard.
Example Scenario
Consider a cluster with nodes A, B, and C:
- Data is written to node A's primary shard.
- The data is replicated to node B.
- If node A fails, the system can retrieve data from the replica shard on node B, maintaining data availability and consistency.
Key Points Table
| Aspect | Description |
| Primary Shard | Original copy of data. |
| Replica Shard | Copy of the primary shard stored on other nodes. |
| Replication Goal | Ensure high availability and fault tolerance. |
| Configuration | Number of replicas can be set per index using ElasticSearch APIs. |
| Operations | Asynchronous replication for low-latency write operations. |
| Consistency | Achieved by ensuring writes are acknowledged by primary and replicas. |
| Challenges | Network latency, resource usage, and eventual consistency issues. |
Conclusion
In conclusion, ElasticSearch replication is a critical feature for building robust and reliable search and analytics platforms. It enhances data availability and resilience while supporting distributed query execution and processing. By understanding and configuring replication appropriately, organizations can ensure their ElasticSearch deployments are both performant and dependable in the face of node failures and other challenges. As with any system, it's crucial to balance between desired availability, consistency, and resource constraints to achieve optimal system performance.
Related reading
- Electing a new leader in distributed systems
- Elixir Leader Election?
- Embedded Distributed Infinispan Cluster Cache Event Listener Issue After Network Disconnection
- Embedded Redis for Spring Boot
- Enable logical replication on Google Cloud Postgres
- Encrypting the Hadoop Distributed Cache file
- End to end integration test for multiple spring boot applications under Maven
- Entity Listener and caching for distributed system

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.