Web Crawler

Distributed crawling

Design a crawler that downloads about a billion web pages a day for a search index, without overloading any website, fetching the same page twice, or getting lost in endless URLs.

Overview

A web crawler starts from a list of known URLs (the seeds), downloads each page, finds the links in it, and adds the new ones to its list of URLs to fetch. Repeat that for weeks and you have a copy of a large part of the web, which a search engine then indexes.

The loop is simple. What makes the interview question hard is doing it at a billion pages a day while being a good guest. Websites did not ask to be crawled: a crawler must follow each site's robots.txt rules, never flood a server with requests, avoid downloading the same thing twice, and not wander forever through sites that generate endless URLs.

Questions to ask first

Before drawing anything, pin down the scope. Here are the questions worth asking, each with the answer this design assumes.

  • What is the crawl for? A general web search index, HTML pages only.
  • How many pages, and how fast? About 1 billion pages a day, and the index knows about 10 billion URLs.
  • New pages, or keep old ones fresh? Both. The crawler discovers new URLs and re-fetches known pages as they change.
  • Render JavaScript? No. It fetches the HTML as served.
  • How polite? Follow robots.txt, send at most one request at a time to a host, and pause between requests.
  • Keep the raw pages? Yes, compressed, so the indexer can be re-run without re-crawling.

The design at a glance

Seed URLs fill the URL frontier, which orders URLs by priority and hands one to a fetcher only when its host may be contacted. The fetcher checks the robots.txt and DNS caches, downloads the page from the web server, and passes it to the parser, which checks for duplicates and saves new pages to the compressed page store. Links from the page go through the link normalizer and the seen-URL filter, a Bloom filter, and only new URLs return to the frontier, along with known URLs that the URL metadata marks as due again. The Visual Explanation below draws this loop.

What you will be able to do

  • Size a crawl: fetch rate, bandwidth, storage and the memory for the seen-URL set.
  • Design a URL frontier that orders URLs by priority and still never overloads a host.
  • Apply the robots.txt rules of RFC 9309, including what to do when the file cannot be fetched.
  • Detect repeated URLs and near-duplicate pages cheaply at a scale of billions.
  • Keep pages fresh and escape crawl traps.

Key concepts

  • URL frontier
  • Politeness
  • robots.txt (RFC 9309)
  • URL normalization
  • Bloom filter
  • SimHash
  • Conditional GET
  • Crawl traps
  • Host partitioning

Functional requirements

  • Start from seed URLs and follow links to discover new pages
  • Download HTML pages and store them, compressed, for the indexer
  • Obey each site's robots.txt rules
  • Never fetch the same URL twice in one pass; skip pages whose content is a duplicate
  • Re-fetch known pages on a schedule that follows how often they change

Non-functional requirements

  • About 1 billion pages a day (a target we chose), about 11,600 a second
  • Polite: at most one request in flight per host, with a pause between requests (a policy we chose)
  • Robust to slow servers, broken HTML, redirect loops and endless URL spaces
  • Survives the loss of any machine without losing the frontier
  • Scales by adding fetcher machines

Interview questions

  • How do you make the crawler polite?
  • How does the URL frontier work?
  • How do you avoid fetching the same URL twice?
  • How do you detect near-duplicate pages at the scale of billions?
  • What do you do when robots.txt cannot be fetched?
  • How do you keep the index fresh without re-downloading everything?
  • How do you scale the crawler across many machines?
  • What are crawl traps and how do you escape them?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio