A crawl queue. Push a URL in, pop a URL out. You can design one in minutes.
It took me a lot longer to notice that the queue is where most of the actual decisions in a crawler live. Everything else is plumbing.
The loop
At its simplest a crawler does four things.
- Fetch a URL.
- Parse what comes back to find links.
- Put the links in a list.
- Fetch the next one from that list.
That’s the whole job. Crawling isn’t indexing and it isn’t ranking. Those are separate stages further down the pipeline. A crawler gets bytes and finds more URLs.
“Put the links somewhere” is the queue. On a small brochure site you barely notice it.
Then you point the crawler at an ecommerce site with faceted navigation and suddenly there are hundreds of thousands of URLs, most of them the same category page with a different filter switched on… and then you realize the queue has to start having opinions.
There are really two problems in there:
- what’s allowed in, and what comes out next
- when to go back and crawl something again (because you want a current state of affairs, not what was true during the last crawl)
What gets in?
Mostly cheap checks at the door.
- Have I seen this URL already? Without that check, a single self referencing link keeps the crawler busy forever.
- Does the site want me there? robots.txt is a request, not a wall. Respecting it is a choice the crawler makes.
- How many filter combinations do I let through before I call it? Some sites will hand you millions of URLs if you let them.
Nothing clever. These are just the most basic gates.
What gets crawled next?
Then there’s the question of the order URLs are crawled. And there are two textbook answers. Breadth-first is one and depth-first is the other. I’ll only discuss breadth-first here because that’s the useful approach for SEO auditing.
Breadth first crawling: queue every link on the page and finish everything one click from the start before going two clicks out. Depth first: follow the first link, then the first link on that page, and keep going until you hit the bottom, then come back for what you skipped.
You get everything eventually either way. For an SEO audit I want breadth first, because crawl depth is something I report on, and breadth first reaches every URL by its shortest route. So “three clicks from the homepage” actually means three.
So, once I sat down to actually build a queue, something (that should have been obvious) started happening.
Watching the queue directly, some sites filled it almost instantly and others trickled in. My first guess was sites with loads of links on the URL where the crawl started. That would make sense because the crawler finds all those links immediately.
Partly right. Not the whole story.
The real outlier was an HTML sitemap. One page listing every URL on the site, linked from the footer of every page.
Breadth-first finds that link one click from wherever you start, and visiting that one page pushes the whole site into the queue. After that, almost every link the crawler finds is a URL it has already seen, so the queue drains faster too.
Which also means my depth report would say every page on that site is two clicks deep. Technically true, but worth keeping as a caveat.
Incidentally, I think that’s exactly what Matt Cutts was talking about all these years ago:
I’d never have seen that from the outside. You have to be staring at the queue.
When do you go back?
This is the second problem, and the one SEO auditing tools don’t really have. A search engine has it badly though. How to decide when to go back and recrawl a page?
It (the search engine) wants to know what’s on a URL now, not last week. And it can’t know whether a page changed without crawling it… which is the exact thing it’s trying to do as little as possible because crawling is not free.
Now, here’s some great news. We have a first-hand account telling us the likely way that Google’s crawlers make this decision. And if Google is doing it this way, this is probably a good way to do it.
I only found the best writing on this because I was deep in the queue problem anyway. It’s a 2018 post on the Unofficial Google Data Science Blog by Bill Richoux, Crawling the internet: data science within a large engineering system.1
An actual account from inside Google of how they worked out recrawl priority. Not a best practices list. Read the original if you’re happy with the maths.
If not, here’s my best attempt at summarizing the gist of it.
Fresh and recent and different things
I don’t want to undersell the account so I’ll start with a definition. An indexed page is fresh when it still matches the live page. Not when it was crawled recently.
A page crawled two years ago that hasn’t changed is perfectly fresh. A page crawled twenty minutes ago that changed nineteen minutes ago is stale.
So the goal isn’t to crawl often. It’s to be right about when a new crawl is in order. Which, you might think, is a bit of a “chicken-egg” scenario because the only way to know if there’s something new is to actually crawl the page.
The ‘machine-learning’ solution to the problem
The data science has a hammer called machine learning and therefore every problem becomes a machine-learning nail.
Here was the initial solution: model each page as changing at some rate. Work out the chance a copy is still fresh at any point after the crawl. Weight it, add it all up, apply the limits, optimize.
They proved the problem convex, which is the good news version of math in the sense that there’s one best answer, and you can find it.
The plan was a distributed solver across many machines, split so no host ever got divided between two of them. Freeze time, solve, apply, repeat. Out comes an ideal recrawl interval for every page.
Elegant. But the crawl team just pointed out that it wouldn’t work.
The pushback
Four objections, and all of these are very useful because they apply to every crawler no matter how small or large.
- It’s a black box. The engineers couldn’t see how they’d diagnose it when it went wrong.
- It answers the wrong question. The solver gives each page an interval. The crawler never asks for an interval. It asks “something just freed up, what do I fetch next?” Getting from one to the other needs another mapping step.
- It’s a lot of new machinery. The existing policy was a heuristic in a few lines of code. The solver would be tens of thousands of lines.
- It can’t keep up. If you freeze time and solve in batches, however long the solve takes is the fastest the crawler can ever react.
I like that this happened. The people who actually run the thing looked at the mathematically correct answer and said… no, wrong question.
The much-simpler-but-still-kind-of-complicated solution
So they went back to what the crawler needs. Not an interval of freshness values. Instead, all you need is to find the URL with the highest “needs another crawl” score.
And this is so obvious, it’s actually kind of funny. Here it is:
Step one, every page gets a score. The moment you crawl a page, its score is zero. You’ve just looked at it, so there’s nothing to gain from looking again.
Then the score starts going up while you’re not looking. The longer it’s been, the more likely the page has changed, and the more it’s worth going back.
Pages don’t all go up at the same speed. A homepage that matters a lot and changes every day climbs fast. An old blog post nobody links to, which hasn’t changed in years, climbs slowly.
Then there’s a limit. When a page’s score gets over the limit, it gets put in a crawl queue, crawled, and its score goes back to zero.
The limit depends on how busy things are. If a site has loads of pages waiting and can only handle so many requests, the bar on that site goes up, so only the pages that really need it get through. If there’s spare capacity, the bar drops and more pages get through.
So the crawler never needs an explicit schedule. Whenever it has room for another fetch, it scans every page’s score and takes the ones at the top. It doesn’t even need to sort them. Finding the top few is a single pass.
Why the simple solution is better
It isn’t only easier to build. It’s better.
- You can debug it. Why hasn’t this page been recrawled? Either something about the page itself (its value, how often it changes) or the competition from other pages on its host. Two questions, both answerable.
- It’s continuous. Nothing freezes. The only batch step left is sorting the list again.
- It spreads load on its own. When a new host comes online its threshold drops, so its pages get crawled sooner and the recrawls spread out instead of all landing at once and hammering the host.
- It isn’t greedy. A greedy system pours everything into the most valuable pages and the long tail never gets a look in. Here a low value page’s score keeps climbing while it waits, so it gets its turn eventually.
What I took from it
Stop asking “how often should I crawl this?” Ask “what is this worth crawling right now?”
If I ever build the recrawl I actually want (only the URLs behind one issue, so you can check fixes as you go) it’ll be a score. My inputs would be cruder than Google’s: internal links, page type, depth, whatever the previous crawl says. Same structure though. And the structure is the bit that matters.
So why is it hard?
A queue is easy. Push, pop, done.
It’s hard because the queue is where a crawler’s whole opinion of the web gets written down. What counts as the same URL. What’s worth the bandwidth. What’s probably changed while you weren’t looking. Every one of those is a judgment call, and the queue is where you stop making them by hand.
Footnotes
-
Bill Richoux, “Crawling the internet: data science within a large engineering system”, Unofficial Google Data Science Blog, 2018. ↩


