Cluster, node, and shard architecture in Elasticsearch - Time & Space Complexity
When working with Elasticsearch, it is important to understand how the cluster, nodes, and shards affect performance.
We want to know how the time to search or index data grows as the cluster size or shard count increases.
Analyze the time complexity of querying data spread across shards in a cluster.
GET /my_index/_search
{
"query": {
"match": { "field": "value" }
}
}
This query searches across all shards of the index distributed on multiple nodes in the cluster.
Look at what repeats when the query runs:
- Primary operation: Each shard runs the query independently.
- How many times: Once per shard in the index.
As the number of shards grows, the query runs on more pieces of data separately.
| Input Size (shards) | Approx. Operations |
|---|---|
| 10 | 10 query runs |
| 100 | 100 query runs |
| 1000 | 1000 query runs |
Pattern observation: The total work grows linearly with the number of shards.
Time Complexity: O(n)
This means the time to complete the query grows directly with the number of shards involved.
[X] Wrong: "Adding more shards always makes queries faster because work is split."
[OK] Correct: More shards mean more separate queries to run and combine, which can increase total time.
Understanding how Elasticsearch splits work helps you explain performance trade-offs clearly and shows you know how distributed systems behave.
"What if we changed the number of replicas per shard? How would that affect the time complexity of queries?"
Practice
node in Elasticsearch cluster architecture?Solution
Step 1: Understand the definition of a node
A node is one machine or server that joins the Elasticsearch cluster to share work.Step 2: Differentiate node from cluster and shard
A cluster is many nodes working together; shards are parts of data inside nodes.Final Answer:
A single machine that is part of the cluster -> Option AQuick Check:
Node = single machine [OK]
- Confusing node with cluster
- Thinking node is a data piece
- Mixing node with shard
Solution
Step 1: Define what a shard is
A shard is a smaller piece or partition of an index that holds part of the data.Step 2: Compare with other options
It is not a full copy (that's a replica), not a node or cluster.Final Answer:
A part of an index that stores data -> Option AQuick Check:
Shard = index part [OK]
- Thinking shard is a full index copy
- Confusing shard with node
- Mixing shard with cluster
Solution
Step 1: Calculate total shards per primary and replica
Each primary shard has 1 replica, so total shards = primary shards + replicas = 5 + 5 = 10 shards.Step 2: Understand shard distribution across nodes
Since there are 3 nodes, shards are distributed but total shards remain 10. However, the question asks total shards in cluster, including replicas.Step 3: Re-check options
15 shards says 15 shards, which is incorrect. The correct total is 10 shards.Final Answer:
10 shards -> Option BQuick Check:
Total shards = primary + replica = 5 + 5 = 10 [OK]
- Counting only primary shards
- Adding nodes to shard count incorrectly
- Confusing replicas with nodes
Solution
Step 1: Understand replica shard limits
Replicas cannot exceed the number of nodes minus one because replicas cannot be on the same node as primary.Step 2: Apply to given cluster
With 2 nodes, max replicas per shard = 1 (2 - 1). Trying 2 replicas is invalid.Final Answer:
You cannot have more replicas than nodes minus one -> Option DQuick Check:
Replicas ≤ nodes - 1 [OK]
- Setting replicas more than nodes - 1
- Confusing primary and replica counts
- Assuming replicas can be zero always
Solution
Step 1: Check replica limit based on nodes
With 4 nodes, max replicas per shard = nodes - 1 = 3. So 2 replicas is allowed.Step 2: Consider shard allocation rules
Each shard copy must be on a different node. 6 primary shards + 2 replicas = 18 shards total, but 18 shards on 4 nodes means some nodes hold multiple shards.Step 3: Avoid allocation errors by balancing shards
Too many shards per node can cause performance issues. 1 replica (total 12 shards) is safer and avoids errors.Final Answer:
6 primary shards and 1 replica per shard, total 12 shards -> Option CQuick Check:
Replicas ≤ nodes - 1 and balanced shards [OK]
- Setting replicas more than nodes - 1
- Ignoring shard distribution limits
- Choosing fewer shards than needed
Ready to prove it?
Pass the test and this pattern moves unknown
