Elasticsearch Shards 101 A Comprehensive Guide

Author

Reads 718

Beautiful sunset over The Shard and Thames River in London.
Credit: pexels.com, Beautiful sunset over The Shard and Thames River in London.

Elasticsearch Shards are a fundamental concept in Elasticsearch, and understanding them is crucial for effective data management.

Elasticsearch Shards are essentially smaller, independent pieces of a larger index. They allow for horizontal scaling, meaning you can add more nodes to your cluster as your data grows.

Each Shard has its own index, and Shards can be split or merged as needed. This flexibility is key to managing large datasets.

Shards are typically created when you index a document, and the number of Shards is determined by the number of primary shards specified in the index settings.

Elasticsearch supports both single-node and multi-node clusters, and Shards play a critical role in both.

What is Elasticsearch Sharding

Elasticsearch sharding is a fundamental concept that allows for horizontal scaling, improving search performance and ensuring high availability.

Shards are the building blocks of Elasticsearch's distributed architecture.

They enable horizontal scaling, which means you can add more nodes to your cluster as your data grows, without having to worry about performance degradation.

There are two types of shards in Elasticsearch.

Shards are essential for improving search performance, ensuring that your search queries can be handled efficiently and effectively.

Curious to learn more? Check out: Elasticsearch Performance

Setting Up and Managing Shards

Credit: youtube.com, Nodes, clusters, and shards in Elasticsearch - S1E3:Mini Beginner's Crash Course

Setting up shards is a crucial step in creating an index in Elasticsearch. You should define the number of shards, which cannot be changed without reindexing the data.

Each shard should hold between 30-50GB of data. For example, if you expect to accumulate around 300GB of logs daily, an index with 10 shards would be appropriate.

To ensure shards are evenly distributed across data nodes, try to make the shard count an even multiple of the data node count. For instance, if you have 12 primary shards, your data node count should be 2, 3, 4, 6, or 12.

Here are some general guidelines for shard size:

Monitoring shard states regularly is also important. You can use the _cat/shards API to check shard states and ensure they are in optimal states, such as STARTED.

How to Structure My

Structuring your shards is crucial for optimal performance.

Each shard should hold between 30-50GB of data.

Credit: youtube.com, Understanding Total Shards Calculation in Elasticsearch

Data Shard (Search Workload) should be around 10-30 GiB, while Data Shard (Logs Workload) can be up to 50 GiB.

Small shards result in small segments, increasing overhead, so aim for an average shard size of a few GB to a few tens of GB.

For time-based data, shards between 20GB and 40GB in size are common, with a maximum of 50 GB.

To reduce overhead and improve query performance, consider forcing smaller segments to merge into larger ones through a forcemerge operation.

This operation should be done once no more data is written to the index, ideally during off-peak hours, as it can be expensive.

To check your shard size, consider referencing the provided formulas.

The formulas for calculating the shard become more complex when using custom routing values, which can help distribute data more evenly.

To enable this feature, the index.routing_partition_size should have a value greater than 1 and less than index.number_of_shards.

For your interest: Elasticsearch Index Template

Primary and Replica Foundation

Credit: youtube.com, Primary Shard and Replica Shard in Elasticseacrh

Setting up shards is a crucial step in creating an index, and it's a decision that can't be changed without reindexing the data.

You might set up an index with 10 shards if you expect to accumulate around 300GB of logs daily, with each shard holding between 30-50GB of data.

Elasticsearch has built a strong reputation as a distributed storage, search, and analytics platform for huge volumes of data.

Understanding primary and replica shards is fundamental to Elasticsearch, as it can help optimize the reliability and performance of the platform.

Replica shards are copies of primary shards, and they're used to increase search performance and for fail-over.

A replica shard is never allocated on the same node as the primary shard, which is why it's like putting a backup on a different disk from the original data.

For an index with five primary shards and one replica, each indexing request touches 10 shards, while search requests are sent to n shards, where n is the number of primary shards.

