Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

System Design (High-Level Design)  Module 23 – Case studies: social, feeds and search

Design a simplified web search engine

Design a web search engine: crawl and index billions of pages, shard the index by document, rank with BM25 and PageRank, and merge top results.

  • Advanced
  • 25 minutes
  • Examples run with Python 3.14.8, Pyodide 314.0.7, Node.js 24.21.0 and quickjs 0.32.0
  • By MySmartCoPilot
Free intro, Pro: 4 of 25 minutes free
Free: 5,787 queries a second, and every one reaches all 100 shards. With a pass: the other 13 sections and 3 interview answers, about 21 minutes.

What you will learn

  • Estimate the query rate, crawl rate, index size and shard count of a web search engine
  • Connect crawling, indexing and ranking into one search pipeline
  • Partition an inverted index by document and answer queries by fan-out and a top-k merge
  • Combine text relevance from BM25 with a link-based signal such as PageRank
  • Plan freshness tiers, spam defences and caching for a search engine

Before you start

What you’ll learn · Before you start

What you will learn

  • Estimate the query rate, crawl rate, index size and shard count of a web search engine
  • Connect crawling, indexing and ranking into one search pipeline
  • Partition an inverted index by document and answer queries by fan-out and a top-k merge
  • Combine text relevance from BM25 with a link-based signal such as PageRank
  • Plan freshness tiers, spam defences and caching for a search engine

Before you start

On this page

A web search engine answers a query with the ten most useful pages out of billions, in a fraction of a second. It is a pipeline with an offline half and an online half. Offline, a crawler fetches pages, an indexer extracts their text and builds an inverted index, a list of the pages that contain each term, split into shards that each hold a slice of the pages, and a link job computes a quality score for every page from the links that point to it, such as PageRank. Online, a broker sends each query to every shard. Each shard scores its own pages by how well the text matches, with a formula such as BM25, combined with that link score, and returns its best few; the broker merges the lists into the top ten and adds their snippets. Popular queries are answered from a cache, and news is crawled and indexed again far more often than pages that rarely change.

5,787 queries a second, and every one reaches all 100 shards

Scope the engine before drawing it, because each answer moves a number:

  • What is searched? Public HTML pages, 5 billion of them, in many languages; no images, video or ads.
  • How fresh? News within minutes, other pages within a month.
  • What comes back? Ten results with a title, an address and a snippet, and further pages on request.
  • How fast and how much? 200 ms at the p99 (99th percentile): The latency within which 99 % of requests finish, so the slowest 1 % take longer. Tail latency is read from such high percentiles, because an average hides the slowest requests. More in “Latency, throughput and percentiles” in the search service, for 500 million queries a day.

The functional requirements: crawl and crawl again, index text and links, answer keyword queries with ranked results and snippets, and remove pages on request. The non-functional ones: that latency, high availability, an index that grows by adding machines, and rankings that spam cannot buy.

Quantity Assumption and result
Queries 500 million a day: 5,787 a second, about 17,400 at a 3 × peak
Pages fetched 5 billion pages, each once a month: 1,929 a second
Text after extraction 5 billion pages × 10 KB: 50 TB
Inverted index about a quarter of the text: 12.5 TB
Shards 50 million pages each: 100, and every query reaches all of them
Link graph 5 billion pages × 40 links × 8 bytes: 1.6 TB

Two rows decide the architecture. The index is far too big for one machine, so it is split, and because every query reaches all 100 shards, the slowest shard sets the latency. And 1,929 fetches a second is a system of its own, the web crawler, so this design starts where the pages are already stored.

Search engine: offline jobs crawl, index and rank pages into shards; a broker answers from a cache or merges every shard's top results.CrawlerPage storeIndexerparse, invertLink jobPageRankIndex shardsslices of pages,with replicasPeoplesearchingQuery brokermerge top kResult cacheofflinesegmentsstatic ranks1. query2. cached?3. top k fromeach shard

Offline jobs build the shards; the broker merges every shard's top results

Text description of the diagram

Offline, the crawler fills the page store. An indexer parses the pages and inverts them into segments of the index shards, and a link job computes a static rank, PageRank, for every page from the link graph. Each shard holds a slice of the pages and has replicas.

Online, a person sends a query (1) to the query broker, which first checks the result cache of popular queries (2). On a miss it sends the query to every shard, and each shard returns its top k results with their scores (3); the broker merges them into the final ten and adds a title and a snippet to each.

Follow one query along the online half of the diagram:

