Elasticsearch is designed to be a distributed system that can handle a large amount of data and provide high availability. One of the key features that enable this is the concept of index replication, which is controlled by the number_of_replicas
setting. This article will delve into the details of this setting, its implications, and how to properly configure it.
The role of replicas in Elasticsearch
In Elasticsearch, an index is a collection of documents that are partitioned across multiple primary shards. Each primary shard is a self-contained Apache Lucene index, and the documents within an index are distributed among all primary shards. To ensure high availability and data redundancy, Elasticsearch allows each shard to have one or more copies, known as replicas.
The number_of_replicas
setting controls the number of replica shards (copies) that Elasticsearch creates for each primary shard in an index. By default, Elasticsearch creates one replica for each primary shard, but this can be changed according to the requirements of your system.
Configuring the number_of_replicas
The number_of_replicas
setting can be configured at the time of index creation or updated later. Here’s how you can set it during index creation:
PUT /my_index
{
"settings": {
"number_of_replicas": 2
}
}
In this example, Elasticsearch will create two replicas for each primary shard in the my_index
index.
To update the number_of_replicas
setting for an existing index, you can use the _settings
API:
PUT /my_index/_settings
{
"number_of_replicas": 3
}
This command will update the my_index
index to have three replicas for each primary shard.
Implications of the number_of_replicas setting
The number_of_replicas
setting has a significant impact on the performance and resilience of your Elasticsearch cluster. Here are some key points to consider:
- Data Redundancy and Availability: Increasing the
number_of_replicas
enhances the availability of your data by creating more copies of each shard. If a node fails, Elasticsearch can still serve data from the replica shards on the remaining nodes. - Search Performance: Replica shards can serve read requests, so having more replicas can improve search performance by distributing the load across more shards.
- Write Performance: However, each write operation must be performed on every copy of a shard. Therefore, a higher
number_of_replicas
can slow down indexing performance as it increases the number of operations that must be performed for each write. - Storage Requirements: More replicas mean more storage space. You should ensure that your cluster has enough capacity to store the additional replicas.
- Resilience to Node Failure: The
number_of_replicas
should be set considering the number of nodes in your cluster. If thenumber_of_replicas
is equal to or greater than the number of nodes, your cluster can tolerate the failure of multiple nodes without data loss.
Best practices for setting number_of_replicas
The optimal number_of_replicas
setting depends on the specific requirements of your system. However, here are some general best practices:
- For a single-node cluster,
number_of_replicas
should be set to 0, as there are no other nodes to hold replicas. - For a multi-node cluster,
number_of_replicas
should be set to at least 1 to ensure data redundancy and high availability. - If search performance is a priority, consider increasing the
number_of_replicas
. However, keep in mind the trade-off with write performance and storage requirements. - Always ensure that your cluster has enough capacity to store the additional replicas.
Ready to try this out on your own? Start a free trial.
Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!
Related content

May 20, 2025
Elasticsearch in Javascript the proper way, part II
Reviewing production best practices and explaining how to run the Elasticsearch Node.js client in Serverless environments.

May 15, 2025
Elasticsearch in Javascript the proper way, part I
Explaining how to create a production-ready Elasticsearch backend in JavaScript.

May 26, 2025
Displaying fields in an Elasticsearch index
Exploring techniques for displaying fields in an Elasticsearch index.

May 9, 2025
Deleting a field from a document in Elasticsearch
Exploring methods for deleting a field from a document in Elasticsearch.

May 21, 2025
Elasticsearch shards and replicas: Getting started guide
Master the concepts of shards and replicas in Elasticsearch and learn how to optimize them.