Skip to main content

Search Systems

Reference: Sujeet Pro β€” Distributed Search Engine


Why Not Just Use SQL?

-- SQL LIKE is O(n) β€” full table scan, no ranking
SELECT * FROM products WHERE description LIKE '%bluetooth speaker%';
-- 1M products β†’ scans all 1M rows, no relevance score

Elasticsearch advantages:

  • Inverted index β†’ O(1) term lookup
  • Relevance scoring (TF-IDF, BM25)
  • Fuzzy matching, stemming, synonyms
  • Near real-time search (1s delay after index)
  • Horizontal scaling built-in

Inverted Index

Documents:
Doc1: "bluetooth speaker portable"
Doc2: "wireless speaker home"
Doc3: "bluetooth headphones portable"

Inverted Index:
"bluetooth" β†’ [Doc1, Doc3]
"speaker" β†’ [Doc1, Doc2]
"portable" β†’ [Doc1, Doc3]
"wireless" β†’ [Doc2]
"headphones" β†’ [Doc3]

Query "bluetooth speaker":
"bluetooth" β†’ [Doc1, Doc3]
"speaker" β†’ [Doc1, Doc2]
Intersection β†’ Doc1 (highest score: matches both terms)

Elasticsearch Concepts

ConceptSQL Equivalent
IndexTable
DocumentRow
FieldColumn
MappingSchema
ShardPartition
ReplicaRead replica

Index Settings

PUT /products
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stop", "snowball"]
}
}
}
},
"mappings": {
"properties": {
"name": { "type": "text", "analyzer": "my_analyzer" },
"name_keyword": { "type": "keyword" },
"price": { "type": "float" },
"category": { "type": "keyword" },
"description": { "type": "text" },
"tags": { "type": "keyword" },
"created_at": { "type": "date" }
}
}
}

Common Query Patterns

Full-Text Search with Boost

GET /products/_search
{
"query": {
"multi_match": {
"query": "bluetooth speaker",
"fields": ["name^3", "description", "tags^2"],
"type": "best_fields",
"fuzziness": "AUTO"
}
}
}

Boolean Query

GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "speaker" } }
],
"filter": [
{ "range": { "price": { "gte": 50, "lte": 200 } } },
{ "term": { "category": "electronics" } },
{ "term": { "in_stock": true } }
],
"should": [
{ "match": { "tags": "featured" } }
]
}
},
"sort": [
{ "_score": "desc" },
{ "created_at": "desc" }
]
}

Faceted Search (Aggregations)

GET /products/_search
{
"query": { "match": { "name": "speaker" } },
"aggs": {
"by_category": {
"terms": { "field": "category", "size": 10 }
},
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 50 },
{ "from": 50, "to": 100 },
{ "from": 100, "to": 200 },
{ "from": 200 }
]
}
},
"avg_price": { "avg": { "field": "price" } }
}
}

Spring Data Elasticsearch

// Document mapping
@Document(indexName = "products")
@Setting(settingPath = "es-settings.json")
public class ProductDocument {
@Id
private String id;

@MultiField(
mainField = @Field(type = FieldType.Text, analyzer = "english"),
otherFields = @InnerField(suffix = "keyword", type = FieldType.Keyword)
)
private String name;

@Field(type = FieldType.Float)
private BigDecimal price;

@Field(type = FieldType.Keyword)
private String category;

@Field(type = FieldType.Date)
private Instant createdAt;
}

// Repository
public interface ProductSearchRepository
extends ElasticsearchRepository<ProductDocument, String> {

@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"name^3\", \"description\"]}}")
Page<ProductDocument> search(String query, Pageable pageable);
}

// Service with custom query
@Service
public class ProductSearchService {
@Autowired private ElasticsearchOperations esOps;

public SearchResult<ProductDocument> search(ProductSearchRequest req) {
Query query = NativeQuery.builder()
.withQuery(q -> q.bool(b -> b
.must(m -> m.multiMatch(mm -> mm
.query(req.getKeyword())
.fields("name^3", "description", "tags^2")
.fuzziness("AUTO")
))
.filter(f -> f.range(r -> r
.field("price")
.gte(JsonData.of(req.getMinPrice()))
.lte(JsonData.of(req.getMaxPrice()))
))
))
.withPageable(PageRequest.of(req.getPage(), req.getSize()))
.build();

return esOps.search(query, ProductDocument.class);
}
}

