Question #19 of 60•Databases•Hard• ~3 min read

How do you scale search queries on a large product catalog (Elasticsearch)?

Executive Takeaway

Deconstruct Elasticsearch's inverted index, search document structures, shard routing, and replica node query splits.

Architectural Breakdown & AnalysisFAANG Aligned

SQL LIKE '%keyword%' queries cause full table scans that fail at scale. Elasticsearch uses an Inverted Index for instant fuzzy searches and faceted filtering.

1. Inverted Index Mechanics

A dictionary mapping every unique word to the specific document IDs containing it. Searching "Wireless Noise Canceling Headphones" intersects candidate lists in milliseconds.

2. Sharding & Replica Distribution

  • Primary Shards: Document collections are partitioned across primary shards (hash(_id) % primary_shards).
  • Replica Shards: Read-only copies of primary shards. Replicas scale concurrent search queries horizontally and guarantee failover.
WARNING — Primary Shard Immutability

Primary shard count cannot be changed after index creation without re-indexing. Plan primary shard sizing based on 3-year data growth projections.

Key Architectural Comparison

Catalog Search PatternQuery EngineSearch LatencyFuzzy / Typo SupportMulti-Facet Filters
SQL `LIKE '%term%'`Relational DB$> 1,000\text{ms}$ (Full scan).None.Slow (requires multi-index scans).
Elasticsearch (Lucene)Inverted Index$< 30\text{ms}$.Levenshtein edit distance built-in.Instantaneous facet aggregations.
Vector Search (ANN)HNSW Graph Index$< 50\text{ms}$.Semantic conceptual matching.Requires hybrid vector + scalar filter.
Previous QuestionHow do you design a resilient retry mechanism with exponential backoff and jitter?Next Question How do you design a secure API Gateway for routing, rate limiting, and SSL termination?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.