Work Queue (Producer-Consumer Pattern)

An asynchronous mechanism that buffers pending tasks in a queue to decouple task producers from task consumers, smoothing out load spikes and preventing machine overload.

General Meaning

In the Producer-Consumer pattern, the component that generates tasks (Producer) does not execute them directly; instead, it places them into an intermediate buffer (Work Queue), from which separate execution processes (Consumers or Workers) pull and process items at their own pace.

This decoupling prevents sudden bursts of work from crashing the system due to resource exhaustion, and allows throughput to be adjusted flexibly by scaling workers or separating queues by workload type.

Why It Matters

  • Smoothing out load spikes: Buffers bursts of requests and processes them sequentially so CPU and RAM are never overwhelmed.
  • Fault tolerance on the consumer side: If a worker crashes mid-task, the queued job can be retried or reassigned to another healthy worker.
  • Concurrency control across heterogeneous resources: Separates GPU/memory-intensive jobs from lightweight HTTP fetches so each queue runs at its optimal concurrency.

How the Site Factory Uses It

The Site Factory uses three main work queues: Cloudflare D1's execution_jobs table (produced by patrol and macgakari, consumed by thin agent_worker.py processes using generation numbers, lease_token, and lease_until, transitioning expired jobs to uncertain), mini-queue heavy on Mac mini (mq.py running Ollama / local LLM jobs strictly one at a time across the cluster, forbidding simultaneous dual-model loading), and mini-queue light (mq.py running CPU-bound web collection and text extraction concurrently).

Producers simply enqueue tasks and exit immediately, while KeepAlive workers claim and execute tasks whenever capacity frees up.

Caveats & Operational Rules

  • Enqueueing a job is not task completion: A successful enqueue only means work is scheduled; timeout detection and verification of a final receipt are essential in case a worker dies mid-execution.
  • Never mix heavy and light jobs in the same queue: Putting GPU-hogging LLM inference in the same queue as lightweight web scraping stalls quick fetches for hours, which is why the Site Factory separates heavy and light queues.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 11 (Stream Processing: Messaging Systems)

Verified: 2026-11-08