← Back to System Design, page 3

system-design-0044 · Hard senior

Design a web-scale search engine: crawling, indexing and answering queries over 10 billion pages.

A crawler with a polite URL frontier fetches pages, deduplicates them and stores their text; an indexing pipeline builds a compressed inverted index split into document-partitioned shards; a query service sends each query to all shards, each returns its local top results, and an aggregator merges and re-ranks them. Recrawling 10 billion pages every 30 days means about 3,900 fetches per second, and a result cache in front of the index absorbs repeat queries.

Full answer

Requirements

Functional:

  • Crawl the web, respecting robots.txt and per-site politeness.
  • Index page text, titles and links; answer keyword queries with ranked results and snippets.
  • Refresh pages; popular or fast-changing pages more often.

Non-functional:

  • Query latency target around 200 ms at p99.
  • High availability for queries; indexing can lag by hours or days.
  • Scale to tens of billions of documents and high query volume.

Capacity estimates

Assumptions: 10B pages; average 100 KB of HTML and 10 KB of extracted text per page; compressed inverted index about 30% of text size; full recrawl every 30 days; 5B queries per day with peak factor 2; 50M documents per index shard.

  • Raw HTML: 10B x 100 KB = 1 PB (keep compressed, or only the latest copy).
  • Extracted text: 10B x 10 KB = 100 TB; index: about 30 TB.
  • Crawl rate: 10B / 30 days = about 333M pages per day = about 3,900 per second; bandwidth 3,900 x 100 KB = about 390 MB/s, about 3.1 Gbps.
  • Queries: 5B / 86,400 = about 58,000 per second; about 116,000 at peak.
  • Shards: 10B / 50M = 200 shards; each query fans out to all 200, so total shard lookups are 200 x the queries that miss the result cache. Each shard is replicated many times to carry that load.

API

GET /search?q=binary+search+tree&lang=en&cursor=... 200 {"results": [{"url", "title", "snippet", "score"}], "next_cursor": "...", "took_ms": 84}

Data model

  • Document store: doc_id -> url, title, extracted text, fetch time, content hash, quality signals.
  • Inverted index per shard: term -> postings list [(doc_id, term frequency, positions)], sorted by doc_id and compressed with delta encoding.
  • Link graph: doc_id -> outlinks, used for link-based authority scores.
  • URL frontier: queue of URLs to crawl, with priority and next allowed fetch time per host.

High-level design

mermaid
flowchart LR
  F[URL frontier] --> CR[Crawler fleet]
  CR --> DED[Dedup: URL Bloom filter + content hash]
  DED --> DS[(Document store)]
  DS --> IDX[Indexing pipeline]
  IDX --> SH[(Index shards x 200, replicated)]
  CR --> LX[Link extractor] --> F
  U[User] --> QS[Query service]
  QS --> RC[(Result cache)]
  QS --> AGG[Aggregator]
  AGG --> SH
  AGG --> RR[Re-ranker + snippets]

Crawling

The frontier holds billions of URLs, partitioned by host so one crawler owns each host and can enforce a delay between requests. Seen URLs are checked against a Bloom filter (billions of entries in a few GB) before enqueueing. Near-duplicate pages are detected with a similarity hash (for example SimHash) so mirrors are not indexed twice. Recrawl frequency is set per URL from how often it has changed and how important it is.

Indexing

Tokenise text (lowercase, language-specific stemming), build postings per batch of documents, and merge batches into shard segments. New documents land in small fresh segments merged in the background, which is how near-real-time indexing works in Lucene-based engines.

Document partitioning (each shard holds all terms for a subset of documents) is the usual choice: shards are independent and balanced. Term partitioning (each shard holds some terms for all documents) makes multi-term queries cross shards and creates hot shards for common words.

Query path

  1. Check the result cache; popular queries are answered directly.
  2. Parse and normalise the query, then send it to one replica of every shard.
  3. Each shard intersects postings lists for the terms, scores matches cheaply (BM25 plus static quality scores), and returns its top 100 or so.
  4. The aggregator merges, then a more expensive learned ranking model re-ranks the top few hundred.
  5. Snippets are generated from the document store for the final page.

Bottlenecks and trade-offs

  • Tail latency: with 200 shards, the slowest shard sets query latency. Use hedged requests (send to a second replica if the first is slow) and return partial results after a deadline.
  • Index tiers: keep a smaller, heavily replicated tier with the most important documents; query the full tier only when the first tier gives too few good results. This cuts cost a lot.
  • Freshness vs cost: frequent recrawls and segment merges cost bandwidth and CPU; prioritise by change rate and importance.
  • Two-phase ranking balances quality and latency: cheap scoring for millions of candidates, expensive models for hundreds.
  • Spam and quality: link farms and duplicate content need dedicated signals; without them, ranking quality collapses regardless of infrastructure.

By level

Fresher: Explain what an inverted index is, how a crawler finds pages, and how a query looks up terms and ranks pages. A fresher should be able to describe a postings list for one word.

Mid: Estimate crawl, storage and query load, design the frontier, index shards and query fan-out, and describe two-phase ranking. Expect the reason document partitioning is preferred.

Senior: Address tail latency at high fan-out, index tiering, freshness strategies, spam resistance, ranking evaluation, and the cost of replicas across regions. The senior answer manages tail latency across hundreds of shards.

Key points

  • Crawler with a host-partitioned, polite frontier and Bloom-filter URL dedup
  • Document-partitioned inverted index shards with compressed postings
  • Scatter-gather query: every shard returns local top-k, aggregator merges and re-ranks
  • 10B pages every 30 days is about 3,900 fetches per second
  • Tail latency, index tiers and two-phase ranking manage cost and speed

Common mistakes

  • Proposing a SQL LIKE query for search
  • Term-partitioning the index without discussing hot terms
  • Ignoring robots.txt and per-host politeness
  • Forgetting that fan-out to many shards makes tail latency the bottleneck

Follow-ups

  • How would you add a news section that must index within a minute?
  • How do you generate snippets quickly?
  • How would you evaluate whether a ranking change is better?

Tips

  • Split the answer into crawl, index and serve
  • Explain why document partitioning is preferred
  • Mention the result cache and hedged requests

Evaluation rubric

  • Requirements for all three subsystems
  • Correct estimates for storage, crawl rate and queries
  • Correct inverted index and scatter-gather design
  • Discusses tail latency, freshness, ranking and cost trade-offs