Autocomplete / Search-as-You-Type

Completion Suggester

PUT /products
{
"mappings": {
"properties": {
"name_suggest": {
"type": "completion",
"analyzer": "simple",
"search_analyzer": "simple"
}
}
}
}

// Index document with suggestions
{
"name": "Bluetooth Speaker",
"name_suggest": {
"input": ["Bluetooth Speaker", "bluetooth speaker", "speaker"],
"weight": 100
}
}

// Query
GET /products/_search
{
"suggest": {
"product_suggest": {
"prefix": "blue",
"completion": {
"field": "name_suggest",
"size": 5,
"fuzzy": { "fuzziness": 1 }
}
}
}
}

Edge N-Gram (Type-ahead)

Better for partial word matching:

"analysis": {
"tokenizer": {
"edge_ngram_tokenizer": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
}
}
// "blue" β†’ "b", "bl", "blu", "blue"
// Searching "blu" matches "blue", "bluetooth", etc.

Search Architecture

User Input β†’ Search API
↓
Query Parser (handle special chars, operators, quotes)
↓
Query Builder (build ES query with filters, boosts)
↓
Elasticsearch Cluster
(5 shards, 1 replica each = 10 nodes total for 100M docs)
↓
Result Ranker (personalization, A/B test scoring)
↓
Response Formatter
↓
Client

Data Sync: DB β†’ Elasticsearch

Option 1: Dual-write (write to DB + ES in same transaction)
Risk: partial failure β†’ inconsistency

Option 2: CDC (Change Data Capture) via Debezium
DB binlog β†’ Debezium β†’ Kafka β†’ ES Consumer
Reliable, near real-time (~1s lag)

Option 3: Batch sync (nightly full reindex)
Simple, but stale data during day
// Debezium + Kafka β†’ ES indexer
@KafkaListener(topics = "dbserver1.public.products")
public void onProductChange(DebeziumMessage msg) {
if ("DELETE".equals(msg.getOp())) {
productSearchRepository.deleteById(msg.getBefore().getId());
} else {
ProductDocument doc = mapper.toDocument(msg.getAfter());
productSearchRepository.save(doc);
}
}

Relevance Tuning

TechniqueEffect
Field boostingName matches > description matches
Freshness boostRecent products scored higher
Popularity boostHigh-view products ranked up
PersonalizationUser's history influences ranking
Synonym expansion"TV" matches "television"
Stemming"running" matches "run", "runs"
Fuzzy matching"speker" matches "speaker"

Distributed Search Engine Internals (Lucene, FST, BM25, WAND & Sharding)

Reference: Sujeet Pro β€” Distributed Search Engine

When designing a hyperscale search engine (Elasticsearch, OpenSearch, Apache Solr, or custom Lucene clusters) over billions of documents, a Staff/Principal engineer must explain how the storage engine compresses inverted indexes into RAM, achieves 1-second Near Real-Time (NRT) visibility, prunes millions of postings via Block-Max WAND, and executes distributed Scatter-Gather queries.

1. Inverted Index Internals: FST Term Dictionary, FOR Delta Encoding & Roaring Bitmaps

A production inverted index does not store a naive HashMap<String, List<Integer>> in memory (which would immediately trigger OutOfMemoryError at 100M+ unique terms). Instead, Apache Lucene splits the index into three ultra-compressed physical layers:

