Cluster health API in Elasticsearch - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Checking cluster health helps us know how well Elasticsearch is working.
We want to understand how the time to get health info changes as the cluster grows.
Analyze the time complexity of the following code snippet.
GET /_cluster/health
{
"level": "shards",
"timeout": "30s"
}
This request asks Elasticsearch for detailed health info about each shard in the cluster.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Elasticsearch checks the status of each shard in the cluster.
- How many times: Once for every shard, so the number of shards determines how many checks happen.
As the number of shards grows, the time to gather health info grows too.
| Input Size (number of shards) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of shards; double the shards, double the checks.
Time Complexity: O(n)
This means the time to get cluster health grows in a straight line with the number of shards.
[X] Wrong: "Getting cluster health is always fast and does not depend on cluster size."
[OK] Correct: The API checks each shard's status, so more shards mean more work and longer time.
Understanding how cluster size affects health check time shows you can think about system scaling and performance.
"What if we change the level from 'shards' to 'cluster'? How would the time complexity change?"
Practice
Cluster Health API primarily provide?Solution
Step 1: Understand the purpose of Cluster Health API
The Cluster Health API is designed to report the health status of the Elasticsearch cluster, such as green, yellow, or red status.Step 2: Compare with other options
Options B, C, and D relate to documents, configuration, and performance, which are not the main focus of the Cluster Health API.Final Answer:
The current health status of the Elasticsearch cluster -> Option AQuick Check:
Cluster Health API = Cluster health status [OK]
- Confusing cluster health with document data
- Thinking it shows node configuration
- Assuming it reports query stats
Solution
Step 1: Recall the correct HTTP method and endpoint
The Cluster Health API uses the GET method with the endpoint/_cluster/health.Step 2: Eliminate incorrect options
POST is not used for this API, and the endpoint must be exactly/_cluster/health. Options C and D have wrong endpoints.Final Answer:
GET /_cluster/health -> Option AQuick Check:
GET + /_cluster/health = Correct syntax [OK]
- Using POST instead of GET
- Wrong endpoint like /_cluster/status
- Mixing endpoint parts
Solution
Step 1: Understand cluster health statuses
Green means all primary and replica shards are allocated properly, yellow means replicas missing but primaries allocated, red means some primaries missing.Step 2: Match the condition to status
Since all primary and replica shards are allocated, the status is green.Final Answer:
"status": "green" -> Option DQuick Check:
All shards allocated = green status [OK]
- Confusing yellow with green
- Thinking red means healthy
- Assuming blue is a valid status
GET /_cluster/health?level=shards but get an error. What is the likely cause?Solution
Step 1: Check valid values for
The Cluster Health API acceptslevelparameterlevelvalues like 'cluster', 'indices', and 'shards'. However, 'shards' is only supported in newer versions and may cause errors if unsupported.Step 2: Analyze other options
GET is correct method, endpoint is correct, anddetailis not a valid parameter for this API.Final Answer:
Thelevelparameter does not accept 'shards' as a value -> Option CQuick Check:
Invalid level value causes error [OK]
- Using POST instead of GET
- Wrong endpoint /_cluster/status
- Using invalid query parameters
Solution
Step 1: Identify the parameter for detailed index health
Thelevel=indicesparameter in the Cluster Health API returns health info for each index.Step 2: Compare with other options
level=clustergives overall cluster health only;/_cluster/stateand/_nodes/statsprovide different info unrelated to health per index.Final Answer:
GET /_cluster/health?level=indices -> Option BQuick Check:
Use level=indices for per-index health details [OK]
- Using level=cluster for detailed index info
- Confusing cluster state with health API
- Requesting node stats instead of health