In the case of an index with five primary shards and one replica, each search query touches five shards (primary or replica) from that index.

Recommended read: Elasticsearch Index Api

Plan Count at Index Creation

Credit: youtube.com, Index Settings: Set shards and replicas

Planing the right number of shards at index creation is crucial for optimal performance. You should determine the appropriate number of shards based on expected data volume, aiming for 30-50GB per shard.

This means if you expect to accumulate around 300GB of logs daily, an index with 10 shards would be a good starting point. You can't change the shard count after index creation without reindexing the data.

Here's a quick rundown of how to plan shard count at index creation:

Keep in mind that this is just a rough estimate, and you should adjust the shard count based on your specific use case.

Example Size Calculation

Calculating the right shard size is crucial to ensure efficient data movement and processing. You want to avoid shards that are too large or too small.

Each shard should hold between 30-50GB of data. This is a general guideline to keep in mind when planning your shard count. For instance, if you expect to accumulate around 300GB of logs daily, an index with 10 shards would be appropriate.

See what others are reading: Elasticsearch Spring Data

Artistic portrait featuring a woman with mirror shards on a pink backdrop, evoking creativity and reflection.
Credit: pexels.com, Artistic portrait featuring a woman with mirror shards on a pink backdrop, evoking creativity and reflection.

The total number of shards that a node can hold is proportional to the node's Java virtual machine (JVM) heap memory. Aim for 25 shards or fewer per GiB of heap memory. For example, a node with 32 GiB of heap memory should hold no more than 800 shards.

To calculate the approximate number of primary shards, use the following equation: (Source data + room to grow) * (1 + indexing overhead) / desired shard size. This helps compensate for data growth over time. For example, if you have 66 GiB of data, you can calculate the number of shards as follows:

Keep in mind that you don't want to create unnecessarily tiny shards that consume huge amounts of CPU and memory. It's better to start with a more middle-of-the-road approach, such as six shards, which leaves you with 12-GiB shards today and 48-GiB shards in the future.

Optimizing Shard Performance

Credit: youtube.com, Optimizing Elasticsearch - How many shards per index?

Optimizing shards and replicas is crucial to maintain optimal performance, especially with time series data where newer indices are generally more active than older ones.

To separate newer and older indices, you can use the rollover index API, which can be set to automatically create a new index once a certain threshold is reached.

Shrinking and force-merging an index are two different ways to reduce the memory and disk footprints of older indices that only require infrequent access.

Having lots of small shards can actually reduce query throughput if there are multiple concurrent queries.

A small set of large shards uses fewer resources than many small shards.

Discover more: Elasticsearch Indices

Benefits

Benefits of optimizing shard performance are clear. Horizontal scaling is made possible by sharding, allowing Elasticsearch to distribute data across multiple nodes as data volume increases.

Sharding enables optimal performance by distributing data, ensuring no single node becomes a bottleneck. This distribution ensures that the cluster can grow horizontally as data volume increases.

Credit: youtube.com, Optimizing OpenSearch Cluster Performance With Dynamic Shardi... Aswath Srinivasan & Robert Hoffmann

Improved search performance is achieved by splitting data into smaller units, allowing Elasticsearch to execute search queries on multiple shards concurrently. This results in faster response times, making search queries more efficient.

Having a small set of large shards uses fewer resources than many small shards, which is beneficial for query throughput. This is because a smaller number of larger shards requires less processing power and memory.

Replica shards ensure high availability, keeping data available even if a node fails. Elasticsearch automatically routes search and indexing operations to the available shards, maintaining the cluster's overall health.

Optimizing

Optimizing shard performance is crucial for maintaining optimal performance in Elasticsearch. This involves monitoring and adjusting configurations as index dynamics change.

For time series data, newer indices are usually more active and require different resource allocations than older indices. Tools like the rollover index API can automatically create new indices based on size, document count, or age.

Credit: youtube.com, Sharding vs. Partitioning #database #performance #optimization

