Sub topic 8
Politeness, rate limits, and crawl budget
This chapter assumes you know how an HTTP fetch works and what 200, 301, 404, 429, and 503 mean. You should also remember that a crawler keeps a frontier, which is a queue of URLs waiting to be fetched, and that it reads robots.txt before fetching. You should be able to read short Python snippets.
A crawler can fetch pages much faster than most servers can serve them. Politeness is the set of rules a crawler follows so it does not harm the sites it visits. Crawl budget is the number of URLs a crawler is willing and able to fetch from one site in a given period. The two are linked, because politeness sets the upper limit on that budget.
Why politeness exists
A fast crawler that opens hundreds of connections to one small server looks the same as a denial-of-service attack. The server slows down, real visitors see errors, and the site owner blocks the crawler. Well-run crawlers therefore limit their own speed per host, not just overall.
The word host matters. A crawler can fetch from thousands of different sites at the same moment without harming any of them. The limit applies to each host separately. The frontier must therefore be organized so that consecutive fetches go to different hosts, or the crawler must wait between requests to the same one.
Politeness usually has these parts:
- A minimum delay between requests to the same host.
- A cap on simultaneous connections to that host.
- Backing off when the server shows signs of strain.
- Obeying robots.txt rules, including any crawl delay it states.
Crawl-delay in robots.txt
Some sites ask crawlers to slow down with a Crawl-delay line in robots.txt. Its value is the number of seconds to wait between requests. This directive is not part of the original robots.txt convention, and crawlers treat it differently. Some honor it. Google's crawler ignores it and adjusts its speed from how the server responds. Check each crawler's documentation before relying on it.
Python's standard parser can read the value:
from urllib.robotparser import RobotFileParser
rules = """User-agent: *
Crawl-delay: 5
Disallow: /private/
""".splitlines()
rp = RobotFileParser()
rp.parse(rules)
print(rp.crawl_delay("ExampleBot"))This prints 5. If no Crawl-delay applies to the agent, crawl_delay() returns None, so your code needs a default.
Server signals that tell a crawler to back off
Beyond robots.txt, the server's responses are the main signal. A polite crawler treats certain responses as a request to slow down:
| Signal | Meaning | Typical crawler reaction |
|---|---|---|
| 429 Too Many Requests | The server is rate limiting this client | Pause and lengthen the delay |
| 503 Service Unavailable | The server is overloaded or in maintenance | Retry later and reduce the rate |
| Other 5xx errors | The server is failing | Slow down; repeated failures lower the crawl rate |
| Slow response times | The server is near its limit | Lower the number of parallel connections |
| `Retry-After` header | The server states how long to wait | Wait at least that long |
Retry-After is either a number of seconds or an HTTP date. Parsing both forms is a common source of bugs. The code below handles only seconds.
A 404 or 410 does not signal strain. Those responses tell the crawler the URL is gone, which affects how often it returns to that URL (covered in the recrawling chapter).
A per-host throttle
The class below keeps one delay per host. It doubles the delay after a 429 or any 5xx, and shrinks it slowly after successes. This pattern is called exponential backoff with gradual recovery. The delay never drops below a base value or rises above a maximum.
import time
from urllib.parse import urlparse
class HostThrottle:
def __init__(self, base_delay=1.0, max_delay=60.0):
self.base_delay = base_delay
self.max_delay = max_delay
self.delay = {}
self.next_ok = {}
def wait(self, url):
host = urlparse(url).netloc
pause = self.next_ok.get(host, 0) - time.monotonic()
if pause > 0:
time.sleep(pause)
def record(self, url, status, retry_after=None):
host = urlparse(url).netloc
current = self.delay.get(host, self.base_delay)
if status == 429 or status >= 500:
current = min(current * 2, self.max_delay)
if retry_after is not None:
current = min(max(current, retry_after), self.max_delay)
else:
current = max(self.base_delay, current * 0.75)
self.delay[host] = current
self.next_ok[host] = time.monotonic() + current
throttle = HostThrottle(base_delay=0.2, max_delay=2.0)
url = "https://example.com/a"
for status in [200, 200, 503, 503, 200]:
throttle.wait(url)
throttle.record(url, status)
print(status, round(throttle.delay["example.com"], 2))The program prints:
200 0.2
200 0.2
503 0.4
503 0.8
200 0.6The delay doubles on each 503, then falls by 25 percent after the success. It does not snap back to the base value. A fast return would send a burst of requests to a server that has only just recovered.
What crawl budget is
Search engines describe crawl budget as the result of two forces:
- Crawl capacity limit: how much the crawler can fetch without overloading the server. This is the politeness side. Fast, error-free responses raise it. Errors and slow responses lower it.
- Crawl demand: how much the crawler wants to fetch. Popular pages, pages that change often, and pages the crawler has never seen create demand. Stale, low-value, or duplicate URLs create little.
The budget is roughly the smaller of the two. A fast server does not get more crawling if the crawler sees nothing worth fetching. A site full of valuable pages does not get crawled faster than its server can bear.
A worked budget calculation
Suppose a crawler waits 1 second between requests to your host and uses one connection. The most it can fetch is 86,400 URLs per day, which is the number of seconds in a day. With two connections the ceiling is about 172,800.
Now suppose your site has 50,000 real pages, but filters and sort options create 2,000,000 URL variations, such as /shoes?color=red&sort=price&page=7. The crawler discovers these through links, and each looks like a new URL. If it spends most of its fetches on variations, your real pages are recrawled less often, and new pages wait longer to be found.
For small sites this rarely matters. Budget becomes a real concern for large sites, roughly tens of thousands of URLs or more, and for sites that publish or change many pages daily.
What wastes crawl budget
- Faceted navigation and filter parameters that generate near-endless URL combinations.
- Session IDs or tracking parameters in URLs, which create duplicates of the same page.
- Duplicate content reachable under several URLs (see the canonical URLs chapter).
- Soft 404 pages, which return status 200 but show a "not found" message.
- Long redirect chains, because each hop costs a fetch.
- Calendar or infinite-scroll pagination that never ends.
- Slow responses, which reduce how many pages fit into the crawler's time window.
How to protect and improve your budget
- Make the server fast and stable. Fewer errors and quicker responses raise the capacity limit.
- Remove or consolidate duplicate URLs, and point duplicates to a canonical URL.
- Block low-value URL patterns in robots.txt, such as internal search results or endless filter combinations.
- Return correct status codes. Use 404 or 410 for removed pages, and avoid soft 404s.
- Keep internal links pointing to final URLs, not redirecting ones.
- List important, up-to-date URLs in a sitemap with accurate
lastmoddates.
Blocking in robots.txt saves fetches but has a cost. The crawler cannot see a page it may not fetch, so it cannot read a noindex tag or a canonical link on it. Blocking is a way to save budget, not a way to remove pages from the index.
Common mistakes
- Throttling globally instead of per host. A single global delay wastes capacity on other sites, and it does not protect any one of them.
- Ignoring
Retry-After. The server told you how long to wait, and retrying sooner can get you blocked. - Treating all errors alike. A 404 means the page is gone. A 503 means the server is struggling. They need different reactions.
- Retrying instantly in a loop. Without a growing delay, a failing server receives more traffic exactly when it can least cope.
- Assuming that more pages mean more budget. Budget follows demand and server health, not page count.
Exercise
A crawler uses one connection and waits 2 seconds between requests to your host. About how many URLs can it fetch from your site in one day at most, and what two changes on your side would most plausibly increase crawling of your important pages?
Show answer
At most 43,200 URLs per day (86,400 seconds divided by 2). Making the server faster and more reliable raises the capacity limit, and removing duplicate or low-value URLs (filters, session IDs, redirect chains) frees fetches for your important pages and raises their share of the budget.
Exercise
Your server returns 503 for several minutes during maintenance. In the HostThrottle class, what happens to the host's delay during this period, and why does the delay not return to its base value immediately after the first 200 response?
Show answer
Each 503 doubles the delay, up to max_delay. After a 200 the delay is multiplied by 0.75, not reset. The gradual recovery avoids sending a burst of requests to a server that has only just come back.
Exercise
A site owner blocks /filter/ in robots.txt to save crawl budget, but pages under it still show up in search results as bare URLs. Why can this happen, and what does it say about robots.txt compared with noindex?
Show answer
robots.txt controls fetching, not indexing. The search engine can still learn the URL from links on other pages and list it without having fetched the content. Because the crawler cannot fetch the page, it also cannot read a noindex tag there. To keep a page out of the index, allow crawling and use noindex.