Design a search engine that returns 10 relevant results from 100 billion web pages in under 200ms. The core challenge is a multi-stage ranking pipeline: cheap boolean retrieval narrows billions to thousands, BM25 scoring picks candidates, then a neural model re-ranks the final set.
Type "how to fix a leaking faucet": 0.3 seconds later, 10 results from 100 billion pages. Behind that speed: an inverted index sharded across thousands of machines, a ranking pipeline that evaluates millions of candidates, and a query processor that understood you meant plumbing, not information leaks.
The defining challenge is the multi-stage ranking pipeline. You cannot run an expensive ML model on 100 billion documents, so you build a funnel: cheap boolean retrieval narrows billions to thousands, lightweight BM25 scoring picks the top candidates, then an expensive neural model re-ranks the final set. The core tension: relevance vs latency: more ranking stages improve result quality but cost time, and every millisecond matters at 100K QPS.
The numbers reveal why a relational database cannot serve search. 100 billion pages, each averaging 50KB of HTML. A naive SELECT * WHERE content LIKE '%query%' would scan 5PB of data per query. The inverted index exists to make this O(1) per term instead of O(N) per page.
Web Pages
Indexed pages: 100 billion. Average raw HTML size: 50KB. Total raw corpus: 5PB. We do not store raw HTML in the search system, only the inverted index and metadata.
One endpoint handles 100K QPS. A second powers autocomplete at keystroke speed. The search endpoint must support pagination, spell correction, and filtering. All without adding latency.
Three storage tiers, each optimized for a different access pattern. The inverted index on SSDs for sub-millisecond term lookups. Page metadata in a distributed key-value store for document-level retrieval. The forward index in memory for re-ranking.
Inverted Index (Two Layers)
Layer 1, Term dictionary (in memory): A hash map from each term to its posting list offset on disk plus the document frequency. 10 million terms at 20 bytes each = 200MB. This fits in RAM on every index server. Every query starts here. A memory lookup, not a disk seek.
Two completely independent data paths. The write path (crawl, process, index, build) runs continuously in the background, rebuilding the index as the web changes. The read path (query, retrieve, rank, serve) handles real-time user queries. They share the index but never block each other.
Write path: Crawler to Document Processor to Indexer to Index Servers. Read path: Client to Query Processor to Index Servers to Ranking Service and back.
Two flows define this system. The search query flow handles 100K QPS, is read-heavy and latency-critical. It must complete in 200ms. The indexing flow processes 10K pages per second, is write-heavy and throughput-critical. It must keep up with the rate of change on the web.
Search Query Flow
Query arrives, checked against cache, tokenized, scattered to index shards, BM25 scored, gathered, re-ranked by ML model, metadata fetched, top 10 returned.
"How do you return 10 results from 100 billion pages in 200ms?" is the real interview question. The answer is two mechanisms working together: an inverted index that makes term lookup O(1), and a multi-stage ranking pipeline that progressively filters candidates through increasingly expensive scoring functions.
Inverted Index Deep Dive
Term dictionary in RAM maps each term to a posting list offset on SSD. Posting list stores sorted docIDs with term frequencies. Skip pointers every 128 entries enable fast intersection.
Three cascading trade-offs shape the architecture: how to partition the index, how to balance ranking quality against latency, and how fresh the index needs to be.
Document-Partitioned vs Term-Partitioned Index
Document partitioning: every query fans out to all shards, each holding all terms for its document subset. Term partitioning: queries go only to relevant term shards, but popular terms create hotspots.
An index shard goes down: 1% of documents become invisible. A popular query overwhelms the ranking service. The crawler indexes a spam farm and pollutes results. Each failure mode requires a different defense.
Index Shard Failure
Each shard has 2-3 replicas. If the primary fails, a replica takes over and continues serving queries. During failover, that shard's documents remain searchable from the replica. If all replicas for a single shard fail, search results are degraded (documents on that shard are temporarily invisible) but the system keeps serving. Better to return 99% of results than to return nothing.
Four extensions beyond keyword search: understanding intent, searching beyond text, personalizing results, and answering questions directly.
Semantic and Vector Search
Embed queries and documents as high-dimensional vectors using models like sentence transformers. Find nearest neighbors in embedding space using approximate nearest neighbor (ANN) algorithms like HNSW or ScaNN. This enables "best pizza nearby" to match documents about "top-rated pizzeria in your area" even without shared keywords. The ANN index runs alongside the inverted index, keyword retrieval handles exact matches while vector search handles semantic matches. Results from both are merged in the ranking stage.