Repository navigation
Independent Agents and Role Separation
Previous Chapter: Managing Hubstorage Crawl Frontiers
- Introduction
- When central coordination stops scaling
- The principles
- The roles
- Putting it together
- Keeping the agents alive
- Simpler variants, and where graph managers still fit
- Choosing an approach
The previous chapters presented the building blocks: crawl managers, issuers, graph managers and crawl frontiers. This chapter is about how to assemble them into a workflow that can grow to many sources, many processing stages and continuous operation, while remaining understandable, resumable and easy to change.
The architecture described here is built on two ideas:
- Independent agents. Every component of the workflow is a self-sufficient agent: it is scheduled on its own, it doesn't know the status of the other components, and it can be stopped and restarted at any moment without losing work. There is no central coordinator.
- Role separation. Every component has a single, well defined role, and the components communicate only through storage: frontiers, folders of batch files, job tags.
The following chapters develop each part of this architecture in detail, and the last one of this part presents complete reference architectures built with it.
The first approach for building workflows with shub-workflow was based entirely on a central graph manager coordinating everything: it scheduled the producer spiders, the crawl managers of the consumer spiders, the post-processing scripts and the delivery, each one after the other according to the graph. Crawl managers only passed configuration to the spiders, and the spiders took care of reading and writing the crawl frontier themselves (see Who reads and who writes the frontier).
This works well for small projects, and many projects still use it. But with more complex projects its complexity escalates very quickly:
- Coordination of statuses. The central manager must know the status of every component, and decide what to do on every combination of outcomes. Every new component, and every new way of failing, adds new branches to the graph.
- Resuming. When the manager stops in the middle of a long workflow, resuming means reconstructing the status of the whole graph. When a single component fails, it is often the whole workflow instance that must be retried or repaired by hand.
- Everything advances at the pace of the slowest step. A stage cannot start until the previous one finishes, even when it could already process the part of the data that is ready. Continuous operation, where new data keeps arriving, does not fit well in a graph that has a start and an end.
- Complex spiders. When the spiders read and write the frontier, the frontier logic (slots, producer and consumer modes, which requests go to the frontier) lives in the spider code. Spiders become harder to write, and it is difficult to build code that supports all the use cases.
- Rigid parallelism. As only one process can write to a frontier slot at a time, the number of slots, and with it the number of parallel jobs, must be designed in advance.
- Changes are expensive. Changing one step means changing, testing and redeploying the graph that coordinates all of them.
The independent agents architecture replaces central coordination with a few principles. Each of them removes one of the problems above.
1. Each component is an independent agent. Spiders, crawl managers, consumers, deduplicators, deliveries: every one is scheduled on its own, either by a ScrapyCloud periodic job or by a simple keep-alive scheduler (see Keeping the agents alive). No component schedules the next stage when it finishes, and no component waits for another one. Each one simply looks, on every loop, whether there is work for it.
2. State lives in storage, not in processes. All the state of the workflow is in durable storage: the seeds in the frontier, the pending work in folders of batch files, what was already consumed in job tags (see Consume-once tags). A running component holds nothing that can't be rebuilt from storage. So any component can be stopped at any moment — cancelled, redeployed, killed by a failure — and the next instance just continues: the components are self-resuming, by construction.
3. Components communicate only through storage contracts. A stage knows nothing about the previous or the next one, except where it reads and where it writes: a frontier slot, a folder and its filename standard, a tag. That contract is all a component depends on. Behind it, each component can be developed, tested, deployed, stopped and scaled independently.
4. One role per component. Spiders only crawl: they receive their seeds as an argument (or a pointer to a batch file), and they extract items. Crawl managers only decide what to crawl and when: they read seeds and schedule spider jobs. Consumers read the items, write the new seeds back to the frontier and pass the items to the next stage. Each of the following stages performs one kind of processing. A component with a single role is simple, and when something goes wrong, the role tells where to look.
5. A single writer for every shared resource. Every frontier slot, and every output folder partition, has exactly one writer. This is a hard requirement of the HCF, and it is also what keeps the data consistent without any locking. Readers can be many: the number of spider jobs reading seeds from a slot is just a parameter of the crawl manager.
6. Processing is at-least-once, and duplicates are absorbed downstream. A component that stops in the middle of an input will process it again after restarting. Rather than trying to avoid that, the workflow is designed to tolerate it: the inputs are retired only after their output is safely written, and the deduplication stages absorb the replayed items.
7. Each role scales on its own. The number of spider jobs is a crawl manager parameter, the number of deduplicators is the number of slots of the consumer output, and so on. A bottleneck in one stage is solved in that stage, without touching the rest.
| Role | Reads | Writes | Built on | Instances |
|---|---|---|---|---|
| Spider | seeds, from its arguments or from a batch file | items | Scrapy spider | many jobs in parallel |
| Crawl manager | seeds from the frontier, or batch files from a folder | spider jobs, with their seeds as arguments | GeneratorCrawlManager |
one per spider or per group of spiders |
| Consumer | items of the finished spider jobs | new seeds into the frontier; items into hash-partitioned batch files | IssuerScriptWithSCJobInput |
one per set of frontier slots it writes |
| Filter, deduplicator, balancer, reducer | batch files of the previous stage | batch files for the next stage | IssuerScriptWithFileSystemInput |
usually one per slot |
| Downloader manager | batch files with the urls to download (for example, media files) | downloader spider jobs, with the batch file as argument | GeneratorCrawlManager |
one per group of sources |
| Delivery | items of finished jobs, or batch files | the files the customer receives | issuer | one, often run periodically |
| Keep-alive scheduler | the list of running jobs | the missing component jobs | BaseScript |
one, run periodically |
| Monitor | stats of all the jobs of a period | aggregated stats, reports, alerts | BaseMonitor |
one, run periodically |
Not every workflow needs every role. A downloader manager, for example, only appears when, after the items are discovered, something else must be fetched for each of them.
There are two kinds of seeds worth distinguishing. Branch seeds — tags, categories, authors, search keywords — lead to listings, from where the spider discovers both items and more branch seeds: they feed the crawl back. Leaf seeds — for example the ids of the items themselves — lead to the target pages and to nothing else. A workflow can use only branch seeds, with the spiders extracting the items from the listings, or both kinds, with one spider discovering leaf seeds and another one extracting the items from them.
This is the complete picture for the running example of these chapters, a site where articles are listed by tag and by author:
┌──────────────────── frontier (HCF) ◄──────────────────────┐
│ slots: mysite.com_tags, mysite.com_authors │ new seeds
▼ │
crawl manager ──► spider jobs ──► items ──► consumer ──────────────┘
(reads seeds) (seeds as args) │
│ items, partitioned by hash(id)
▼
slot 0 .. slot N-1 batch files
│
▼
deduplicator 0 .. deduplicator N-1
│
▼
delivery ──► customer bucket
keep-alive scheduler: periodically checks that every component above is running
monitor: periodically aggregates the stats of all of them
None of these components schedules another one, and none of them waits for another one. The crawl manager keeps scheduling spider jobs while there are seeds in the frontier. The consumer reads every spider job as soon as it finishes. The deduplicators process the consumer batches as soon as they appear. The delivery delivers whatever is ready each time it runs. If the consumer stops for an hour, the spider jobs simply wait to be consumed, and the crawl manager continues with the seeds already in the frontier; when the consumer comes back, it catches up. As an indication of the scale this design supports: a single consumer can serve dozens of sources, while ten deduplicators, each one with a bloom filter of around a billion items, deduplicate the whole output over years of continuous crawling.
If no component schedules another one, something must start them. There are two mechanisms, often combined:
- ScrapyCloud periodic jobs. The simplest one: each component is a periodic job. It fits well the components that process what is available and stop, like a delivery or a monitor.
- A keep-alive scheduler. A small script, itself run periodically (for example, every hour), that lists the running jobs and schedules every component that should be running but isn't: the crawl managers of the enabled spiders, the consumer, one deduplicator per slot. It holds no state and knows nothing about the work: it only keeps the agents alive. A component that stops for any reason, or that is stopped on purpose in order to deploy a new version, is back on the next run.
Accordingly, the components work in one of two modes. Continuous components (loop_mode set) run until they
reach their maximal running time, or until they are stopped, and then the scheduler starts them again: crawl
managers, consumers, deduplicators. Run to completion components (close_on_no_inputs) process the work
available, stop, and are started again by the next periodic run: deliveries, reducers, monitors.
Crawl managers started in this way must acquire the spider jobs left running by their previous instance (see Crawl managers scheduled automatically), and the issuers rely on their consume tags and on the inputs they didn't retire yet. That is all the resuming they need.
The architecture above is not all or nothing. Some simplifications are legitimate:
- In very simple projects, with no cross-job deduplication or transformation, the spiders can deliver their items directly, through Scrapy item pipelines that write to the delivery storage. There is no consumer and no processing stage at all.
- A workflow that doesn't need to feed itself with new seeds has no frontier writer: the crawl manager reads its work from a folder of input files (for example, uploaded by the customer), or from a fixed list.
And graph managers still have their place in projects built with independent agents: for the short, strictly ordered sequences of steps that run as a unit. For example, a weekly alerts process consisting of a few scripts that must run one after the other, or the per request preparation of the inputs of a crawl. The graph manager is then one more agent: started periodically or on demand, short lived, and coordinating only its few tasks, never the whole system.
| Your project | Recommended approach |
|---|---|
| A single spider, crawled now and then | a crawl manager, or just a periodic spider job |
| A crawl followed by its delivery, run as a unit | a graph manager with a crawl manager and a delivery |
| Many runs of the same spider with different inputs | a generator crawl manager |
| A crawl that must feed itself with seeds, possibly many sources | independent agents: crawl managers, a consumer that writes the frontier |
| Continuous operation, deduplication across runs, several processing stages | independent agents, with the post-crawl data chain |
| A short sequence of steps within any of the above | a graph manager for that sequence |
The next chapters develop the independent agents architecture: how crawl managers feed spiders with seeds, how consumers write the frontier, the post-crawl data chain, how the agents are kept running and controlled, and, finally, complete reference architectures.
💡 Claude assistance. shub-workflow ships a Claude Code plugin,
shub-workflow-toolkit, whoseshub-workflow-architecturesskill helps you design the architecture of your own project in an interactive session: it asks about your requirements, proposes an architecture built on the ideas of these chapters, writes a design document, and scaffolds the scripts. Install it as described in the repository README and ask Claude to help you design your project architecture.
Next Chapter: Feeding Spiders
Foundations
Building blocks
Designing workflows at scale
- Managing Hubstorage Crawl Frontiers
- Independent Agents and Role Separation
- Feeding Spiders
- Closing the Loop
- The Post-Crawl Data Chain
- Keeping It Running
- Reference Architectures
Operating workflows