Sub topic 8
Politeness, rate limits, and crawl budget
This chapter assumes you know how HTTP requests and status codes work, and that you have read the chapters on fetching pages and on the crawl frontier. You should also be able to read short Python snippets.
Why crawlers must be polite
A crawler can send requests much faster than a web server can answer them. A site on a small server can slow down or fail if a crawler requests hundreds of pages per second. A crawler that harms the sites it visits will get blocked. So crawlers limit how hard they push.
Politeness is the set of rules a crawler follows to avoid overloading a host. The common rules are:
- Limit the number of simultaneous connections to one host.
- Wait a minimum delay between requests to the same host.
- Slow down when the server shows signs of strain.
- Identify itself with a clear
User-Agentheader.
Rate limits per host
A rate limit caps how many requests a crawler sends to one host in a given time. The limit applies per host, not to the whole crawl. A crawler can fetch from thousands of hosts at once and still be gentle with each one.
This changes how the frontier works. The frontier cannot be a single queue, because one slow host would block everything behind it. Many crawlers keep one queue per host and a scheduler that records when each host may next be contacted.
Reacting to server signals
The server tells the crawler how it is coping. Three signals matter most:
| Signal | Meaning | Typical crawler response |
|---|---|---|
| `429 Too Many Requests` | The client is sending requests too fast. | Back off and honor any `Retry-After` header. |
| `503 Service Unavailable` | The server is overloaded or down. | Pause and retry later. |
| Slow responses or timeouts | The server may be struggling. | Lower the request rate for that host. |
Some crawlers also read a Crawl-delay line in robots.txt. Support for it varies. Some crawlers honor it and others ignore it, so do not rely on it to control a specific search engine.
A simple delay in code
This example enforces a minimum gap between requests to the same host.
import time
from urllib.parse import urlparse
class HostThrottle:
def __init__(self, delay_seconds=1.0):
self.delay = delay_seconds
self.last_request = {}
def wait(self, url):
host = urlparse(url).netloc
now = time.monotonic()
earliest = self.last_request.get(host, 0) + self.delay
if now < earliest:
time.sleep(earliest - now)
self.last_request[host] = time.monotonic()
throttle = HostThrottle(delay_seconds=1.0)
for url in ["https://example.com/a", "https://example.com/b"]:
throttle.wait(url)
print("fetching", url)The second request waits about one second because both URLs share a host.
Crawl budget
Crawl budget is the number of pages a search engine is willing and able to fetch from a site in a given period. It comes from two factors:
- Crawl capacity limit: how much the site can handle without slowing down. Fast, stable servers allow more.
- Crawl demand: how much the engine wants to crawl the site. Popular, frequently updated pages create more demand.
Crawl budget matters mostly for large sites with many thousands of URLs. A small site is usually crawled fully with no tuning.
What wastes budget
Budget is spent on every fetch, including useless ones. Common sources of waste:
- Many URLs that show the same content, such as tracking parameters or session IDs.
- Endless URL spaces, such as calendar pages that link to the next month forever.
- Long redirect chains.
- Pages that return errors.
- Very slow pages, which use up capacity.
Fixing these frees budget for pages you care about. Canonical URLs, clean internal links, and correct status codes all help.
Exercise
A crawler has 500 URLs queued for one host and a 2 second delay per host. Roughly how long does it take to fetch them from that host, ignoring response time? Why does adding more crawler machines not make this faster?
Show answer
About 1000 seconds, which is 500 requests times 2 seconds. More machines do not help because the delay is a limit on the host, not on the crawler. All machines together must still respect it, so they would need to share the host's schedule.