[1. Term Index (FST in RAM)] [2. Term Dictionary (.tim)] [3. Posting Lists (.doc / .pos)]
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Finite State Transducer β”‚ β”‚ On-Disk Sorted Terms β”‚ β”‚ Compressed DocIDs + Term Freqs β”‚
β”‚ "bl" ──► Block Offset β”‚ ───► β”‚ β€’ "bluetooth" (DF: 42k) β”‚ ──► β”‚ β€’ FOR Delta: [102, +3, +14, +2] β”‚
β”‚ "sp" ──► Block Offset β”‚ β”‚ β€’ "speaker" (DF: 91k) β”‚ β”‚ β€’ Roaring Bitmap (Filter Cache) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Term Index via Finite State Transducer (FST in Heap RAM):
    • Storing every unique string term in RAM is prohibitively expensive. Lucene builds a Finite State Transducer (FST)β€”a directed acyclic word graph (automaton) that shares both common prefixes and common suffixes across terms while mapping each prefix to the disk block offset of the .tim Term Dictionary.
    • Memory Footprint: Compresses the in-memory term index by 10x–40x, allowing a shard with billions of documents to keep its entire FST prefix index in just a few dozen megabytes of JVM heap.
  2. Posting List Compression via Frame-of-Reference (FOR) Delta Encoding:
    • A Posting List stores sorted monotonic Document IDs ([1004, 1007, 1015, 1020, ...]) plus Term Frequencies (tf), Positions (pos for phrase queries), and Offsets.
    • Instead of storing 32-bit integers (4 bytes each), Lucene computes Delta Gaps between consecutive IDs ([1004, 3, 8, 5, ...]), groups deltas into blocks of 128 integers, and packs every delta in the block using the bit-width of the maximum delta in that block (Frame of Reference [FOR] / SIMD-BP128 bit-packing). If the max delta in a 128-doc block is 12, every DocID takes only 4 bits instead of 32 bits!
  3. Roaring Bitmaps for Filter Caching & Set Intersections:
    • When executing bool.filter clauses (e.g., in_stock = true AND category = "electronics"), Lucene caches the matching Document ID set as a Roaring Bitmap inside the OS/Node Filter Cache:
      • Divides the 32-bit DocID space into chunks of 216=65,5362^{16} = 65,536 integers (keyed by the upper 16 bits).
      • Sparse Chunk (<4,096< 4,096 docs): Stored as a sorted uint16[] array (2 bytes per doc).
      • Dense Chunk (β‰₯4,096\ge 4,096 docs): Stored as a fixed 65,536-bit65,536\text{-bit} (8 KB) bitset enabling single-instruction CPU AVX-512 bitwise AND/OR intersections.
      • Contiguous Runs: Compressed via Run-Length Encoding (RLE).

2. Lucene Immutable Segments, Tiered Merging & Near Real-Time (NRT) Lifecycle

Why are Lucene segments strictly immutable, and how does Elasticsearch make newly indexed documents searchable within 1 second without calling expensive disk fsync on every write?

Incoming Document Write (PUT /products/_doc/1)
β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ί [1. In-Memory IndexBuffer]
β”‚ β”‚
β”‚ (Every 1s: Refresh)
β”‚ β–Ό
β”‚ [2. OS Page Cache Segment] ◄── Searchable in 1s (NRT)!
β–Ό β”‚
[3. Translog (WAL)] (Every 30m or Translog Full: Flush / Lucene Commit)
(fsync every 5s or per-request) β–Ό
└──────────────────────────────────────► [4. Physical Disk Segment (.cfs/.doc) + Truncate Translog]
  1. Why Immutability? Rewriting compressed FSTs and delta-encoded posting lists in-place when a single document changes is physically impossible without corrupting bit-packed offsets. Therefore, Lucene writes new documents into new, immutable mini-inverted indexes called Segments:
    • Lock-Free Concurrent Reads: Because segments never mutate, search threads require zero locks and rely heavily on the Linux OS Page Cache (mmap).
    • Deletes & Updates: Deleting a document merely flips a bit in a per-segment .liv (Live Docs Bitset) file. Updating a document atomically marks the old doc bit as deleted in .liv and appends the new version into a new segment.
  2. The 4-Stage Near Real-Time (NRT) Indexing Lifecycle:
    • Stage 1 β€” IndexBuffer + Translog Append: Incoming documents are parsed/analyzed in the JVM IndexBuffer and simultaneously appended to an append-only Write-Ahead Log (translog) for crash durability. At this moment, the document is NOT yet searchable.
    • Stage 2 β€” Refresh (Default every 1s): Once per second, the IndexBuffer is written into a new immutable Lucene segment inside the Linux OS Page Cache (without waiting for a slow disk fsync!) and opened by an IndexReader. The document is now searchable within 1 second (NRT)!
    • Stage 3 β€” Translog fsync: Configured either per-request (index.translog.durability: request) or asynchronously every 5s (async) to guarantee zero data loss on power failure.
    • Stage 4 β€” Flush / Lucene Commit: Every 30 minutes (or when translog exceeds 512 MB), all in-memory OS page cache segments are fsynced to physical disk, a new commit point (segments_N) is written, and the old translog is truncated.
  3. Tiered Segment Merging (Background Compaction):
    • Creating a new segment every 1 second produces thousands of tiny segments, slowing down search (since a query must check every segment) and wasting file descriptors.
    • A background TieredMergePolicy merges similarly sized small segments into larger segments, physically purging tombstoned documents marked in .liv and reclaiming disk space.