Shrinking and force merging are techniques used to decrease memory and disk usage for older, less active indices. Shrinking reduces the number of shards in an index, while force merging reduces Lucene segments and frees up space used by documents that have been deleted.

Optimizing shards and replicas is essential for maintaining optimal performance and ensuring that no single node becomes a bottleneck. Horizontal scaling, improved search performance, and high availability are all benefits of sharding.

By splitting data into smaller units, Elasticsearch can execute search queries on multiple shards concurrently, resulting in faster response times. This is a key advantage of sharding, as it allows the cluster to grow horizontally as the data volume increases.

Shard Best Practices

Choosing the right number of primary shards is crucial for optimal performance, and you should consider factors such as data volume, query load, and hardware resources when determining this number.

Selecting too few primary shards can lead to performance issues, while selecting too many can result in wasted resources and indexing performance problems.

Credit: youtube.com, How to Rebalance Elasticsearch Shards Across Three Nodes

Having at least one replica shard for each primary shard is recommended, as it ensures high availability and improved search performance.

However, having too many replicas can consume additional resources and affect indexing performance.

Regularly monitoring shard allocation helps identify imbalances and potential issues in the cluster, and Elasticsearch provides APIs such as the Cluster Health API and the Shard Allocation API to monitor and manage shard allocation.

In case of an imbalance, Elasticsearch provides the Cluster Reroute API to manually move shards between nodes, but use this API with caution, as it can impact cluster performance if not used correctly.

Shard Operations

You can shrink an existing index to fewer shards if you no longer write to it, using the shrink index API.

This can reduce storage costs and make your cluster more efficient.

ILM has a shrink action for indices in the warm phase, allowing you to further optimize storage usage.

Avoid Node Hotspots

Credit: youtube.com, Can Data Persist in Elasticsearch When Cold Nodes Reach Maximum Shards Despite Available Hot Nodes?

You should limit the number of shards on a single node to prevent hotspots. This can be done using the index.routing.allocation.total_shards_per_node index setting.

Explicitly setting this limit helps prevent a single node from becoming overwhelmed with too many shards. For example, if you have an index with a high indexing volume, it's best to limit the number of shards on each node.

To configure this setting, you can use the update index settings API. This ensures that your shards are evenly distributed across data nodes.

Aim to make your shard count an even multiple of the data node count. This helps prevent hot nodes and ensures shards are evenly distributed.

For example, if you have 12 primary shards, your data node count should be 2, 3, 4, 6, or 12.

Remember, shard count is secondary to shard size. If you have a small amount of data, it's better to use a single shard.

Shrink an Existing Index

Credit: youtube.com, Optimizing Index Operations in Elasticsearch: Shrink & Rollover - Daily Elastic Byte S01E05

You can reduce the shard count of an existing index that no longer receives new data using the shrink index API.

This approach is particularly useful for indices that are no longer being written to, as it can help save resources and reduce costs.

The shrink index API is a powerful tool that can be used to reclaim disk space and optimize performance.

ILM also offers a shrink action for indices in the warm phase, providing additional flexibility for managing your data.

With ILM's shrink action, you can proactively manage your index's shard count and ensure it remains optimized for your use case.

Use Rollover API for Dynamic Indices

Implementing rollover indices is a game-changer for managing shard sizes on dynamic datasets. This approach ensures that shard sizes remain manageable by periodically creating new indices and transferring data to them.

Rollover indices are particularly useful for time series data, which tends to grow rapidly. By implementing rollover indices, you can prevent individual shards from becoming too large and unwieldy.

Credit: youtube.com, How to change Number of Shards on elk | Why to use Shrink API over Re Index API | Learn |

To roll over indices effectively, consider the following best practices:

  • Use the Rollover API to automate the process and minimize downtime.
  • Configure the rollover frequency based on your dataset's growth rate and performance requirements.

By rolling over your indices regularly, you can maintain a healthy balance between data freshness and shard size, ensuring optimal performance and reliability in your Elasticsearch cluster.

