Queue concurrency and throughput
Read-only analysis. Sources: implementation in backend/src/features/jobs/,
backend/wrangler.jsonc, and current Cloudflare Queues documentation (platform limits,
consumer concurrency, batching/retries).
1. The control plane, separated
| Control | Meaning | Current value | Effect |
|---|---|---|---|
max_batch_size | messages delivered to one invocation | 1 | one invocation = one search (today) |
max_batch_timeout | how long Queues waits to fill a batch | 10 s | with batch size 1 this is effectively "deliver immediately" |
max_concurrency | simultaneous invocations for the queue | unset | scales to platform max 250 [DOC] |
max_retries | retries before DLQ | 2 | 3 total attempts (initial + 2) |
retry_delay | base delay before redelivery | unset (exponential default) | retries can land while a slow attempt is still running |
dead_letter_queue | destination after retries | jobwzrd-scrape-dlq | no consumer — messages age out at retention |
| message retention | queue-level | default 4 days | max 14 days [DOC] |
| consumer wall clock | per invocation | 15 min [DOC] | a search with 10 slow sources fits, barely |
| consumer CPU | per invocation | 30 s default, configurable to 5 min [DOC] | our CPU use is small; wall clock dominates |
Batch size 1 does not mean one active task in the system. It means one search per invocation. With concurrency unbounded, the system today can run 250 searches at once, each with 4 in-flight scraper calls = up to ~1,000 concurrent HTTP requests to two scraper containers. That is the single most dangerous number in this document.
2. What the queue cannot tell you (delivery semantics)
- At-least-once delivery is the guarantee [DOC]. A message can be delivered more than once (retry, isolate eviction, visibility timeout in pull mode).
message.ack()is the only duplicate short-circuit that means anything, and today the short-circuit is the D1 row status:claimSearchmatchesstatus='queued'only [IMPL]. That is correct for duplicates, and wrong for crashes (§4).message.retry()does not count as a failed invocation, so autoscaling ignores it — a queue full of retries will not scale out [DOC]. Our 2-attempt policy means a systemic scraper outage burns 3 × messages of work with no extra concurrency.- A thrown exception out of
queue()is a failed invocation and influences autoscaling [DOC]; our consumer catches everything (never throws), so autoscaling sees only backlog. That is acceptable but must be a deliberate choice, not an accident.
3. The three-user scenario, as built
Assumptions: each source has independent latency (0.3 s – 25 s), A has 5 sources, B has 3, C has 10.
t=0.0 POST /searches (A) ─► searches row(queued) ─► enqueue m_A
t=0.1 POST /searches (B) ─► ... ─► enqueue m_B
t=0.2 POST /searches (C) ─► ... ─► enqueue m_C
t=0.3 ┌ invocation I-A (m_A) claim A ─ pool(4) over [a1..a5]
├ invocation I-B (m_B) claim B ─ pool(4) over [b1..b3] ← concurrent, separate invocation
└ invocation I-C (m_C) claim C ─ pool(4) over [c1..c10]
each invocation: reserveCap → scrape → adapt → ingest (D1 commit per source)
D1 writes: 17 concurrent-ish batches; D1 serialises them (single-writer database)
t≈0.3 a1,b1,c1,c5 return → 4 ingests commit → rows visible in D1 (no API surface yet)
t≈4 I-B settles (3 sources done) → searches: B=complete
t≈8 a5 slow (25 s): I-A still running (a5 occupies 1 of 4 slots; others finished)
t≈26 I-A settles → A=complete; I-C settles ≈ 3 waves later → C=completeProperties, honestly:
- 1 passes only partially. A slow source delays its own search's settlement; it does not delay B or C. But inside a search, the settlement is all-or-nothing, so a user whose 5th source is slow sees nothing until t=26.
- 2 fails. There is no per-source message, so no source can be retried alone. A crash between c3 and c4 replays all ten sources on redelivery, and each replay is a real scrape against the scraper's own daily budget (the backend cap is keyed and not re-charged — that part is right).
- 4/7 fail at the interface. Rows land as each source completes, but the only read is
/searches/list, whose row is only meaningful after settlement. Requirement 7 is not satisfiable without a per-source read. - 6 fails. Any success ⇒
complete. A user cannot tell "10 of 10" from "1 of 10". - 9 is unbounded. FIFO per queue, per-user caps do not create fairness.
4. Target execution model (recommended)
producer: one message per (search_id, source_id)
message = {v, search_id, source_id, ref, hmac, enqueued_at}
consumer: one source per invocation
claimSource(search_id, source_id): pending → running (lease 10 min)
reserveCap(fetch:<search>:<source>) ← idempotent by op_key
scrape → adapt → ingest (bounded 50 rows, 100 statements)
markSource(ok|empty|failed|held, fetched, stored, skipped, http, duration)
recomputeSearch(search_id): queued|running|partial|complete|failed|held
ack
settle rule: all sources terminal → compute status from source states
any ok + any failed → partial
all empty/ok (no failures) → complete
all failed → failedWhy this shape:
- Each source retries alone; the retry budget is per source, not per search.
- A crash loses at most one source's work.
- Progress is queryable at source granularity while other sources run.
- The DLQ holds individual source failures that can be inspected/replayed safely.
- The search row settles from counts — one statement, no cross-invocation lock.
Concurrency math for the same scenario: 18 messages. At max_concurrency = 10 the
executor runs 10 sources, then 8. A slow source occupies one of ten slots, never a
search's whole wave. Per-scraper in-flight = ≤10 (services protect themselves; see §6).
Trade-off that must be accepted: the settle step runs once per source completion
(UPDATE searches ... WHERE id=?), so 10 sources ⇒ 10 small writes plus 10 ingest batches,
instead of 1 settle. That is the cost of isolation and it is worth it.
5. Should the one-active-search rule stay?
Three different policies, deliberately separated:
| Policy | Status | Effect if changed |
|---|---|---|
| One active search per user | [IMPL] SEARCH_ACTIVE | Removing it multiplies a user's fan-out by N and makes cap math ("20/day") less meaningful; also removes the accidental fairness cap |
| Many source messages in one search | missing today | required for §4; does not require removing the one-search rule |
| Many users in parallel | works | unchanged |
| Many searches per user in parallel | refused today | revisit after per-source messages + queued fair scheduling |
Recommendation: keep the rule through B4. It is currently the only thing bounding a single user's blast radius, and per-source fan-out makes a single search feel faster anyway.
6. Capacity model (protect the scrapers and the boards)
Measured inputs [TEST], from B6-lite acceptance and the live runs:
| Observation | Value |
|---|---|
MiniWzrd /scrape amazon | 250 rows in ~2.5 s (250 rows = 5 pages) |
MiniWzrd /scrape-all 28 ATS | ~54 s |
jobindie /scrape indeed | 2–10 rows in ~1–3 s |
| One search acceptance | 1 source, 250 fetched → 50 stored, sub-10 s |
| Gateway tunnel overhead | ~1 CONNECT per request, 1 s observed |
Proposed limits (labels: target, not measured):
| Limit | Initial target | Enforced by | Rationale |
|---|---|---|---|
| Queue consumer concurrency | 10 | max_concurrency | 10 invocations = 10 concurrent sources |
| In-flight per scraper service | ≤8 | consumer concurrency (single queue, one source per message) | each source hits one service |
| Per-source concurrency | 1 per source per search | one message per (search, source) | two users may still hit the same source; acceptable |
| Backend search caps | 20 / user / day | platform/limits.ts | unchanged |
| Backend source-fetch caps | 200 / user / day | platform/limits.ts | 20 × 10 exactly |
| Per-source daily budget | 50 sources/day/service (proposed) | scraper tenant budget or gateway | prevents one board saturating the tenant |
| Scraper tenant budget | 20 searches × 10 = 200/day/store | scraper registry (today in-memory) | see D6 |
| Retry budget | 2 per source message | max_retries | a third failure is a defect |
| D1 ingest per source | 50 rows = 100 statements | MAX_RESULTS_PER_SOURCE | 1/10 of the 1,000-statement invocation ceiling |
| D1 ingest per search | 300 rows = 600 statements | MAX_INGEST_PER_SEARCH | 60 % of ceiling — with per-source messages the ceiling is per message, so this becomes a sum, not a single invocation |
Not proven: whether D1 sustains 10 concurrent ingestion batches of 100 statements.
D1 serialises writes on the primary [DOC]; the question is queueing latency, not
correctness. Measure before raising concurrency: run 10 synthetic searches, record
batch duration and query latency (see METRICS-AND-ALERTS.md), and only then step 10→25.
Do not use Durable Objects yet. The only shared-state needs are (a) consumer concurrency, already a Queue setting, and (b) per-source budgets, already a cap ledger. A DO would add a second consistency domain to reason about for no current benefit. Revisit only if per-tenant fair scheduling becomes a requirement.
7. Separate queues?
| Workload | Separate queue? | Why |
|---|---|---|
| Scraper HTTP fetch (this slice) | no — keep jobwzrd-scrape | homogeneous latency (seconds), retry semantics identical, one consumer path |
AI ranking (/jobs/ranked/ai, 20 rows/run) | yes, when it becomes async (jobwzrd-ai) | different failure class (provider 429/timeout), different retry policy (never retry a billed call blindly), different concurrency (provider budget) |
| Exports (CSV/PDF) | later (jobwzrd-export) | CPU-heavy, no external effect, safe to retry; must not contend with fetch concurrency |
| Email send | never | synchronous by design; an uncertain send must never be auto-retried |
| DLQ replayer | no queue | a scheduled/manual tool, not a consumer |
Rule applied: a separate queue is justified only by a different capacity, latency, retry or failure-isolation requirement — not by code organisation.
8. Exact Wrangler settings recommended (not applied)
"queues": {
"producers": [{ "binding": "SCRAPE_QUEUE", "queue": "jobwzrd-scrape" }],
"consumers": [
{
"queue": "jobwzrd-scrape",
"max_batch_size": 1,
"max_batch_timeout": 10,
"max_concurrency": 10, // explicit; never leave unset
"max_retries": 2,
"retry_delay": 30, // seconds; do not redeliver into a still-running attempt
"dead_letter_queue": "jobwzrd-scrape-dlq"
},
{
"queue": "jobwzrd-scrape-dlq",
"max_batch_size": 10,
"max_batch_timeout": 30,
"max_retries": 3, // DLQ consumer itself may fail transiently
"max_concurrency": 1
}
]
}max_batch_size stays 1: with per-source messages, a batch of 5 would mean 5 sources ×
100 statements in one invocation, still under the 1,000-statement ceiling, but it would also
mean one slow source delays four unrelated ones inside the batch. One message per
invocation keeps the failure boundary exactly one source wide.
9. Backpressure chain (who stops whom)
D1 write latency ↑ → ingest batch slower → invocation longer → queue backlog ↑
backlog ↑ → autoscale toward max_concurrency (10) → bounded by setting
scrapers 429/503 → source message fails → retry (2) → DLQ → search partial
board WAF → scraper 502 → same → visible failureThere is no step in this chain that widens concurrency beyond max_concurrency, which is
why that value — not cap arithmetic — is the production safety valve.