3. Relevance Scoring: Okapi BM25 Math & Block-Max WAND Early Termination

β‘  Why BM25 Replaced Classic TF-IDF​

In classic TF-IDF, if the word "speaker" appears 50 times in a spammy product description, its Term Frequency (TFTF) score is 50Γ—50\times higher than a concise document mentioning "speaker" once. Okapi BM25 (Best Matching 25) fixes this via asymptotic TF saturation and document length normalization:

ScoreBM25(D,Q)=βˆ‘t∈QIDF(t)β‹…f(t,D)β‹…(k1+1)f(t,D)+k1β‹…(1βˆ’b+bβ‹…βˆ£D∣avgdl)\text{Score}_{\text{BM25}}(D, Q) = \sum_{t \in Q} \text{IDF}(t) \cdot \frac{f(t, D) \cdot (k_1 + 1)}{f(t, D) + k_1 \cdot \left(1 - b + b \cdot \frac{|D|}{\text{avgdl}}\right)}

Where:

  • IDF(t)=ln⁑(1+Nβˆ’n(t)+0.5n(t)+0.5)\text{IDF}(t) = \ln\left(1 + \frac{N - n(t) + 0.5}{n(t) + 0.5}\right): Inverse Document Frequency (NN = total docs in shard, n(t)n(t) = docs containing term tt). Rare terms ("sennheiser") carry massive weight; common terms ("the") approach 0.
  • f(t,D)f(t, D): Raw Term Frequency of term tt in document DD.
  • k1k_1 (TF Saturation Parameter, default 1.21.2): Controls how quickly additional occurrences of a term hit diminishing returns. Even as f(t,D)β†’βˆžf(t, D) \to \infty, the term's TF factor asymptotically caps at k1+1=2.2k_1 + 1 = 2.2.
  • bb (Document Length Normalization, default 0.750.75): Penalizes long documents (∣D∣>avgdl|D| > \text{avgdl}) that match terms purely by verbosity, while setting b=0b = 0 disables length normalization (useful for short SKU titles).

β‘‘ WAND (Weak AND) & Block-Max WAND (Lucene 8+ Dynamic Pruning)​

If a user searches "bluetooth speaker" and 5,000,000 documents match, evaluating the floating-point BM25 formula on all 5 million documents to return only the Top K=10K=10 results wastes 99.9% of CPU cycles:

  • WAND (Broder et al.): Maintains the current minimum score ΞΈ\theta in the Top-KK Min-Heap and stores the Maximum Possible Impact Score (MaxScoret\text{MaxScore}_t) for each query term across the entire segment. By sorting posting cursors by current DocID and summing MaxScoret\text{MaxScore}_t until the sum exceeds ΞΈ\theta, WAND skips (advance(pivotDocId)) millions of documents that cannot mathematically beat ΞΈ\theta.
  • Block-Max WAND (BMW): Global segment MaxScoret\text{MaxScore}_t is often skewed by one single outlier document. Block-Max WAND stores the upper-bound BM25 impact score inside every 128-document FOR block. During traversal, if the sum of block-level max scores for the current 128-doc range is ≀θ\le \theta, Lucene skips the entire 128-document block without decompressing it, accelerating Top-10 queries by 3x–10x!

