Elasticsearch Shards and Capacity Planning: Optimize Performance and Scaling

Plan Elasticsearch capacity to maintain performance, scalability, and cost efficiency in search and analytics environments.

Elasticsearch is a distributed system used for search, logs, and metrics. Production deployments, self-hosted or cloud-based, require infrastructure and cluster configuration planning. This process ensures reliable performance. This article defines methods to estimate capacity and create deployment plans using usage metrics.

In short: keep each shard between 10 and 50 GiB, run at least three master nodes, and limit shards per node to 20 per GiB of Java heap.

Capacity Planning

  1. Determine the Minimum Number of Master Nodes
  2. Elasticsearch Service Sizing

Determine Minimum Master Node Count

Master nodes control cluster-wide activities, including index creation, deletion, and shard allocation. Cluster stability depends on master node health.

Dedicate nodes to the master role. Overloaded nodes with multiple roles may fail. Configure master-eligible nodes to hold only the master role to ensure they focus on cluster management.

Small clusters may not require dedicated master-eligible nodes. For clusters with more than six nodes, use dedicated master-eligible nodes.

Use the formula below to determine the quorum. The quorum is the minimum number of master-eligible nodes required for decision making.

CODE
01 Minimum Master Nodes = (N / 2) + 1

N is the total count of master-eligible nodes in the cluster, rounded to the nearest integer.

Maintain a minimum of three master nodes. Failure to meet this requirement risks a split-brain state. This state causes cluster instability and data loss.

Consider the following examples:

Scenario A

Scenario A: The cluster contains ten regular nodes. Each node can hold data and become master. The quorum is six. If the current master node fails due to a network issue, the cluster elects a new master and remains healthy.

Scenario B

In Scenario B, the cluster consists of three dedicated master nodes and one hundred data nodes. The quorum is two. If one master node fails, the cluster elects a new master and remains operational.

Scenario C

In Scenario C, the cluster has two nodes. The quorum is two. If a network failure separates the nodes, each attempts to elect itself as master. This prevents the cluster from operating.

Setting the value to one is allowed. However, this setting does not prevent data loss if the master node fails.

Note: Do not change the master node setting frequently. Frequent changes can cause cluster instability as the service adjusts the number of dedicated master nodes.

Sizing the Elasticsearch Service

Elasticsearch sizing requires an educated estimate rather than a fixed method. Key inputs include storage requirements, service usage, and Elasticsearch architecture. Use this estimate as a starting point. Validate it with representative workloads. Monitor performance.

Key sizing inputs include:

Elasticsearch scales horizontally. Poor initial indexing or sharding causes issues later. You will face approval delays to add hardware. You will underutilize infrastructure.

Three key components determine appropriate cluster settings:

If you want help sizing a production cluster, see our Elastic search and observability services.

Calculate storage requirements

Elasticsearch stores documents in indices. Classify storage by index type:

Assess the following factors in addition to storage space:

  1. Replica Count: A replica is a full copy of an index. Each replica consumes disk space equal to the primary index. The default replica count is 1. Maintain a replica count of 1 to prevent data loss. Replicas also improve search performance.
  2. Elasticsearch reserves 5% or 10% for margin of error and 15% to remain under disk watermarks. This space supports segment merges, logs, and internal operations.

Low storage capacity frequently causes cluster instability. Verify calculations for instance types, instance counts, and storage volumes.

How Many Shards Should an Elasticsearch Index Have?

Select the appropriate indexing strategy for each index. By default, Elasticsearch divides indices into primary and replica shards. For example, an index with 2 primary shards and 1 replica shard contains 4 total shards. You cannot change the primary shard count after index creation.

Each shard consumes CPU and memory. Excessive small shards cause performance issues and out-of-memory errors. Do not create shards that are too large.

Maintain a shard size between 10 and 50 GiB.

Calculate the approximate number of shards using this formula:

CODE
01App. Number of Primary Shards =
02(Source Data + Room to Grow) * (1 + Indexing Overhead) / Desired Shard Size

Optimize shard size. It must be small enough for efficiency. It must remain large enough to prevent hardware strain.

Review this example to verify the calculation:

Scenario 1

Consider 50 GiB of static data. Apply the formula: (50 × 1.1 / 30) = 2 shards.

