Bird
Raised Fist0

Elasticsearch Architecture - Shards, Replicas, Query Routing

HLDmediumInterview freq: high
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/6
Coord. Node

Receives search query from client

Parses query and determines relevant shards

Protocol:HTTP REST API
BeforeClient query is sent to cluster endpoint
AfterCoord. Node holds parsed query and shard targets
Primary Shard

Executes query on primary shard data

Processes query and prepares results

Protocol:Internal RPC
BeforePrimary Shard has latest shard data and query request
AfterPrimary Shard has query results ready for aggregation
Replica Shard

Executes query on replica shard copy

Processes query for load balancing and fault tolerance

Protocol:Internal RPC
BeforeReplica Shard has replicated shard data and query request
AfterReplica Shard has query results ready for aggregation
Coord. Node

Aggregates results from primary and replica shards

Merges and ranks results before responding to client

Protocol:Internal RPC
BeforeCoord. Node has partial results from shards
AfterCoord. Node has final aggregated query response
Cluster State Mgmt

Updates cluster metadata after shard state changes

Propagates cluster state changes to nodes

Protocol:State updates
BeforeCluster state is consistent with previous shard allocations
AfterCluster state reflects current shard locations and node health
Shard Allocation & Balancing

Rebalances shards based on cluster state

Moves shards to balance load and maintain availability

Protocol:State updates
BeforeShard allocation may be uneven or nodes overloaded
AfterShard allocation is balanced across nodes for optimal performance
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────────────────────┐
│         Coord. Node           │
│  (Receives client queries,    │
│   routes to shards)           │
└───────────────┬───────────────┘
                │ HTTP REST API
                ▼
┌───────────────┐
│ Primary Shard │
│ (Executes     │
│  query, writes│
│  changes)     │
└───────┬───────┘
        │ Internal RPC
        ▼
┌───────────────┐
│ Replica Shard │
│ (Executes     │
│  query copies,│
│  provides HA) │
└───────┬───────┘
        │ Updates via replication
        ▼
┌───────────────────────────────┐
│ Cluster State Mgmt            │
│ (Maintains cluster metadata,  │
│  shard locations, node status)│
└───────────────┬───────────────┘
                │ State updates
                ▼
┌───────────────────────────────┐
│ Shard Allocation & Balancing  │
│ (Decides shard placement and  │
│  rebalances for load)          │
└───────────────────────────────┘

Elasticsearch uses a Coordinating Node to receive client queries and route them to the appropriate shards. The Primary Shard executes the query and handles writes, while Replica Shards provide fault tolerance by maintaining copies and also executing queries for load balancing. Cluster State Management maintains metadata about shard locations and node health, ensuring the cluster has a consistent view. Shard Allocation and Balancing dynamically assigns shards to nodes and redistributes them to optimize load and availability. This architecture enables scalable, fault-tolerant search with efficient query routing and data replication.

Primary Focus

✓

Practice

(1/5)
1. In the vector database request flow, at Step 4 the ANN Search Worker queries the HNSW index with a new vector. What state does the worker observe if the index is currently rebuilding due to a recent bulk insert?
easy
A. The worker returns stale search results from the previous index snapshot
B. The worker blocks until the rebuild completes before returning results
C. The worker returns an empty result set indicating no neighbors found
D. The worker queries a fallback brute-force scan of all vectors

Solution

  1. Step 1: Understand HNSW index rebuild behavior

    HNSW index rebuilds asynchronously; during rebuild, queries use the last consistent snapshot.
  2. Step 2: Analyze worker behavior at Step 4

    The ANN Search Worker returns results from the previous stable index snapshot, so stale but consistent results are served.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    HNSW queries serve last snapshot during rebuild, avoiding blocking [check]
Hint: HNSW queries serve last snapshot during rebuild, no blocking [check]
Common Mistakes:
2. In the For-You feed request flow, at step 4 the Feed Ranking Service receives user watch history and video metadata. What does it do if the user has no recent watch history?
easy
A. Falls back to a global trending video list for recommendations
B. Queries the Creator Stack for personalized creator recommendations only
C. Waits for user watch history to be populated before responding
D. Returns an empty feed immediately to avoid delay

Solution

  1. Step 1: Understand Feed Ranking fallback logic

    Empty feed (A) harms UX; waiting (C) blocks response; global trending (B) is a fallback but not personalized.
  2. Step 2: Identify personalization fallback

    Creator Stack (D) provides personalized creator recommendations when watch history is missing, improving engagement.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Creator Stack fallback personalizes feed without watch history [check]
Hint: Fallback to Creator Stack for personalization without history [OK]
Common Mistakes:
3. For computing ETA in a city with highly variable traffic, which approach is better and why?
medium
A. Use a precomputed route cache updated daily with average travel times
B. Use a static shortest path algorithm with fixed average speeds per road segment
C. Use a heuristic-based routing ignoring traffic but optimizing for shortest distance
D. Use a dynamic routing algorithm that incorporates live traffic data and historical patterns

Solution

  1. Step 1: Compare static vs dynamic ETA computation

    Static shortest path (A) and precomputed caches (D) ignore live traffic variability, leading to inaccurate ETAs.
  2. Step 2: Evaluate heuristic and dynamic approaches

    Heuristic ignoring traffic (C) fails in variable conditions; dynamic routing (B) adapts to live and historical data for accurate ETA.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Dynamic routing with live traffic yields accurate ETAs in variable conditions [check]
Hint: Dynamic routing adapts ETA to live traffic, static can't [check]
Common Mistakes:
4. You must design a load balancer for a global web app that requires session stickiness and fault tolerance. Which approach is best?
medium
A. Use IP Hash to ensure session stickiness but risk uneven load distribution
B. Use Round Robin to evenly distribute requests without session affinity
C. Use Least Connections with a centralized session store for stickiness
D. Use Weighted Round Robin with weights adjusted dynamically per server health

Solution

  1. Step 1: Identify session stickiness requirement

    Session stickiness requires requests from the same client to reach the same server to maintain session state.
  2. Step 2: Evaluate options for stickiness and fault tolerance

    Round Robin ignores stickiness; Least Connections needs external session store adding complexity; Weighted Round Robin adjusts load but not stickiness. IP Hash provides stickiness by hashing client IP.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    IP Hash ensures session stickiness but may cause uneven load distribution [check]
Hint: IP Hash ensures stickiness but risks uneven load distribution [check]
Common Mistakes:
5. At 10x scale, which component in the code repository system is most likely to become the bottleneck first, and why?
hard
A. The branch metadata store, because it is a centralized stateful service with high write contention
B. The merge service, because merge conflict resolution requires CPU-intensive three-way diffs
C. The object storage cluster, because storing large blobs grows linearly with commits
D. The pull request notification system, because fan-out to millions of users overwhelms messaging queues

Solution

  1. Step 1: Identify bottleneck candidates at 10x scale

    Branch metadata store handles all branch pointer updates; centralized and stateful, it faces high write contention.
  2. Step 2: Compare other components

    Object storage scales horizontally; merge service CPU load is distributed; notification system can be sharded and throttled.
  3. Step 3: Conclude bottleneck

    Branch metadata store's centralized writes limit throughput and cause latency spikes first.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Centralized branch metadata store -> write contention bottleneck [check]
Hint: Centralized metadata store hits write contention first [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard