Design a Web Search Engine
Understanding the Problem
🌐 What is a web search engine? A web search engine like Google or Bing takes a few words from a user and returns the most useful pages on the web, ranked, in a fraction of a second. Behind the search box sit three big systems: a crawler that downloads the web, an indexer that turns pages into a structure you can search, and a serving system that answers queries against that structure. In an interview, the crawler is usually its own question; this one is about the index and the query path.
Functional Requirements
Search engines do a lot (images, maps, ads, answer boxes), so agree on the core with your interviewer up front.
Core Requirements
- Users should be able to enter a query and get the 10 most relevant web pages, each with a title, URL and a snippet showing why it matched, and page through more.
- New and updated pages should become searchable quickly: within minutes for fast-moving sites like news, within a day or so for the rest.
- Users should be able to search for exact phrases (
"how to tie a bowline").
Below the line (out of scope)
- Crawling. We consume pages from a crawler; see Design a Web Crawler.
- Ads, image and video search, maps, and answer boxes.
- Query autocomplete; see Design Typeahead Autocomplete.
- Personalized results.
Non-Functional Requirements
Core Requirements
- The system should search an index of about 10 billion pages.
- The system should handle about 100k queries per second at peak.
- The system should return results in about 200ms at the median and under 500ms at p99, measured at the server.
- The system should be highly available. Returning slightly incomplete results is better than returning an error.
- Results should be relevant: the right page near the top matters more than any other property.
Below the line (out of scope)
- Strong consistency. A page that changed a few minutes ago can show its old snippet.
- Exact result counts ("about 1,230,000 results" is an estimate everywhere).
Let's size the index, because it decides the architecture. Say an average page has about 1,000 words of text and about 400 distinct terms. An index entry per (term, page) pair is 10B × 400 = 4 trillion postings. Compressed, a posting (document ID gap plus a term count) costs one or two bytes, so about 6 TB. Storing word positions for phrase queries adds around one more byte per word, 10B × 1,000 words ≈ 10-15 TB. On top of that, we keep compressed page text for snippets, maybe 3 KB per page, which is 30 TB.
So the core index is around 20 TB, and the whole thing is around 50 TB. That's far more than one machine, so we have to shard. If each shard covers 10 million pages (about 20 GB of index, which fits in RAM), we need 1,000 shards, and as we'll see, every query has to visit all of them.
The number to call out is the fan-out. 100k queries a second, each touching 1,000 shards, is 100 million shard-level lookups a second. Every design choice after that is about making each lookup cheap, making fewer of them, and stopping the slowest shard from setting the latency for the whole query.