Assume a target shard size of 30 GiB.

Scenario 2: Projected Growth

Assume the 50 GiB grows by 4x within 12 months. Total volume reaches 200 GiB. Apply the formula: ((50 + 150) × 1.1 / 30) = 8 shards.

Provisioning for future volume prevents under-sharding. It also avoids creating numerous small shards. Small shards increase CPU and memory overhead. They degrade current performance. With 8 shards, the current per-shard size is: (50 × 1.1) / 8 = 6.86 GiB.

Current shard size is below the recommended range of 10 to 50 GiB. This size consumes excess resources. A middle-ground approach uses five shards. This size yields 11 GiB per shard now (50 * 1.1 / 5). It yields 44 GiB per shard in the future ((50 + 150) * 1.1 / 5).

Both approaches rely on approximation rather than precise sizing.

Do not use exact sizing. Exact sizing risks disk exhaustion before reaching the threshold. For example, consider a 128 GiB instance. If disk usage stays below 80% (103 GiB) and shard size is 10 GiB, the node holds approximately 10 shards.

Limit shards per node to 20 per GiB of Java heap.

Instance Type Selection and Validation:

After determining storage requirements and shard counts, select hardware. Requirements vary by workload. Instance storage limits define the CPU and memory resources available for the workload.

The following formulas guide instance type selection.

Choosing the right instance type
CODE
01Total Data (GB) = Raw data (GB) per day * Number of days retained * (Number of replicas + 1)
CODE
01Total Storage (GB) = Total data (GB) * (1 + 0.15 disk Watermark threshold + 0.1 Margin of error)
CODE
01Total Data Nodes = ROUNDUP(Total storage (GB) / Memory per data node / Memory:Data ratio)

The following example illustrates these formulas.

A logging application ingests 3 GiB per day with a 90-day retention period. Use nodes with 8 GB of memory for this deployment size.

Calculate the values as follows:

Total Data (GB) = 3GB x 90 days x 2 = 540GB

Total Storage (GB) = 540GB x 1.25 = 675GB

Total Data Nodes = 675GB / (30 x 8GB) = 3 nodes

Summary of capacity calculations:

Capacity Planning Summary

Related reading: benchmarking Elastic Cloud on AWS, GCP and Azure, how to delete an index in Elasticsearch and troubleshooting Elasticsearch errors.

Sources:

https://www.elastic.co/blog/found-sizing-elasticsearch

https://www.cncf.io/blog/2021/03/25/how-to-build-an-elastic-search-cluster-for-production/

https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/what-is-amazon-elasticsearch-service.html

https://www.elastic.co/blog/benchmarking-and-sizing-your-elasticsearch-cluster-for-logs-and-metrics

Elasticsearch shards and capacity questions

How many shards should an Elasticsearch index have?
Keep shard size between 10 and 50 GiB. Estimate the shard count from data size, growth and a target shard size; for 50 GiB of static data with a 30 GiB target, the article calculates 2 shards. Each shard uses CPU and memory, so avoid many small shards, and limit shards per node to 20 per GiB of Java heap.
How many master nodes does an Elasticsearch cluster need?
Maintain a minimum of three master nodes, because fewer risks a split-brain state that causes cluster instability and data loss. For clusters with more than six nodes, use dedicated master-eligible nodes. The quorum is the minimum number of master-eligible nodes needed for decisions; with three dedicated masters it is two.
How do you calculate storage for an Elasticsearch cluster?
Classify indices as growing or rollover. For rollover indices, multiply the data generation rate by the retention time, then include replicas: each replica uses disk equal to the primary, and the default replica count is 1. Reserve 5% or 10% for error margin and 15% to stay under disk watermarks.
What happens when Elasticsearch has too many small shards?
Each shard consumes CPU and memory. Excessive small shards cause performance issues and out-of-memory errors, and they add overhead that degrades current performance. Do not create shards that are too large either. Optimize shard size so it is small enough for efficiency and large enough to prevent hardware strain.
How do you size Elasticsearch data nodes?
The article's example: a logging application ingests 3 GiB per day with 90-day retention on nodes with 8 GB of memory. Total data is 540 GB with one replica, storage is 675 GB with a 1.25 factor, and the result is 3 data nodes. Treat sizing as an educated estimate and validate it with representative data.