Sub topic 16
Scaling Out: Partitioning the Crawl Across Machines
This chapter assumes you have read the chapters on URL normalization, the seen set, the frontier, and politeness. You should be comfortable with hash sets and per-host queues. A single-machine crawler eventually hits limits in bandwidth, memory for the seen set, or CPU for parsing. The usual answer is to run several crawler workers and split the work between them.
Partition by host
You must decide which worker owns which URL. Two choices are common.
| Strategy | How it assigns a URL | Effect |
|---|---|---|
| Hash the full URL | Any worker may get any URL on a site | Balanced load, but many workers hit one host, so politeness breaks |
| Hash the host name | One worker owns all URLs of a host | Politeness and robots.txt stay local to one worker |
Partitioning by host is the standard choice. The worker that owns a host keeps that host's queue, its next-allowed-fetch time, and its cached robots.txt. No coordination is needed to enforce the delay, because only one machine ever talks to that host.
import hashlib
from urllib.parse import urlsplit
def owner(url, num_workers):
host = urlsplit(url).netloc
digest = hashlib.sha1(host.encode('utf-8')).digest()
return int.from_bytes(digest[:8], 'big') % num_workers
print(owner('https://example.com/a', 4) == owner('https://example.com/b', 4)) # TrueUse a stable hash such as SHA-1. Python's built-in hash() on strings changes between processes, so different workers would disagree about ownership.
Routing discovered links
A worker often finds links that belong to another worker. It sends them there instead of adding them to its own frontier. The receiving worker normalizes nothing new. It checks its own seen set and queues the URL if it is unseen.
This also splits the seen set. Each worker stores only the URLs of its own hosts, so memory per machine falls as workers are added. Sending links in batches over a message queue or an RPC call reduces network overhead.
Problems to plan for
- Skew. Hashing hosts evenly does not make load even. One huge site can overwhelm its worker. Politeness limits how fast that host can be crawled anyway, so the worker usually waits on delays rather than running out of capacity.
- Changing the worker count. With
hash % N, adding a worker reassigns most hosts. Consistent hashing moves only a small fraction of hosts, so most per-host state stays where it is. - Worker failure. Persist each worker's frontier and seen set with checkpoints, so another machine can take over its hosts. Until then, the dead worker's hosts are not crawled.
- Lost or duplicated messages. A link sent twice is harmless because the seen set filters it. A link lost in transit is a missed page, so use acknowledged delivery for link batches.
Exercise
Why is hashing the host name better than hashing the full URL for a distributed crawler, and what goes wrong if you use Python's built-in hash() to pick the worker?
Show answer
Hashing the host sends all URLs of one host to one worker, so that worker alone enforces the per-host delay and holds the robots.txt rules and queue. Hashing the full URL spreads a host across workers, which can overload it. Python's built-in hash() for strings is randomized per process, so different workers compute different owners for the same host. Use a stable hash such as SHA-1 instead.