Shard Configuration and Tuning

When it comes to shard configuration and tuning, understanding the relationship between JVM heap memory and shard count is crucial. A node can hold a maximum of 25 shards per GiB of heap memory.

To give you a better idea, a node with 32 GiB of heap memory should hold no more than 800 shards. The cat/ allocation API can provide a quick view of the number of shards and total shard storage across data nodes.

Aim for shard sizes that are neither too large nor too small. Large shards can cause inefficiencies in data movement and processing, while small shards can increase memory and disk overhead.

Here are some general guidelines for shard size calculation:

In general, it's a good idea to balance shard size to prevent inefficiencies and ensure optimal performance.

Default Routing

Credit: youtube.com, How to configure Default route- by eng NEDJAR Mohamed Amine

Default routing is handled automatically by Elasticsearch, making it easy to use without manual intervention.

Most users won't need to deal with routing manually because Elasticsearch uses a simple formula to determine the appropriate shard for a given document. This formula uses the document's ID, which is passed through a hashing function to generate a number. The remainder of dividing this number by the number of primary shards in the index gives the shard number.

This default behavior ensures that documents are distributed evenly across shards. The process is different when executing search queries, as the query is then broadcasted to all shards.

The "routing" value is equal to a given document's ID by default, which is then used to determine the shard location.

A different take: Document in Elasticsearch

Detect Storage Skew

Detecting storage skew in your Elasticsearch cluster is crucial to ensure optimal performance. Storage skew occurs when one or more nodes hold a higher proportion of storage for one or more indexes than the others.

Recommended read: Elasticsearch Storage

Credit: youtube.com, What are Elasticsearch shards? Why do they matter? Elasticsearch cluster architecture explained.

Indications of storage skew include uneven CPU utilization, intermittent and uneven latency, and uneven queueing across data nodes. These issues can lead to inefficient data movement and processing.

A far less common issue involves limiting the number of shards per node, which is typically not a concern if you size your shards appropriately. For example, an m6g.large.search instance has a maximum disk size of 512 GiB.

To determine whether you have skew issues, look for these signs. If you notice uneven CPU utilization, latency, or queueing across nodes, it may be a sign of storage skew.

To adjust the maximum shards per node, configure the cluster.max_shards_per_node setting. This setting is especially important for Elasticsearch 7.x and later, which have a limit of 1,000 shards per node.

By being aware of these potential issues and taking steps to prevent them, you can ensure your Elasticsearch cluster runs smoothly and efficiently.

For another approach, see: Elastic Search Cluster

JVM Heap Size

JVM Heap Size is a crucial factor in determining the number of shards a node can hold. The total number of shards is proportional to the node's JVM heap memory.

Credit: youtube.com, JVM tuning and garbage collection optimization | iCert Global

Aim for 25 shards or fewer per GiB of heap memory. This means a node with 32 GiB of heap memory should hold no more than 800 shards.

There's a limit of 1,000 shards per node, so be mindful of this maximum capacity. The cat/allocation API provides a quick view of the number of shards and total shard storage across data nodes.

Shard communication is a crucial aspect of Elasticsearch shards, allowing them to work together seamlessly. Each shard can communicate with its peers to ensure data consistency and availability.

In Elasticsearch, shards can be configured to communicate with each other through a process called "shard coordination." This coordination enables shards to share information and make decisions collectively.

Shard search is facilitated by shard communication, which allows Elasticsearch to return accurate results even when data is distributed across multiple shards. This is particularly useful for large-scale datasets where data is often fragmented across multiple shards.

Routing to Index Partition

Credit: youtube.com, Elasticsearch - Index, Shard and Routing

To mitigate the risk of an imbalanced cluster, you can configure your index to use custom routing values that go to a subset of shards. This setting, index.routing_partition_size, is applied at index creation and determines how evenly distributed the data will be.

A value greater than 1 and less than index.number_of_shards is required for this setting to be effective. This means you need to choose a number that balances the trade-off between even distribution and the number of shards searched per request.