4. Distributed Partitioning: Document-Based vs. Term-Based & Scatter-Gather Execution

How should a 10-billion-document inverted index be partitioned across 100 cluster nodes?

Partitioning StrategyHow It WorksQuery Execution FlowPros & Cons
Document-Based Partitioning (Local Index β€” Elasticsearch / OpenSearch Default)Documents are sharded by hash(doc_id) % N. Each shard holds a complete, self-contained Local Inverted Index for its 1/N1/N subset of documents.Scatter-Gather: Coordinator broadcasts query to all NN shards in parallel; each shard computes its local Top-KK; coordinator merges NΓ—KN \times K into global Top-KK.Pros: Fast single-shard document indexing; fault isolation (1 dead shard = Nβˆ’1/NN-1/N results still available).
Cons: Every query hits all shards; IDF skew on small shards.
Term-Based Partitioning (Global Index)The inverted index is partitioned by Term: Shard 1 holds posting lists for terms "a".."f", Shard 2 holds "g".."m", etc.Coordinator routes query only to the shards holding the specific query terms ("bluetooth" shard + "speaker" shard).Pros: High throughput for single-term queries; exact global IDF.
Cons: Catastrophic Multi-Term Network Join (must ship multi-megabyte posting lists across the network to intersect AND queries); indexing 1 doc with 200 words writes to all shards!

The Two-Phase Scatter-Gather Protocol (QUERY_THEN_FETCH vs. DFS_QUERY_THEN_FETCH)​

In Document-Based Partitioning, fetching full JSON _source payloads from all NN shards would saturate network bandwidth. Instead, Elasticsearch executes a Two-Phase Scatter-Gather:

  1. Phase 1 β€” Query Phase:
    • Client sends GET /products/_search?size=10 to any node (which acts as the Coordinating Node).
    • Coordinating Node fans out the lightweight query to a primary or replica of all NN shards.
    • Each shard executes Block-Max WAND over its local segments and returns only the Top 10 (doc_id, _score) pairs (just ~160 bytes per shard!).
  2. Phase 2 β€” Fetch Phase:
    • The Coordinating Node merges the NΓ—10N \times 10 lightweight score tuples in a Priority Queue, picks the true Global Top 10, and sends a targeted GET _source request only to the specific shards holding those 10 winning documents.
  3. The IDF Skew Problem & DFS_QUERY_THEN_FETCH:
    • By default (QUERY_THEN_FETCH), each shard calculates IDF(t)\text{IDF}(t) using only its local shard document count, which can skew rankings if rare terms cluster unevenly on one shard.
    • Setting search_type=dfs_query_then_fetch adds a pre-round trip (Distributed Frequency Search [DFS]) that gathers term frequencies across all shards first to compute the exact Global IDF before scoringβ€”at the cost of an extra network RTT.

Interview Questions

A: Search engines use analyzers, inverted indexes, and ranking algorithms optimized for linguistic matching. SQL LIKE is limited, often scans heavily, and lacks relevance scoring.

A: An inverted index maps terms to posting lists of matching document IDs. Queries become fast set operations on postings instead of scanning full documents.

Q: How do you keep Elasticsearch in sync with your primary database?

A: Publish DB changes through CDC or outbox events and update Elasticsearch asynchronously with retries. Keep reindex/replay tooling for drift recovery.

Q: What is BM25 and how does it improve over TF-IDF?

A: BM25 is a probabilistic ranking function that better normalizes term frequency and document length. It usually produces more relevant rankings than plain TF-IDF defaults.

Q: How would you design an autocomplete/search-as-you-type feature?

