Design a web-scale search engine
A web search engine has three big subsystems: crawling the web, building an inverted index from what you crawl, and serving ranked results fast. Each is a hard distributed-systems problem; the interview usually focuses on the index + serving path.
Storage & Data, 10 components, about 45 min
1. Scope
In scope
- • Crawl and store web pages
- • Build an inverted index (term → documents)
- • Serve ranked results for a text query
- • Rank by relevance + importance signals
Out of scope
- • Spam/SEO adversarial defense internals
- • Personalization & user history
- • Image/video/vertical search
2. Functional requirements
Must have
- • Given a query, return relevant pages ranked by quality
- • Continuously crawl and refresh the index
- • Sub-second query latency
- • Deduplicate and respect robots.txt while crawling
Nice to have
- • Query autocomplete / spelling correction
- • Snippets / highlighting
- • Freshness boosting for news
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • 40B pages in the index, avg page ~100 KB raw
- • ~100K queries/second at peak
- • Average query touches ~3 terms
- • Compressed index ~5–10% of raw text size
Derived numbers
5. API design
Return ranked results + snippets
Query autocomplete
Add/update a document in the index
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
Document vs term sharding
Document sharding splits the corpus across shards; each query goes to ALL shards (scatter-gather), each returns local top-k, then you merge. Simple and balances load, but every query fans out widely. Term sharding puts whole posting lists on one shard; a query only hits shards for its terms, but multi-term intersection crosses the network and hot terms create hotspots. Most large engines use document sharding with heavy replication for QPS.
Scatter-gather query serving
The query service broadcasts the query to all (or relevant) index shards, each computes BM25 scores against its local postings and returns its top-k, and the aggregator merges to a global top-k before fetching snippets from the doc store. Latency is bounded by the slowest shard, so you replicate shards and hedge requests (send to two replicas, take the first).
Ranking: relevance + importance
Relevance (does the page match the words?) comes from TF-IDF/BM25 on the postings. Importance (is the page trustworthy?) comes from query-independent signals like PageRank computed offline on the link graph, plus freshness and click signals. Final score blends them; the expensive learned re-ranking runs only on the top few hundred candidates.
Crawling politely at scale
The URL frontier dedupes (bloom filter over seen URLs) and prioritizes (importance, freshness). Fetchers are partitioned by domain to enforce per-host politeness/robots.txt and avoid hammering one site. Crawled pages land in the doc store; a change-detection pass decides what to re-index so you spend crawl budget on pages that actually change.
9. Bottlenecks
- Query fan-out latency → replicate shards, hedge requests, cache hot queries
- Hot terms in term-sharding → prefer document sharding
- Crawl budget → prioritize frontier, change-detection to skip unchanged pages
- Index freshness vs cost → tiered indexes (fast small fresh + large slow base)
10. Trade-offs
Index partitioning
Choose: Document-partitioned index: each shard indexes a subset of documents; queries go to all shards and results merge.
Instead of: Term-partitioned: each shard owns some terms; fewer shards per query, but multi-term queries cross shards and hot terms create hotspots.
Document partitioning balances load and makes adding documents local; scatter-gather cost is controlled with replicas.
Ranking in one pass or a cascade
Choose: Cascade: cheap BM25 retrieval of ~1,000 candidates per shard, then learned ranking on the merged top few hundred.
Instead of: Run the expensive model on every match: far too slow.
Each stage spends compute only on candidates that survived the previous stage.
Index freshness
Choose: Large immutable base index rebuilt periodically plus a small real-time index merged at query time.
Instead of: Fully mutable index: fresh, but constant segment merges hurt latency.
Most queries hit stable content; news and new pages come from the small fresh tier.
11. Failure modes
One shard slow (tail latency)
Every query waits for the slowest shard.
Mitigation: Hedged requests to replicas after the p95 latency, and return partial results if a shard misses the deadline.
Spam pages climb rankings
Result quality collapses for some queries.
Mitigation: Spam classifiers at index time, link-graph signals, and demotion lists.
Bad index build shipped
Missing or wrong results globally.
Mitigation: Validate against golden queries before swap; roll out to a fraction of replicas first.
12. What interviewers probe
Answer each one yourself before opening it.
What is in a posting list and how is it compressed?Show a strong answer
Sorted doc ids with term frequencies and positions; delta-encode ids and compress with variable-byte or PForDelta, enabling fast skip-based intersection.
How do you evaluate ranking changes?Show a strong answer
Offline NDCG on human-rated query sets, then online A/B tests on click metrics, guarding against click-bait wins.
How do you serve 100K QPS?Show a strong answer
Replicate each shard; total replicas = QPS ÷ per-replica QPS; cache results for popular queries, which follow a power law.
How do phrase queries work?Show a strong answer
Positional postings: intersect docs containing all terms, then check that positions are consecutive.
Sign in to track this guide in your curriculum progress.