The formulas for calculating the shard change when this setting is present, using the _routing field to calculate a set of shards within the index and then the _id to pick a shard within that set. This is achieved through the following formulas: routing_value = hash(_routing) + hash(_id) % routing_partition_size and shard_num = (routing_value % num_routing_shards) / routing_factor.

By using this feature, you can reduce the impact of searches on your cluster while still maintaining a level of even distribution.

Node Communication

Credit: youtube.com, Elasticsearch Architecture: Understanding Nodes, Clusters, and Shards

Node Communication is handled through a custom binary protocol, which serializes objects to facilitate communication between nodes.

By default, nodes communicate on port 9300, although you can specify a range of ports between 9300 and 9400 if needed.

Each node in a cluster is cluster-aware and shares a cluster state that keeps track of shard locations.

You can send requests to any node, which will then be rerouted to the relevant nodes for proper execution.

To discover other nodes, you need to list them in the discovery.zen.ping.unicast.hosts configuration in the Elasticsearch configuration file.

The preferred port for discovery is the transport port, usually 9300.

To check your transport port, you can use a standard Elasticsearch configuration file.

On a similar theme: Elasticsearch Port

Elasticsearch Auto-Balancing

Elasticsearch automatically balances shards within a data tier by spreading an index's shards across as many nodes as possible. This helps distribute the load evenly and prevents hot nodes.

To achieve optimal performance, it's essential to understand how rebalancing works. Increasing cluster.routing.allocation.cluster_concurrent_rebalance above the default of 2 can lead to unnecessary rebalancing movements, which can drastically increase the time it takes for the cluster to reach a balanced state.

A better approach is to remove the custom settings and let Elasticsearch use its default values. This can help prevent unnecessary rebalancing and ensure a balanced state is reached efficiently.

Reduce Cluster Count

Credit: youtube.com, How to Optimize Your Cluster Architecture to Reduce Elasticsearch & OpenSearch Costs

Reducing a cluster's shard count can be a complex task, but one simple method is to avoid using a max_age threshold for the rollover action if you use Index Lifecycle Management (ILM) and your retention policy allows it.

This approach can help prevent the creation of empty indices or many small shards, which is a common issue in oversharded clusters.

To take it a step further, consider using the max_primary_shard_size parameter to control the size of your shards. This can help maintain a healthy balance in your cluster and prevent unnecessary shard creation.

By implementing these strategies, you can effectively reduce your cluster's shard count and improve its overall performance.

Elasticsearch Auto-Balances Within a Data Tier

Elasticsearch automatically balances shards within a data tier, grouping nodes into data tiers and spreading index shards across as many nodes as possible.

This automatic rebalancing ensures that data is evenly distributed across nodes, but it can sometimes make unnecessary rebalancing movements if cluster.routing.allocation.cluster_concurrent_rebalance is increased above its default value of 2.

Credit: youtube.com, Elasticsearch shard allocation awareness - Avoid local disaster become global

To prevent this, it's recommended to remove the cluster.routing.allocation.cluster_concurrent_rebalance setting from the configuration, allowing its default value to take effect.

For optimal performance, it's also a good idea to make the shard count an even multiple of the data node count, as this helps ensure shards are evenly distributed across data nodes and prevents hot nodes.

A shard count of 12 is a good example, where the data node count should be 2, 3, 4, 6, or 12.

Frequently Asked Questions

How to check the number of shards in Elasticsearch?

To check the number of shards in Elasticsearch, use the `GET _cat/shards` API call. This will return a list of shards, including their status and other relevant information.

Victoria Kutch

Senior Copy Editor

Victoria Kutch is a seasoned copy editor with a keen eye for detail and a passion for precision. With a strong background in language and grammar, she has honed her skills in refining written content to convey a clear and compelling message. Victoria's expertise spans a wide range of topics, including digital marketing solutions, where she has helped numerous businesses craft engaging and informative articles that resonate with their target audiences.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.