Large shards are why your search cluster needs the biggest nodes
How very large shards and unpartitioned indices force Elasticsearch and OpenSearch onto the largest node sizes, and how to fix the layout.
- BITServices team
- #opensearch
- #elasticsearch
A search cluster that keeps growing is usually blamed on data volume. In the cases we see, the bigger cause is how the data is laid out: a few very large indices, never partitioned, with very large shards. Here is why that gets expensive and what to do about it. Everything below applies to both Elasticsearch and OpenSearch.
What a shard costs you
An index is split into shards, and each shard is a separate Lucene index on one node. Three things follow from that.
A query runs once per shard, on one thread. A search request is sent to every shard of the index, and each shard is searched by a single thread on its node. If one shard holds 300 GB, the query waits for that one thread to get through 300 GB. You cannot make that faster by adding nodes. You can only make the node bigger.
Moving a shard means copying all of it. When a node restarts, fails or is replaced, its shards are copied to other nodes. A 300 GB shard takes a long time to copy, and while it copies, the cluster runs with less redundancy and more load. To make this safe, every node needs a lot of spare disk and memory.
The cluster is sized for the biggest shard. Shards cannot be split across nodes. The node that holds the largest shard needs enough heap, disk and CPU for it, and in practice all data nodes end up that size.
The result is a cluster that always runs on the largest node size available, even when the total amount of data would fit on smaller nodes.
How big is too big
Elastic's own guidance is to keep shards between roughly 10 GB and 50 GB. Smaller than that and you pay overhead for too many shards. Larger and you get the problems above.
You can see your shard sizes with:
GET _cat/shards?v&s=store:desc&h=index,shard,prirep,store,node
If the top of that list is in the hundreds of gigabytes, the layout is the problem, not the hardware.
Partition the data
The fix is to split large indices into smaller ones along a line that matches how the data is used.
- By time for logs, events and anything that is mostly appended and read by date. A new index per day, week or month, created automatically with rollover.
- By tenant for multi-tenant products, where most queries are for one customer. Each tenant, or group of small tenants, gets its own indices.
Use index templates so new indices get the right settings and mappings, and aliases so the application keeps reading and writing one name. The application does not need to know how many indices sit behind it.
With partitioned data you can also treat old data differently. Index Lifecycle Management in Elasticsearch, or Index State Management in OpenSearch, can shrink old indices, move them to cheaper nodes and delete them on a schedule.
Spread the load, not only the data
With smaller indices you are no longer tied to one big cluster. For a large multi-tenant system it often makes sense to run two or three smaller clusters per region and spread the tenants across them. A problem in one cluster then affects only the tenants on it, and each cluster can be sized and upgraded on its own.
Fix the mappings at the same time
Partitioning means reindexing, and reindexing is the one moment when changing mappings is cheap. Look for:
nestedfields that are not queried as nested. Every nested object is indexed as a separate hidden document, so a document with 50 nested items is 51 documents in Lucene.- text fields that are only ever filtered on. They should be
keyword. - fields nobody searches. Store them, but do not index them.
What it saves
In one of our projects the search platform was moved to OpenSearch, partitioned by tenant and split into smaller clusters, with the mappings and the heaviest queries fixed on the way. The cluster no longer needed the largest node sizes, and the client saved $1M a month. The new layout did most of the work, not the platform change.