How We Run a Billion Tasks a Day (and Still Sleep at Night)
Thousands of customers, endlessly customized configurations, and zero compromise on speed: how we keep one customer’s million-item job from slowing everyone else down
The moment a million items wake up
Picture a single scraping stage that belongs to one customer. It holds a million items, and its schedule says it is time to run. Within moments, a million units of work want to enter the system.
If they all land in a shared queue at once, everyone behind them waits, including customers with small stages that refresh every few seconds. They did nothing wrong; they just shared a queue with someone much bigger.
That scenario shaped almost every architectural decision on our web scraping platform. This post walks through the designs we rejected, the system we run today, the surprises we only found in production, and what I’d do differently.
Context: what our platform actually does
Every customer on our platform defines a project for collecting data from the web. A project is made of scan stages, and the stages are chained. The results of one stage generate the tasks for the next. A stage that scans a listing page might discover hundreds of new item pages, and each one becomes a task in the following stage.
Customers decide how often each stage runs, down to the level of seconds. Across thousands of customers and very different project sizes, the result is a bursty, uneven workload. In production:
- About 11,000 tasks per second on average, and more than 26,000 per second at peak.
- Tens of thousands of pods running the workload every day.
- More than 2.5 billion HTTP requests per day.
Eleven thousand tasks per second is close to a billion tasks a day.
The two questions that defined the architecture
When we stepped back, the whole design came down to two questions:
- How do we manage the work that is waiting to be done?
- How do we move the results at the rate they are produced?
They sound like one problem. They aren’t, and treating them as one was the first mistake we had to undo.
Defining “fast”
Target websites respond at very different speeds, and we don’t control that. So we needed a latency goal that measures our work and leaves out theirs.
We set a target of 300ms for processing inside the platform. The clock starts when a run begins and stops when the Sink, the component that delivers results, finishes transmitting the result to the customer. Time spent talking to the target site is excluded.
Queue wait is also excluded and tracked separately. I’ll come back to that, because measuring it well became one of our biggest lessons.
The obvious solution doesn’t scale
Our first instinct was the obvious one: one piece of infrastructure for the whole flow, and a separate schedule for each item. It didn’t hold up.
Option 1: Kafka for everything?
Kafka is excellent at high write throughput. But our work queue needs features Kafka doesn’t offer:
- No delayed jobs. We need to say “run this item later,” and Kafka has no built-in way to do that.
- No job-level locks. We need to know that exactly one worker owns a job while it runs.
- No cheap way to prevent duplicates in the queue. If the same item is enqueued twice, we want to catch that before it costs us two scrapes.
You can build all of this on top of Kafka, but then you’re writing a job queue on top of a log.
Option 2: BullMQ for everything?
BullMQ, the Redis-backed job queue for Node.js, is great at queue management. It has delayed jobs, locks, retries, rate limits, and grouping.
The problem is volume. A billion results a day, each carrying the queue’s locks and events, turns Redis into a bottleneck. The central store that makes BullMQ convenient becomes the thing everything waits on.
Option 3: A separate schedule for every item?
The most intuitive way to model “each item refreshes every N seconds” is to give each item its own schedule.
We have more than 10 million active items. Per-item scheduling means millions of repeatable jobs and timers in Redis, all of which must be updated and cleaned up whenever a customer changes a project. The cost of a small config change grows with the number of items.
What the failures had in common
The three dead ends shared a pattern: each tried to make one mechanism do two very different jobs:
- Coordinating work: a fairly small amount of control data that needs strong guarantees (locks, delays, dedup, fairness).
- Moving results: a very large amount of data that mostly needs throughput.
That was the turning point. Once we stopped looking for one tool to do both, the design fell into place.
The solution: BullMQ for the work, Kafka for the results
BullMQ manages the work waiting to run; Kafka carries the results. Four design principles sit around that split. At a high level:
Job Scheduler (one per stage) → pages through items in the database → BullMQ queue (lean jobs, grouped per project) → scraper workers → full data goes to the customer’s Kafka topic via the Sink; status, discovered children, and a content hash go to the Orchestrator → the Orchestrator dedupes against Postgres and feeds new work into the next stage.
1. One scheduler per stage, not per item
Instead of millions of item-level timers, each stage gets a single Job Scheduler in BullMQ.
When a stage with a million items wakes up, its scheduler does not load a million rows into memory. It pages through the stage’s items in the database with a cursor, in batches of 1,000, and sends each batch to the queue with a single addBulk call.
In simplified pseudo-code, the loop is roughly:
on stage wake-up:
cursor = start
loop:
batch = fetch next 1,000 items of this stage after cursor
if batch is empty: stop
queue.addBulk(lean jobs for batch)
cursor = last item in batch
Memory stays flat regardless of stage size, each queue call carries up to 1,000 jobs instead of one, and a project change touches one scheduler rather than millions of timers.
2. Keep the job in the queue lean
All the items in a stage share the same chain of work: request, parsing, and transformation. There is no reason to copy that definition into every job.
So a job holds only the item’s ID and its state. The job configuration, meaning the stage settings that define the work, lives in Redis, stored once per stage.
A nice side effect: when a customer changes a stage’s settings, the change also applies to jobs already waiting in the queue, because no job carries a stale copy. It also keeps jobs small, which matters when the queue lives in memory.
3. Content doesn’t pass through components that don’t need it
The data we collect weighs about 40KB on average and about 670KB at p99. Pushing that through every hop would be expensive. But the Orchestrator, the component that decides what happens next, doesn’t need the content. It needs to know three things:
- Did the scan succeed?
- Which new items were discovered?
- Did anything change?
So we split the output at the final stage. The worker sends the full data directly to the customer’s topic. To the Orchestrator, it sends only:
- the status,
- the newly discovered items (children),
- and instead of the content, a hash of the content.
With a hash, the Orchestrator detects a change with a simple string comparison, without ever touching the 670KB documents. The heavy payload goes straight to the customer; the lightweight control data travels separately.
4. Only what’s new moves forward
The last principle is about not doing the same work twice.
The Orchestrator dedups by a hash of each item, leaving out any fields the customer chose to exclude, so a field they don’t care about can’t make an item look “new.” Postgres enforces it with a unique constraint on hash and stage, and inserts use ON CONFLICT DO NOTHING (table and column names below are illustrative):
INSERT INTO items (stage_id, hash, ...)
VALUES (...)
ON CONFLICT (stage_id, hash) DO NOTHING;
The database does the hard part: no read-then-write race, no application-level lock. An item that is missing from several scans in a row is deleted. Because of that, the database has stayed below 20GB, even with more than 10 million active items and constant churn.
How many pods do we need? Avoiding the KEDA traps
The split solved throughput. It didn’t answer the next question: how many workers should be running right now? With tens of thousands of pods spinning up every day, getting that wrong is expensive in both directions.
Fairness first: groups and rate limits
First, fairness. We assign each job to a group based on its project, and every group has its own rate limit. This is made possible by BullMQ Pro’s group feature, available in the paid version.
A project with a million items can build up a backlog without all of its jobs becoming available to run at once, and smaller projects keep flowing alongside it.
Trap 1: Scaling on queue length is always late
Our first approach was the standard one: scale pods on queue length with KEDA. But queue length is a lagging signal. By the time it triggers a scale-up, the queue has already swelled.
Trap 2: A rate-limited queue lies about how busy it is
Group rate limits create the opposite problem. A rate-limited queue can look overloaded even when most of its jobs can’t run yet.
Scale on raw queue length and you add pods that sit idle, waiting for jobs the rate limiter won’t release.
The fix: two signals, take the higher one
So each scraper deployment sends KEDA two signals, and KEDA picks the higher of the two.
Signal 1: Arrival rate divided by measured capacity
KEDA divides the rate at which jobs enter the queue by the capacity of a single pod.
The capacity isn’t a guess. It’s calculated from actual processing durations stored in ClickHouse, using a 15-minute average, and only when there are more than 1,000 samples, so a handful of outliers can’t skew it.
Here’s a worked example. An HTTP pod runs 5 jobs concurrently. Say a stage takes 1.4 seconds per job, and we keep 10% headroom:
capacity per pod = 0.9 × 5 ÷ 1.4 ≈ 3.2 jobs/second
queue receiving 1,500 jobs/second
pods needed ≈ 1,500 ÷ 3.2 ≈ 470 pods
What I like most is how it handles slow target sites: when they slow down, processing time rises, capacity per pod falls, and pods are added automatically. The system reacts to the actual cost of the work.
Signal 2: Queue length, but only of what’s allowed to run
The second signal is a corrected version of queue length.
First, a floor of 25 waiting jobs per pod prevents scale-down while real work remains. Second, for each group we only count what the rate limit allows in the next five minutes. A group limited to 10 jobs per five seconds is counted as at most 600 jobs, even if a million are waiting. (Five minutes is 60 five-second windows, times 10 jobs.)
The signal now reflects work that can actually run soon. Together, the two signals cover each other’s blind spots: one reacts early, the other prevents premature scale-down.
Trap 3: A fake zero is more dangerous than a missing metric
This is the trap I’d warn other teams about first. Our capacity signal depends on processing time. If the metric reports a processing time of 0, the math says each pod has infinite capacity, and the autoscaler scales the service down to the minimum, possibly at peak load. A missing metric is visible; a quiet zero looks like good news. So we made the failure behavior explicit:
- When ClickHouse isn’t available, our monitoring returns the last valid value instead of zero or an empty result.
- After that, KEDA falls back to a fixed capacity, never to 0.
Defining what your system does when its inputs are wrong matters as much as defining what it does when they’re right.
Things you only discover in production
The page that was too big to report
At one point, a single page led to the discovery of thousands of new items. All those children went into one message to the Orchestrator, and that message exceeded Kafka’s message size limit.
The produce failed, the Orchestrator never heard about the result, and the item got stuck in the “new” status, silently. The fix meant telling two kinds of failure apart:
- A transient network error resolves itself. The next cycle will try again and probably succeed.
- A message that’s too large will fail every single time. Retrying is pointless.
So only in the too-large case does the worker resend the result without the children and mark the item as invalid, so the customer sees it needs attention. The principle: a silent failure is worse than a loud one.
Freshness beats history
Production also clarified what customers really need: data freshness matters more than keeping history. That changed some defaults:
- The pipeline doesn’t keep the content. It goes straight to the customer’s topic; the pipeline exists to get the result where it needs to go, not to be an archive.
- A failed task is not sent to retry. The next cycle is already on its way, and it’ll bring fresher data than a retry of a stale request would.
“Retry on failure” is a reflex in many systems. For a workload built on frequent refresh cycles, retries mostly add load and deliver old data.
Measuring what actually happens
We measure in-platform processing time in ClickHouse, keyed by runId, so we can follow a single run through the pipeline. We’ve also written about our approach to observability across distributed microservices.
At the median:
- the scraper takes 10–60ms of in-platform processing,
- the Sink takes about 2ms.
At p99, measured separately for individual operations along the pipeline:
- adding a batch to the queue with
addBulk: 91ms, - publishing a message to Kafka: 31ms,
- processing in the Orchestrator: 18ms.
These are separate measurements of separate operations, so they shouldn’t be summed into one end-to-end figure, but they show where the time goes.
Estimating queue wait with Little’s Law
Queue wait was harder. We didn’t record per-task wait times from the beginning (more on that below), so we estimated it.
We used Little’s Law: the average time in a queue equals the queue length divided by the arrival rate. We applied it in five-minute windows.
The median of those estimates was about one second, and the p99 was about eight seconds.
To be precise: each estimate is the average wait within a window, not the wait of a single task, so the slowest individual tasks may have waited longer. And wait time grows when a stage releases thousands of items at once: the opening scenario again.
What it cost us, and what we’d do differently
No architecture is free, and this one has clear costs.
Splitting BullMQ and Kafka means:
- two managed services to operate,
- two kinds of lag to monitor (queue backlog on one side, consumer lag on the other),
- and a BullMQ Pro license, which we need for group-level rate limits.
Each tool does what it’s best at, but anyone considering this pattern should know the operational surface grows.
The one thing I’d change: measure per-task wait from day one
If I could go back, the main thing I’d do differently is measure the wait of every single task from day one.
The frustrating part is that the data was already there. BullMQ already stores when a job was created and when it started running. Recording the difference between those two timestamps would have been trivial.
Had we done that, we would know how long the slowest tasks actually waited, and what happened to wait times after each change to our autoscaling policy. Little’s Law estimates are useful, but they hide exactly the tail behavior you most want to see.
Takeaways for other engineers
If you’re building a high-volume, multi-tenant job system:
- Separate coordinating work from moving data. A job queue and a log are different tools for a reason.
- Schedule at the coarsest level that works, and keep jobs lean with shared configuration.
- Don’t move payloads through components that only need a summary. A hash often answers “did this change?”
- Let the database enforce uniqueness.
- Make fairness explicit with per-tenant groups and rate limits.
- Scale on leading signals, correct the lagging ones, and design for bad metrics. A fake zero is worse than a missing value.
- Make permanent failures loud, and know what your users value. For us, freshness beat history.
- Instrument per-task queue wait from the start. You can’t measure it in retrospect.
Closing reflection
Most of this work wasn’t about a clever algorithm. It was about drawing clear lines: between work and results, between control data and payloads, between work that’s queued and work that’s allowed to run, between a missing number and a misleading one. Each line turned a tangled problem into smaller ones we could reason about.
At scale, the most dangerous failures are the quiet ones: a zero that looks like infinite capacity, a message that fails silently, a wait time nobody recorded. Build your system so it tells you when something’s wrong. That’s what lets you sleep at night.
Common Questions About Web Scraping at Scale
Explore key engineering challenges in large-scale web scraping, from distributed task scheduling and workload isolation to autoscaling and latency optimization.
Why should task coordination and data streaming be separated in large-scale systems?
Task coordination requires capabilities such as job locking, delayed execution, deduplication, and rate limiting. Data streaming, on the other hand, requires high throughput. Using BullMQ for task coordination and Kafka for result streaming allows each component to operate efficiently without creating unnecessary infrastructure bottlenecks.
How can large-scale web scraping systems prevent workloads from interfering with each other?
Grouping jobs by project and applying independent rate limits helps maintain fairness across multi-tenant environments. This prevents large workloads from dominating shared queues and allows smaller, time-sensitive jobs to continue processing alongside them.
How can Kubernetes autoscaling be optimized for high-volume job queues?
Combining incoming job rates with measured pod capacity provides a leading scaling signal. A second signal based on queue length adjusted for group-level rate limits helps prevent premature scale-down. Using the higher of these two signals improves resource allocation and responsiveness to changing workloads.
How can processing latency be reduced in distributed data collection systems?
Keeping queued jobs lightweight, storing shared configurations separately, and avoiding unnecessary payload transfers between services reduces processing overhead. Content hashing also enables efficient change detection without requiring components to process entire data payloads.
How can queue waiting time be measured in distributed systems?
Little's Law can estimate average queue waiting time by dividing the average queue length by the arrival rate over a defined time window. However, recording individual job creation and execution timestamps provides more precise measurements and better visibility into tail latency.