Interactive · message by message One query through the online half Step through one query, from the cache that misses to the ten results, and see why every shard takes part.
One query through the online half, step 7 of 7. Messages so far: App to Broker: monsoon flood; Broker to 100 shards: the query, to all 100; 100 shards to Broker: each shard's best few; Broker to App: ten results.AppBrokertop ten, with snippets100 shardsBM25 + link scoremonsoon floodthe query, to all 100each shard's best fewten resultswaits for the slowest of 100 shards

The drawing shows the whole scene; its 7 steps are listed below.

Every step as text
  1. A query arrives. Someone searches for monsoon flood. The query goes to the broker, the service that answers it for the whole index.
  2. Not in the cache. Popular queries are answered from a cache of results. This one is not there, so the broker has to ask the index.
  3. Every shard is asked. The index is split into 100 shards, each holding a slice of the pages. Any of them may hold a good match, so the query goes to all of them.
  4. Each shard scores its slice. Each shard scores its own pages by how well the text matches, with a formula such as BM25, combined with each page's link score.
  5. The best few come back. Each shard returns only its best few pages with their scores, not everything that matched.
  6. Merged into ten. The broker merges the lists into the top ten and adds a title, an address and a snippet to each.
  7. The answer. Ten results go back. The broker could only answer once the last shard had replied, so the slowest of the 100 set the latency.

Checkpoint

Check the estimate and the two halves

  1. Question 1 of 2 Each page leaves about 10 KB of text after extraction, and the inverted index takes about a quarter of the text. How many terabytes is the index for 5 billion pages?

    Type a number.

    A hint

    The text first, then a quarter of it.

    Show the answer: Question 1 of 2

    Answer: 12.5 TB (anything from 11.25 to 13.75 counts)

    5,000,000,000 × 10 KB = 50 TB of text, and a quarter of that is 12.5 TB of index, before replicas. Split into 100 shards, each holds about 125 GB.

  2. Question 2 of 2 Which of these happen offline, before any query arrives?

    Choose every answer that is right.

    Show the answer: Question 2 of 2

    Answer:

    • Crawling and storing pages
    • Building inverted index segments from the stored pages
    • Computing PageRank from the link graph

    Crawling, indexing and link analysis prepare everything in advance, so a query only reads it. Merging the shards' answers and checking the cache happen for each query, on the path that has a latency budget.

The deep dives this design must answer:

  • Index layout: an index split by page or by term, and why engines usually split by page.
  • Ranking: BM25 scores how well a page’s text matches; PageRank scores the page itself from its links.
  • Merging: each shard returns its best results, and the broker merges them into the final ten.
  • Slow shards: with 100 shards in every query, one slow shard can slow most queries.
  • Caching: many queries repeat, so a cache of results answers a large share of them.
  • Freshness: news is crawled and indexed in minutes, stable pages once a month.
  • Spam: link farms and stuffed keywords must not buy a high rank.

An answer is judged on the requirements and estimates, the pipeline, the index layout, ranking, serving latency, freshness and spam.

Two calls reach every shard: one for scores, one for snippets Pro: opens with a Pro, Premium or Ultimate pass

In this part:

  • Split the index by page, and every shard is a small search engine
  • BM25 scores the text, and PageRank scores the page from its links
    • PageRank by power iteration, with dead ends and a link farm, run
    • Combining the signals
  • How does the broker merge 100 answers into an exact top ten?
    • Repeated queries cost nothing
  • With 100 shards in every query, a slow shard is the normal case
    • Hedging after 10 ms
    • Sizing the replicas, with numbers
  • News gets a tier of its own, and spam is fought at crawl and rank time
  • What breaks first at 10 and 100 times the load, and in a failure?
  • Practise: one round of PageRank, then the whole design
    • Write one round of PageRank, with pages that link nowhere
  • Four simpler designs, and why each fails
  • What each level is expected to cover
  • A sample answer and a 45-minute self-mock
  • Interview questions
    • What does PageRank measure, and why is it computed offline rather than for each query?
    • Your index of 5 billion pages has to be split over 100 machines. Do you split it by page or by term? Defend the choice with what happens to a two-word query, a hot term and a lost machine.
    • Every query fans out to 100 index shards. Each shard answers within 20 ms at its own 99th percentile, yet queries take 400 ms at the 99th percentile. Explain the gap and bring it under 100 ms.
    • A site with 10,000 new pages that all link to one store page now ranks first for a popular query. How do you stop link farms from buying rank without punishing genuine sites whose pages link to each other?
  • Key takeaways

3 diagrams · 3 runnable examples (JavaScript, Python) · 1 exercise with sample tests · 3 interview answers · about 49 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Read a complete sample first: Design a news feed

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.