AlgoViz

Design a URL crawler

hard 60 min

Start from a few addresses, download pages, find links, and repeat a billion times a month.

Agree on the scope

Ask what changes the design, then commit to a short list.

Clarifying requirements

  1. Interviewer

    Let's design a web crawler. What would you ask?

  2. You

    First the purpose and the size:

    • I assume it feeds a search index.
    • 1 billion pages a month, with each month partly new pages and partly returns.
    • HTML first. PDFs, images and video can be added as types later. Pages that need JavaScript go to a side pool, since a browser costs far more than a plain request.
    • Freshness differs: a news front page in an hour, an archive in months.
  3. Interviewer

    What about politeness and the law?

  4. You

    Politeness is a hard rule, not a setting:

    • Obey robots.txt, before any other request to a host.
    • One request a second for each host unless it asks for less.
    • Say who we are in the User-Agent, with a page that explains how to opt out. Several IP addresses spread load; they are never used to get around a block.
    • Personal data and copyright rules differ by country. I would make hosts and types excludable, and ask lawyers instead of guessing.
  5. Interviewer

    How do you discover URLs?

  6. You

    Three sources:

    • About 10,000 good seeds, such as encyclopedias, news and government sites.
    • Links: I assume 80 on a page.
    • Sitemaps: a file holds up to 50,000 URLs, with the time of the last change. A hint, not a promise.
    • Order is neither pure breadth nor pure depth: by value, with a budget for each host.
  7. Interviewer

    Several regions?

  8. You

    Yes, later. A Japanese site is closer from Tokyo, which cuts waiting on every round trip and shows what local readers see. Each host belongs to one region, by where its server is. Pages stay there; links and records travel.

Functional requirements

  • Find URLs from seeds, links and sitemaps
  • Download HTML and store it compressed, with headers
  • Obey robots.txt and a pace for each host
  • Never fetch the same URL twice by accident; recognise copies
  • Visit pages again when they are likely to have changed
  • Operators: add seeds, set limits, block a host, force a recrawl

Out of scope for now

  • Ranking
  • Building and querying the index
  • Rendering every page in a browser
  • Pages behind a login

Non-functional requirements

QualityTargetWhy
Throughput1B pages a month: 386/s, 1,157/s at peakThe job
Politeness1 request a second and 1 connection for each hostHard rule; being blocked ends the crawl
Availability99.9% (about 8.8 hours a year)A pause loses freshness, not data
DurabilityFrontier and pages survive any one machineRebuilding takes months
Freshness90% of important pages within their intervalStale results
ConsistencyEventual; a double fetch is waste, not damageNo global locks
LatencyNot a target for one page; total rate isA 2 s fetch is fine when thousands run together

If we must degrade, slow down the fetching first. Never drop a page that was already fetched, and never drop politeness.

Why these questions

Each answer changes the design

You askIf the answer isThen
How many pages?1 million a monthOne process and one database
One site or the web?One sitePoliteness sets the time; no sharding
Keep old versions?YesAppend-only files, no overwriting
How fresh?Minutes, for a few pagesA separate fast lane, feeds and sitemaps
JavaScript?All pagesCompute, not hosts, becomes the limit
Who may we crawl?Only partnersNo traps; agreed limits