# Worker Queues Design notes from James (2026-08-25) on moving the backend off the naïve "do the work inside the HTTP handler" model and onto a real task queue. Captured from #dev-talk so the reasoning survives. ## The forcing function We are targeting a **2 GB device**. Low-power hardware is what pushed this from "nice idea" to "we just need to do this for sure." A box that small cannot afford to have every inbound request spawn its own unbounded pile of work. ## Where we are now Every endpoint does its work directly in the request handler. It is simple and it is correct, but it is not as good as it could be: - No backpressure. A front end that hammers us gets to allocate as much concurrent work as it can ask for. - Work logging is tangled up with HTTP handler logging, so there is no clean audit trail of what the device actually did. - Hard to test complex orderings — there is no seam to inject at. ## The design Endpoints stop doing work. They construct an object carrying the important data of the request, push it onto a work queue, and return. A pool of goroutines picks work off the queue and executes it. ### Mutable vs. immutable is the dividing line This is the rule that makes the whole thing coherent: - **Mutable operations** (delete, rename, upload, move) → go into the queue. - **Immutable operations** (list, stat, anything that returns information to the user) → answered directly and immediately. They are not queuing up work to be done, so they have no business in the queue. ### Two layers of channels The mistake last time was a **single layer of channels**. The correct shape is **two layers** — a dispatcher channel feeding a pool of per-worker channels. That is what turns it into a proper queuing system rather than one shared pipe. See the marcio.io article below for the canonical version of this pattern. ## Why it was pulled out last time The previous attempt was abandoned largely over status-code semantics: if you tried to delete a file that did not exist, the endpoint returned **202 Accepted**, because the handler did no checking at all. It could not assume the file wouldn't exist by the time the job ran — the queue could legitimately have an upload ordered before the delete. The resolution is to stop treating 202 as a wart and treat it as the contract. The queue is **ordered**, so the front end can reason about the end state. ## What the front end does with 202 Once the backend accepts a mutation, the front end applies it optimistically: - Delete accepted → remove the file from the view immediately, don't wait for confirmation. - Rename accepted → treat the file as renamed from that moment on. - A delete of the *renamed* file, issued before the rename has actually executed, is fine — the ordered queue reconciles it in the end. ## What we get - **Backpressure for free.** A low-memory system can get absolutely slammed by the front end and the backend will just happily chug through the pile at whatever rate it can manage. - **A real audit trail.** Work logging is separated from HTTP handler logging, so you can see what actually happened on your device. - **Testability.** The process is abstracted behind interfaces, so a test job can orchestrate complex combinations of requests and assert that they behave a certain way. - **Headroom to scale up** to genuinely large systems. ## The general principle From James's grad work: any time you are doing something in parallel, ask whether the work can be abstracted into a **task object**. If it can, generate a queue of tasks and keep a pool of threads ready to grab from it and push output wherever it needs to go. That is how load balancers work. That is how essentially every well-designed parallel system works. ## Sources Go concurrency fundamentals — Rob Pike: - [Go Concurrency Patterns](https://www.youtube.com/watch?v=f6kdp27TYZs) (Google I/O 2012, 51:27) - [Advanced Go Concurrency Patterns](https://www.youtube.com/watch?v=QDDwwePbDtw) (Google I/O 2013, 34:10) Worker-queue write-ups: - [Worker queues in Go](https://nesv.ca/posts/worker-queues-in-go/) — the TL;DR of the whole idea - [Handling 1 million requests per minute with Go][marcio] — the two-layer channel (dispatcher + worker pool) pattern [marcio]: http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang/ ## Status Design agreed, not yet implemented. It is a small part of the backend, and it should be written by hand rather than pulled in from a library.