SD System Design In Depth
0%
lesson system-designsearch

Information retrieval system design

Module 5.4: Information Retrieval & Search Engines Updated Sep 17, 2026
📄 Cheat Sheet
How Search Really Works
ByteByteGo How Search Really Works Watch on YouTube ↗

Walks the full crawl, index, retrieve and rank pipeline end to end with clean animations, exactly the two-plane architecture this unit introduces.

0:00 / --:--

Under the Hood: Search Engine Architecture

Traditional relational databases optimize for exact-match point lookups and transactional ACID guarantees using B-Trees. An Information Retrieval (IR) system (such as Apache Lucene, Elasticsearch, and Google Search) is engineered for full-text search, unstructured natural language understanding, fuzzy matching, and multi-factor relevance ranking across billions of documents.

Dual-Plane IR Pipeline: Indexing vs Query Serving

flowchart TD subgraph IngestPipeline ["1. Offline / Streaming Ingest Pipeline"] Docs["Raw Documents (HTML, JSON, DB Records)"] --> Tokenizer["Tokenization & Normalization"] Tokenizer --> Stemmer["Linguistic Stemming (Porter / Snowball)"] Stemmer --> TermDict["Term Dictionary & Inverted Index Construction"] TermDict --> Segment["Flush Immutable Lucene Segments to Disk"] end subgraph QueryPipeline ["2. Online Query Serving Pipeline"] UserQuery["User Search: 'distributed cache redis'"] --> QP["Query Parser & Analyzer"] QP --> TermLookup["Term Dictionary Inverted Index Seek"] TermLookup --> Intersect["Postings List Intersection (Skip Pointers / Roaring)"] Intersect --> Ranker["Relevance Scoring (BM25 + Learning-to-Rank)"] Ranker --> TopK["Return Top 20 Results with Highlights & Facets"] end

Under the Hood: The Indexing Anatomy

  • Character Filters: Strip HTML markup, decode entities (e.g. & -> &), and map custom regex patterns.
  • Tokenizer: Breaks continuous text streams into discrete linguistic tokens based on word boundaries, punctuation, and Unicode script rules.
  • Token Filters: Lowercases tokens, removes stop words (optional), expands synonyms, and applies stemming algorithms (reducing "running", "runs", "ran" to the common root "run").

Worked example: a 200 ms search latency budget

Suppose an e-commerce site indexes 200 million products and promises p99 search latency of 200 ms at the edge. A realistic budget looks like this:

StageBudgetWhat happens
Network and TLS to the edge40 msClient to nearest region
Query parsing and understanding10 msAnalyzer, spell check, entity tagging
Candidate retrieval40 msScatter to shards, postings intersection, BM25, local top 1,000
Re-ranking50 msML model scores roughly 1,000 candidates using hundreds of features
Fetch and highlight20 msLoad stored fields for the final 20 hits
Headroom40 msGC pauses, retries, slow shards

The key design insight is the funnel: cheap scoring on millions of postings, expensive scoring on only a thousand candidates, and full document fetch for only the final page.

Search engine vs relational database

ConcernRelational DBSearch engine such as Lucene
Core structureB-tree on keysInverted index of term to sorted doc IDs
Answer typeExact set of matching rowsRanked list, usually top K
WritesIn-place, transactionalBuffered into immutable segments, visible after refresh
ConsistencyRead-your-writesNear-real-time, typically about 1 s behind
Best atJoins, constraints, updatesFull text, fuzzy match, facets, relevance

Failure modes to plan for

  • Analyzer drift: changing a tokenizer or synonym list without reindexing makes old and new documents disagree about what a term is.
  • Hot queries: a small set of head queries dominates traffic; a result cache in front of the engine often absorbs a large share of load.
  • Slow shard tail: a query waits for the slowest of N shards, so p99 per shard matters more than the average.

Real systems follow this shape: Elasticsearch and OpenSearch wrap Lucene segments with a distributed coordinator; Google described serving its index from memory across thousands of machines with a multi-level tree of root, parent and leaf servers.

Check yourself Question 1 of 3
Shared analyzer recall
From: Under the Hood: The Indexing Anatomy

Documents were indexed with a lowercase filter and a Porter stemmer, so the text "Running shoes" is stored as the terms run and shoe. A user searches for Running. Why must the query string go through the same token filters as the documents?

Key Takeaways

IR systems separate offline document indexing pipelines from online low-latency query scoring engines.
Text analysis pipelines convert raw text into normalized tokens via character filters, tokenizers, and stemmers.
Search engines trade off transactional ACID mutations for immutable index segments optimized for fast inverted lookups.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue