Search and Elasticsearch · senior
Shards, replicas and cluster health
An index is split into primary shards, each with replicas. The number of primaries is fixed at creation, and red means a primary shard has no home.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- A primary shard is a Lucene index that holds part of the data. Replicas are copies for failover and extra read capacity. A search runs on every shard in parallel.
- Green: all shards assigned. Yellow: all primaries assigned but some replicas are not. Red: at least one primary is unassigned, so part of an index is missing and queries that need it fail or return partial results.
- First calls: GET _cluster/health, GET _cat/nodes, GET _cat/shards?v&h=index,shard,prirep,state,unassigned.reason, then GET _cluster/allocation/explain for one unassigned primary.
- Typical red cause: a node died and the index had 0 replicas, set for a fast reindex and never set back. Always set replicas back after bulk loading.
- Shard size: one huge shard runs each search on one thread on one node, and merges and recovery become huge. Elastic suggests tens of GB per shard (10 to 50 GB is the range often quoted). Thousands of tiny shards cost memory, file handles and cluster-state work.
- The primary count cannot change on an existing index. Use the _split API (the index must be read-only, and the new count must be a multiple) or reindex into a new index behind an alias.
- Too many small daily indices can overload the cluster and make log ingestion reject bulk requests with 429.
The red pen
Common traps: mistake → fix
Mistake: Yellow means the cluster is down
Fix: Yellow means a replica is missing. Data is complete, but there is no redundancy.
Mistake: I can add primary shards to a live index with one setting
Fix: Primary count is fixed. You split or reindex.
Mistake: More shards always means faster search
Fix: Each shard has a fixed cost. Many tiny shards slow the cluster.
Easy to hard
Questions interviewers ask, easy to hard
- What do green, yellow and red mean?
- What are the first calls you run when the cluster is red?
- Why can one node crash cause red, and how do replicas prevent it?
- How do you choose the number of primary shards?
- One index has a 180 GB shard and the cluster has thousands of tiny shards. What do you do?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.