A: Index edge n-grams or use completion suggester with popularity signals and typo tolerance. Cache top prefixes and debounce client queries.

Q: What is faceted search and how do you implement it?

A: Faceted search shows aggregated counts by attributes (brand, price range, category) alongside results. Implement with filtered aggregations over indexed keyword/numeric fields.

Q: How do you handle Elasticsearch going down while still accepting writes to your primary DB?

A: Keep DB as source of truth, queue indexing events durably, and replay when cluster recovers. Degrade gracefully by serving fallback search modes if needed.

Q: How would you scale Elasticsearch to handle 1 billion product documents?

A: Plan shard count by index size/throughput, use index lifecycle tiers, and separate ingest from query workloads. Optimize mappings, refresh intervals, and replica strategy for SLA.

Q: What are the trade-offs of Elasticsearch's near real-time (~1s) indexing?

A: NRT improves throughput by batching refreshes but introduces short visibility delay after writes. Lower refresh intervals reduce delay but increase indexing overhead.

Q: How do you tune relevance in search results?

A: Tune analyzers, field boosts, synonym handling, and business signals (freshness/popularity). Evaluate with offline judgments and online A/B metrics like CTR and conversion.


Case Study: Hotel Search at Scale

From the freeCodeCamp Design Software Systems Like a Senior Engineer course.

  • Index one document per hotel with a geo_point, amenities, rating, and a denormalised price and availability summary.
  • Query with a geo_distance filter plus terms filters for amenities and a range filter for price. Sort by relevance blended with price or rating.
  • Date availability is the hard filter. Keep a precomputed per-night availability summary in the index, and treat it as approximate.
  • Feed the index from inventory.changed events (outbox or CDC). A lag of a few seconds is acceptable.
  • Search is a hint, booking is the truth. The booking path re-checks the OLTP database at hold time, so staleness costs a rare "just sold out" message, never a double booking.

See also: OpenSearch Architecture Deep Dive & Splunk-to-OpenSearch Migration Guide, OpenSearch Query, Search & Aggregation Guideline, OpenSearch Indexing, Schema Design & Day-2 Operations Guideline, Senior Engineer Design Course, Inventory Reservation System.


Senior/Staff Interview Playbook: How to Ace Distributed Search Engine Interviews

Reference: Sujeet Pro β€” Distributed Search Engine

1. The Senior Framing Strategy (First 60 Seconds)

When asked to "Design a Distributed Search Engine (like Elasticsearch, E-Commerce Product Search, or Twitter Search)", establish these four architectural pillars in the first 60 seconds:

  • State Non-Negotiable Invariants Upfront:
    1. Search is an Eventually Consistent Read Projection (Never the System of Record): The OLTP database owns transactional truth; the search cluster ingests mutations asynchronously via CDC (Debezium + Kafka) with a 1-second Near Real-Time (NRT) visibility SLA.
    2. Immutable Segment Architecture: Inverted index segments are written once and never mutated in placeβ€”enabling lock-free concurrent reads cached directly in the Linux OS Page Cache (mmap).
  • Back-of-the-Envelope Capacity & Memory Physics: Never allocate 100% of server RAM to the JVM Heap! Explicitly state the 50% JVM Heap / ≀31Β GB\le 31\text{ GB} Compressed Oops Rule: on a 64 GB RAM search node, cap JVM Heap at 30–31 GB (to keep 64-bit object pointers compressed to 32-bit CompressedOops) for the FST Term Index, Aggregation Heap, and IndexBuffer, leaving the remaining 32+ GB of physical RAM to the Linux OS Page Cache to hold memory-mapped .doc/.tim inverted index segments. Also size shard counts so each shard stays between 20 GB and 50 GB.
  • Proactive Failure-Mode Identification: Call out the four production failure modes before the interviewer asks:
    1. Scatter-Gather Tail-Latency Amplification: Fanning a query out to 100 shards means global latency equals the slowest shard (P99P99 of 100 shards β‰ˆP99.99\approx P99.99 of a single shard)β€”mitigated via Adaptive Replica Selection (ARS) and custom Routing Keys (_routing=tenant_id).
    2. Deep Pagination OOM (from: 100000, size: 10): Forcing every shard to sort and return 100,010 tuples to the coordinating node (solved via search_after + Point-in-Time [PIT] cursor pagination).
    3. High-Frequency Refresh Thrashing: Refreshing on every write creates thousands of tiny segments and starves disk I/O with background merges.
    4. Unbounded Mapping Explosion: Dynamic JSON fields creating 50,000+ mapping attributes and crashing the cluster state manager.
  • Day-2 Operational Realism: Mention Zero-Downtime Alias Reindexing (products_v1 -> products_v2 behind read alias products_read) for mapping changes, Tiered Segment Merging, and Block-Max WAND pruning for sub-50ms P99 query SLAs.

