AlgoViz

Design a URL crawler

hard 60 min

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

See it run

Follow one page through the crawl, then look inside the frontier, the seen set, near duplicates and return visits.

Operations

Fetch one page

The whole loop: lease, check, fetch, parse, store, feed back.

Step 1 of 12

Ask for work

Frontierby hostDNSRobots rulesFetcherWebsiteParserObject storage

worker

idle

The fetcher asks its region's frontier for hosts. Why: one call brings many URLs, instead of one call for each.

Step 1: Ask for work

host = frontier.lease(worker)addr = dns.resolve(host.name)for url in host.urls:    if not robots.allowed(url):        frontier.report(url, "disallowed"); continue    res = http.get(addr, url, etag=url.etag, timeout=30)    pages.send(url, res)                 # to Kafka    frontier.report(url, res.status, rest=delay(res))    sleep(delay(res))def parse(url, res):    where = files.append(gzip(res.body))    records.save(url, where, hash(res.body))    frontier.submit(links(res.body))
Time
350 ms average
Scale
386/s
Peak (3x)
1,157/s

The worker leases a host, so during the lease nobody else can ask that site for anything.

In the real world

The same url crawler doing real jobs. Pick one and step through it.

Every page is valid, new and worthless.

Step 1 of 4

Looks normal

Frontierby hostDNSRobots rulesFetcherWebsiteParserObject storage

new pages

86,400 a day

The host gives new pages at one a second.

Step 1: Looks normal

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Each worker keeps its own pace

    critical

    Eight workers make eight requests a second to one site.

    Wrong
    sleep(1)                  # in every processfetch(queue.pop())
    Right
    lease = frontier.lease_host(worker)  # one owner, one holderfor url in lease.urls:    fetch(url); sleep(lease.delay)
  • Crawling when robots.txt fails

    critical

    A server error means the rules are unknown. The standard says to assume that everything is disallowed.

    Wrong
    if robots.status != 200:    allow_all()
    Right
    if 400 <= robots.status < 500: allow_all()elif robots.status >= 500:     disallow_all()
  • Scanning for due pages

    major

    A scan of ten billion rows never finishes in time.

    Wrong
    SELECT * FROM urlsWHERE next_fetch_at < now() ALLOW FILTERING
    Right
    SELECT * FROM dueWHERE hour = ? AND shard = ?
  • One object for each page

    major

    A billion uploads cost more than storing the bytes.

    Wrong
    s3.put(key=hash(url), body=page)
    Right
    file.append(page)          # upload at 1 GBrecord.save(file.name, file.offset)
  • Trusting redirects

    critical

    A site can redirect the crawler to an address inside your own network.

    Wrong
    http.get(url, follow_redirects=True)
    Right
    for hop in range(5):    addr = resolve(url)    if is_private(addr): raise Refused    # check robots for the new host too

Interview tips

  • Start with constraints

    • Pages a month, politeness, freshness.
    • 386 a second, 1,157 at peak.
    • Hosts are the limit, and say so early.
  • Spend time on the frontier

    • Value in front, hosts at the back.
    • One owner for each host.
    • Show how it survives a crash.
  • Know your sizes

    • Bloom filter: 18 GB at 0.1%.
    • 12.5 TB of pages a month.
    • 31,000 link checks a second.
  • Name the trade-off

    • Filter against exact set.
    • Pace against throughput.
    • Fresh copies against new pages.