AlgoViz

Design a URL crawler

hard 60 min

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

Grow the design

Start with the smallest system that works. Change it only when something breaks.

Start small

Grow the crawler, one break at a time

Version 1: 1. One process

Forced by: The task itself: seeds in, pages out.

Our systemOperatorsCrawlerone processPostgreSQLURLs and pagesWebsites
What changes
A loop takes the next unvisited URL from PostgreSQL, fetches it, stores the page and inserts its links. URL is unique in the table.
What it costs
One fetch at a time: about three pages a second.
What breaks next
Add threads, and they all hit the same host, because a page's links mostly point at its own host.

Say: I start with one process and one table. It is right for a million pages a month.

Flow

One page, start to end

Twelve messages

Of 87 links, perhaps 12 are new on a fresh site. On a well-known one, none.

The crawl loop

A frontier shard leases a host to a fetcher. The fetcher checks the DNS and robots cache, then asks the website, and puts the page in Kafka. A parser stores the page and returns links to the frontier shard.
The loop closes: links return to the frontier that owns their host.

What happens inside each step

  1. 1

    Select

    The highest level with a ready host. The lease holds the host for a minute.

  2. 2

    Check

    Address from the resolver cache, which saves 50 to 100 ms for a new host. Then robots.txt, then our own pace and budget.

  3. 3

    Fetch

    Reuse the connection, ask for gzip, send ETag or last time. Follow at most 5 redirects. 10 s to connect, 30 s in total, 10 MB at most.

  4. 4

    Process

    Parse as a stream and survive broken HTML. Find links in anchors, resolve relative ones, normalise. Read title, language and canonical link. Compute hash and SimHash.

  5. 5

    Store

    Append to the current file, upload at 1 GB or after some minutes, then write the record. Until then Kafka keeps the page.

  6. 6

    Feed back

    Report the result, which sets the next allowed time and the next visit.

Why each part

PartChoiceBecauseRemove it if
FrontierOwn service, RocksDBTwo orders at once; no ready product does itOne site only
FetchersGo, Rust or async PythonMostly waiting; thousands of socketsNever
URL recordsCassandraMany small writes by keyUnder about 100M URLs: PostgreSQL
PagesObject storageCheap, largePages are read at once and dropped
LogKafkaParsers may be slow or restartedParsing is cheap and in the fetcher
CoordinatorZooKeeper or etcdLeases for shardsOne frontier machine
MonitoringMetrics, logs, alertsThe web changes every dayNever