2. Classic Interviewer Traps & Principal Rebuttals

Trap 1: "Why does Elasticsearch take 1 full second (refresh_interval: 1s) to make a document searchable? Why don't we just fsync the inverted index to disk on every POST /doc so search is strongly consistent and instant?"​

  • Junior/Mid Answer: "Disk fsync is slightly slow, so Elasticsearch waits 1 second to batch writes to disk."
  • Principal Rebuttal:

    "The 1-second refresh does NOT even fsync segments to disk! Constructing an inverted index segment (sorting terms lexicographically, building the Finite State Transducer [FST], and delta-encoding posting lists with Frame-of-Reference [FOR]) is CPU- and memory-intensive. If we generated and fsynced a new immutable Lucene segment on every single document write, the node would create thousands of tiny segment files per second, exhausting file descriptors and crushing disk IOPS.

    Instead, Lucene decouples Search Visibility (refresh) from Disk Durability (translog + flush):

    1. Every write appends to an append-only Translog (WAL) (which handles crash durability) and enters an in-memory IndexBuffer.
    2. Every 1 second (refresh), the IndexBuffer is compiled into a new immutable segment written only to the Linux OS Page Cache (zero disk fsync), making it immediately readable by IndexReader.
    3. Only during a periodic flush (Lucene commit) are those OS page cache segments actually fsynced to physical disk and the translog truncated. For bulk backfills, we temporarily set refresh_interval: 30s or -1 to increase indexing throughput by 5x."

Trap 2: "To scale our search engine to 10 billion documents, shouldn't we partition the index by Term (Shard 1 gets 'a–d', Shard 2 gets 'e–h') so a query only touches the shards for the words in the query instead of all 100 shards?"​

  • Junior/Mid Answer: "Yes! Term-based partitioning is much more scalable because a 2-word query only hits 2 shards instead of broadcasting to all 100 shards."
  • Principal Rebuttal:

    "While Term-Based Partitioning (Global Index) looks attractive on paper, it fails catastrophically for interactive multi-term search due to Cross-Network Posting List Joins and Write Amplification:

    1. Read Path Network Bottleneck: Suppose a user searches 'bluetooth AND speaker'. Shard A holds the posting list for 'bluetooth' (5 million DocIDs) and Shard B holds 'speaker' (8 million DocIDs). To intersect them, Shard A must serialize and transmit megabytes of posting lists over the network to Shard B or the coordinator on every query, destroying sub-50ms latency SLAs.
    2. Write Path Fanout: Indexing a single document containing 300 unique words requires a distributed write transaction across nearly all 100 term shards!

    With Document-Based Partitioning (Local Index), each shard holds a complete inverted index for its local document slice. Every shard intersects 'bluetooth AND speaker' locally in RAM/CPU cache using Roaring Bitmaps and Block-Max WAND, returning only the top 10 (doc_id, score) tuples (~160 bytes) to the coordinator."

