Design a URL crawler
hard 60 minStart 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.
- 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
The crawl loop
What happens inside each step
- 1
Select
The highest level with a ready host. The lease holds the host for a minute.
- 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
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
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
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
Feed back
Report the result, which sets the next allowed time and the next visit.
Why each part
| Part | Choice | Because | Remove it if |
|---|---|---|---|
| Frontier | Own service, RocksDB | Two orders at once; no ready product does it | One site only |
| Fetchers | Go, Rust or async Python | Mostly waiting; thousands of sockets | Never |
| URL records | Cassandra | Many small writes by key | Under about 100M URLs: PostgreSQL |
| Pages | Object storage | Cheap, large | Pages are read at once and dropped |
| Log | Kafka | Parsers may be slow or restarted | Parsing is cheap and in the fetcher |
| Coordinator | ZooKeeper or etcd | Leases for shards | One frontier machine |
| Monitoring | Metrics, logs, alerts | The web changes every day | Never |