Quick revision
The short answer
The cluster is oversharded. Every shard costs heap memory and adds entries to the cluster state that the master must manage, so 40,000 mostly tiny shards on 12 nodes, about 3,300 per node, make the master slow and the heap full. We fix it by moving to data streams with rollover and lifecycle policies (ILM in Elasticsearch, ISM in OpenSearch) that create indices by size rather than by day, by giving new indices fewer primaries, by shrinking, merging or deleting old ones, and by running dedicated master nodes.
What to remember
In this answer, we saw that 40,000 small shards on 12 nodes is oversharding: every shard costs heap and cluster state, and the master slows down as the state grows. We fixed it by rolling over data streams by size with ILM or ISM instead of creating daily indices, giving new indices one primary, deleting, shrinking and merging old indices in batches, and adding dedicated master nodes so cluster management stays stable.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The cluster is oversharded. Every shard costs heap memory and adds entries to the cluster state that the master must manage, so 40,000 mostly tiny shards on 12 nodes, about 3,300 per node, make the master slow and the heap full. We fix it by moving to data streams with rollover and lifecycle policies (ILM in Elasticsearch, ISM in OpenSearch) that create indices by size rather than by day, by giving new indices fewer primaries, by shrinking, merging or deleting old ones, and by running dedicated master nodes.
Why this question is asked
Daily indices with five shards each were a common pattern years ago, and they work at first. A few years later the same cluster has tens of thousands of shards, cluster operations take seconds, and nodes run out of heap while holding very little data. Interviewers like this scenario because it tests whether we can connect a configuration habit to its cost and fix it without losing data. In this answer, we will work out what the numbers tell us, see why each shard costs memory and master time, and then go through the fixes in the order we would apply them.
1Reading the Numbers
Let us start with arithmetic. One daily index with 5 primaries and 1 replica creates 10 shards per day. If the cluster stores, say, a dozen kinds of logs, that is 120 new shards every day, about 43,000 a year. Spread over 12 nodes, 40,000 shards is roughly 3,300 shards per node. Elasticsearch's default limit, cluster.max_shards_per_node, is 1,000 shards per non-frozen data node, so someone has probably raised that limit to keep going, which is a warning sign in itself.
Now the size. If the whole cluster holds 20 TB including replicas, the average shard is about 500 MB. Many will be far smaller, because quiet log types produce a few megabytes a day, split five ways. The data would fit comfortably in about 400 to 800 shards of 25 to 50 GB.
2Why Small Shards Are Expensive
A shard is a full Lucene index. Each one, however small, has open files, segment metadata, mapping structures and per-shard data structures held on the JVM heap, plus thread pool work for refreshes and merges. Ten thousand tiny shards cost far more memory than a hundred large ones holding the same data.
The master node pays too. The cluster state, the metadata the master maintains and publishes to every node, lists every index, its mappings and every shard's location. Each index creation, mapping update or shard movement changes it, and the master must compute and send the new state to all nodes. With 40,000 shards this state is large, every change is slow, and when a node restarts the master must reallocate thousands of shards. Searches also suffer: a query over a month of one log type touches 30 x 5 = 150 shards, and each shard adds coordination overhead even if it holds almost nothing.
3Fix 1: Roll Over by Size With Data Streams and Lifecycle Policies
The root cause is creating indices by calendar instead of by size. A data stream is a named, append-only target, for example logs-payments, backed by a series of hidden indices. A lifecycle policy rolls the stream over to a new backing index when the current one reaches a size or age limit, and later deletes old ones. A quiet log type might roll over once a month; a busy one several times a day. Each backing index then holds shards close to the target size.
Notice the two limits on rollover. max_primary_shard_size keeps shards near 40 GB for busy streams, and max_age stops quiet streams from keeping one index open for ever. A common guideline is shards of 10 to 50 GB, but check your vendor's current guidance for your workload. In OpenSearch the same idea is expressed with Index State Management (ISM) policies and rollover aliases or data streams.
4Fix 2: Clean Up the Existing Shards
New indices now come out right, but the 40,000 existing shards remain. We work through them from cheapest to most expensive. First, delete what is past retention; often a large share of indices is older than anyone needs. Second, shrink older indices from 5 primaries to 1 with the _shrink API, after making them read-only, and force merge them to one segment. That alone cuts their shard count by five. Third, for long tails of tiny daily indices, reindex a month of them into one monthly index and delete the originals.
Order matters for safety. We do this gradually, a few hundred shards at a time, watching cluster health and pending tasks, because shrinks and reindexes themselves create work for the already struggling master. We also lower cluster.max_shards_per_node back towards the default once we are under it, so the limit protects us again.
- Stop the growthtemplates: 1 primary, rollover by size
- Delete expired indicesfastest win
- Shrink read-only indices to 1 primarythen force merge
- Reindex tiny daily indices into monthly onesdelete the originals
- Shards per node near a healthy number?YesRestore the default shard limitNoContinue in small batches
5 steps in order. Play them, or step through them one at a time.
Each step is done in batches while watching cluster health and pending tasks.
5Fix 3: Dedicated Master Nodes
In many oversharded clusters, the master role runs on the same nodes as data. When those nodes are busy with indexing and merges and their heap is full, the master's work of updating cluster state is delayed, and the whole cluster feels slow. Three dedicated master-eligible nodes, small machines that hold no data, keep cluster management stable: they have their own heap for the cluster state and are not disturbed by search load. Three is the usual number, because a majority of two can still elect a master if one fails.
Dedicated masters are not a substitute for fixing the shard count. They make a large cluster state manageable, but 40,000 shards would still waste heap on the data nodes. Both changes together bring the cluster back to health.
6Follow-up questions
How many shards per node is healthy?
There is no single number. Older guidance was at most 20 shards per GB of heap; recent Elasticsearch guidance focuses instead on per-shard and per-field overhead and the default limit of 1,000 shards per node. In practice, a few hundred well-sized shards per data node is comfortable for most clusters. Check your vendor's current documentation.
Why not just add more nodes?
It would spread the shards thinner and buy time, but the cluster state would stay just as large and the master would still be slow. We would also be paying for hardware to hold overhead rather than data. Reducing the shard count fixes the cause.
7Conclusion
In this answer, we saw that 40,000 small shards on 12 nodes is oversharding: every shard costs heap and cluster state, and the master slows down as the state grows. We fixed it by rolling over data streams by size with ILM or ISM instead of creating daily indices, giving new indices one primary, deleting, shrinking and merging old indices in batches, and adding dedicated master nodes so cluster management stays stable.
Open with
This is oversharding. Daily indices with five shards each, plus a replica, create ten shards per day per log type, and with a dozen types the cluster reaches tens of thousands of shards, about 3,300 per node, while most are tiny.
Then, if they want more
Each shard is a full Lucene index with its own heap overhead, and every shard and index is part of the cluster state that the master has to update and publish, so the master gets slow and data nodes run out of heap holding little data.
First I would stop the growth: index templates with one primary, and data streams with an ILM or ISM policy that rolls over on size, around 40 GB per primary shard, with a maximum age so quiet streams still roll. The common guideline is shards of tens of gigabytes, but I would check the vendor's current advice.
Then I would clean up in batches: delete anything past retention, shrink read-only indices from five primaries to one and force merge them, and reindex tiny daily indices into monthly ones. I would watch cluster health and pending tasks while doing it.
Finally I would add three dedicated master nodes so cluster management is not competing with search and indexing load, and bring the shards-per-node limit back to the default once we are under it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Elasticsearch documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A cluster has 40,000 small shards across 12 nodes from daily indices with 5 shards each...": https://scrivio-interview-library.netlify.app/databases/search-engines/15d6ea71ab/