Design a URL crawler
hard 60 minStart from a few addresses, download pages, find links, and repeat a billion times a month.
API and data
The contract first, then the data that serves it.
Interface
Messages more than endpoints
Readers never call a crawler. There is a small HTTP interface for operators and an internal one between parts. Inside, remote calls carry the pulling of work, and a log carries what happened. See RPC and pub/sub.
For operators
| Request | Does | Answer |
|---|---|---|
POST /v1/seeds | URLs, priority, depth | Accepted, duplicates, ids |
GET /v1/seeds, DELETE /v1/seeds/{id} | List, remove | 200, 204 |
GET /v1/hosts/{host} | Status and statistics | Found, fetched, pending, errors, robots state, delay |
PUT /v1/hosts/{host}/config | Pace, budget, priority | 200 |
POST /v1/hosts/{host}/block | Stop now | 204 |
POST /v1/hosts/{host}/recrawl | All, changed or new, with a reason | 202 and a job id |
GET /v1/stats, /v1/frontier/depth, /v1/workers, /v1/errors | Watch the system | 200 |
Between services
| Call | Carries | Note |
|---|---|---|
| Frontier.LeaseHosts | Worker, region, how many | Returns hosts with a few URLs each and a lease time |
| Frontier.Submit | New links with a source | Deduplicated by the host's owner |
| Frontier.Report | Result, status, time, next delay | Ends the lease |
| Robots.IsAllowed | URL | Allowed, delay, when the file was read |
| Dns.Resolve | Host | Addresses, time to live, from cache or not |
| Pages.Store, Pages.Get | Compressed body, headers | Key is file and offset |
| Pages.CheckFingerprint | 64 bits | Known copy or not |
protobuf
message LeaseHostsRequest { string worker_id = 1; string region = 2; int32 max_hosts = 3; }message HostLease { string host = 1; int64 lease_until_ms = 2; int32 delay_ms = 3; // at least this long between requests repeated UrlToFetch urls = 4; // best first}message UrlToFetch { string url = 1; int64 last_fetched_at = 2; // 0 = never string etag = 3; // for a conditional request int32 retry_count = 4;}Events in Kafka
| Topic | Key | Says | Read by |
|---|---|---|---|
| url-discovered | Host | URL, source, link text, hint of value | Frontier |
| page-fetched | Host | Status, size, type, duration, where the body is | Parsers |
| page-parsed | Host | Changed or not, fingerprint, links | Index team, recrawl planner |
| fetch-failed | Host | Error, attempt, next attempt | Frontier, alerts |
Data
The URL record
SQL
CREATE TABLE urls ( host text, bucket int, -- bucket = hash(url) % 64 url_hash blob, -- 128-bit url text, status text, -- pending, ok, gone, error, blocked http_status int, last_fetched_at timestamp, next_fetch_at timestamp, etag text, last_modified text, content_hash blob, simhash bigint, file text, file_offset bigint, -- where the body is visits int, changes int, retry_count int, discovered_at timestamp, discovered_from text, PRIMARY KEY ((host, bucket), url_hash));CREATE TABLE due ( -- what to fetch in a given hour hour timestamp, shard int, next_fetch_at timestamp, url_hash blob, host text, PRIMARY KEY ((hour, shard), next_fetch_at, url_hash));Where each kind of data lives
| Data | Store | Because |
|---|---|---|
| URL records | Cassandra | About 2,000 writes a second, by key, easy to split |
| Visit history | Cassandra, 90 days | Append only, read by one URL |
| Pages | Object storage, packed | Cheap, written once, read in bulk |
| Frontier | Memory and RocksDB on the owner, log in Kafka | Local decisions, survives a restart |
| Seen set | Memory on the owner | 90,000 checks a second |
| robots, DNS, pace | Memory on the owner | Belongs to the host |
| Operator search | Elasticsearch | Find by host, status or error; may be late |
Consistency by query
| Query | Level | Why |
|---|---|---|
| State of one URL | Quorum read and write | The schedule depends on it |
| History, statistics | One | Approximate is fine |
| Due list | Quorum write, duplicates allowed | A second entry only costs a check |
| Deleting after a takedown | All stores, then verify | Must really be gone |
Counters for each host are kept by its owner and written once a minute. Cassandra counters are not safe to repeat after a timeout, and a little error here would mislead the budget.