Trap 3: "What happens when a user clicks 'Page 5,000' (from: 50000, size: 10) on a 20-shard Elasticsearch cluster, and how do you fix it?"​

  • Junior/Mid Answer: "Each shard skips the first 50,000 results and returns 10 results, which is fast because the index is sorted."
  • Principal Rebuttal:

    "A shard cannot skip its first 50,000 local results because the coordinator doesn't know which shard holds the globally top-ranked documents! If all 50,000 globally highest-scoring documents happen to reside on Shard 3, skipping on Shard 3 would lose the actual results.

    Consequently, in a 20-shard cluster, from: 50000, size: 10 forces every single shard to score, sort, and return its Top 50,010 tuples to the Coordinating Node. The Coordinating Node must allocate a priority queue in JVM heap for 20Γ—50,010=1,000,20020 \times 50,010 = \mathbf{1,000,200} tuples just to discard 1,000,190 of them and return 10! That is why Elasticsearch enforces a hard guard at index.max_result_window = 10000.

    In production, we ban deep from/size offset pagination and use search_after with a Point-in-Time (PIT) snapshot: the client passes the sort tuple [_score, _id] of the last item on the current page. Each shard filters strictly for entries after that cursor and returns only 10 items (20Γ—10=20020 \times 10 = 200 items merged at the coordinator), keeping latency constant at O(K)O(K) regardless of page depth."

Trap 4: "If a query term like 'wireless' matches 20 million documents on a shard, does Lucene compute the floating-point BM25 score for all 20 million documents?"​

  • Junior/Mid Answer: "Yes, it iterates through the posting list for 'wireless', calculates BM25 for each matching document, and keeps the top 10 in a min-heap."
  • Principal Rebuttal:

    "Scoring 20 million documents in floating-point math on every query would blow past our 50ms SLA. Modern Lucene (8.0+) uses Block-Max WAND (Weak AND) whenever track_total_hits is not forced to true (default caps exact count at 10,000).

    Every 128-document Frame-of-Reference (FOR) block in the posting list stores a precomputed Block-Max Impact Score (the maximum possible BM25 contribution of any document in that block, given max tf and min doc length). During query evaluation, Lucene compares the sum of block-max upper bounds against the current 10th-best score ΞΈ\theta in the Top-10 Min-Heap. If a 128-document block cannot mathematically beat ΞΈ\theta, Lucene skips the entire compressed block without even decoding its DocIDs, routinely pruning 90%+ of matching postings."


3. Quick Reference β€” Mid-Level vs. Principal Candidate Cheat Sheet

Concept / ScenarioMid-Level Candidate SaysPrincipal Candidate Demonstrates
Inverted Index Memory Layout"Maps each word in a hash map to a list of document IDs.""Details the 3-tier physical layout: FST (Finite State Transducer) prefix index in JVM heap β†’\rightarrow .tim Term Dictionary β†’\rightarrow FOR (Frame-of-Reference) 128-block delta-encoded postings + Roaring Bitmaps in OS Page Cache."
Near Real-Time (NRT) Writes"Elasticsearch indexes documents in 1 second.""Traces the 4-stage lifecycle: IndexBuffer + Translog WAL β†’\rightarrow 1s Refresh (writes immutable segment to OS Page Cache without fsync) β†’\rightarrow 5s Translog fsync β†’\rightarrow Periodic Flush/Commit + Tiered Segment Merging."
Relevance Ranking & Pruning"Uses TF-IDF or BM25 to score documents.""Writes the BM25 equation explaining TF saturation (k1=1.2k_1=1.2) and length normalization (b=0.75b=0.75), paired with Block-Max WAND early termination to skip non-competitive 128-doc blocks."
Sharding & Query Execution"Split the index into shards and query them.""Contrasts Document Partitioning (Local Index) vs. Term Partitioning (Global Index), detailing two-phase Scatter-Gather (QUERY_THEN_FETCH vs. DFS_QUERY_THEN_FETCH)."
JVM Heap & Hardware Sizing"Give Elasticsearch as much JVM Heap RAM as possible.""Enforces the ≀31Β GB\le 31\text{ GB} JVM Heap CompressedOops boundary (50% RAM rule), dedicating the remaining 50%+ RAM to the Linux OS Page Cache for memory-mapped Lucene segments."
πŸ“–
Track Page Progress0 / 635 Read
Knowledge Base Completion0%