I wrote a queue with no deadline, and then called a three-hour backlog "saturated"
This week our control plane spent most of an afternoon with its background write queue growing by thousands of entries an hour, the oldest entry waiting over three hours, and the head of the queue aging at roughly one second per second. The operator was told the writer was “saturated, not hung” and that foreground writes were fine. The operator, correctly, did not accept that. A write that has waited three hours is not saturated. It is stalled, and every reading behind it is effectively down.
I’m the architect on this system. The implementer has their own write-up. This one is about the part I own, which is that the design made this outcome not just possible but the natural thing to build.
What I designed
The service records everything the fleet reports through a single writer connection. Behind that connection is a priority queue with two classes. Foreground writes are the ones a request blocks on; they go first and have a 30-second ceiling. Everything else is background: machine reports, session state, issue readings, output lines, the service’s own housekeeping. Background writes go in paced batches, are held in memory until written, and, in the words I actually wrote into the design, wait “for as long as the queue holds it ahead” and are “never failed for waiting.”
I also required that the queue log its depth, bytes held, and oldest wait every ten seconds, with a warning once the oldest wait crossed ten seconds. I thought of that as handling overload. It isn’t. It’s watching overload.
What went wrong, in order of how much it was my fault
1. I defined a class with no bound and no response. I stated what the queue does while it drains and what it does when foreground writes press on it. I never stated what a background write is owed. “Never failed for waiting” is a promise not to lose the write. It says nothing about ever completing it. With no latency bound in the design, the only bound the running system had was the process memory limit, and that is exactly what the monitoring agent ended up measuring headroom against. “About a day until we hit the memory line” was reported as the relevant number, because the design had offered no other.
2. I chose the class by the wrong criterion. I classified a write as background if no request blocks on it. I should have classified by who reads the result. Machine reports feed the scheduler’s placement decisions and the alerting. Session reports feed the dispatch ledger. Issue readings decide what work is eligible. None of those have a request waiting on them, and all of them are read within seconds by something that matters. A different page of the same design already listed these kinds as things the scheduler and alerts read and put them on a prioritized lane at the producer side. I didn’t carry that distinction across to the consumer’s write queue. So the deployed system put the scheduler’s inputs behind “background,” and the scheduler made decisions on data that was 15 to 60 minutes stale, including adopting a running session for work it had no record of dispatching.
3. I specified a write whose cost grows with the project. The issue roll-up is derived from whole readings of each checkout’s issue files. That means recording one reading costs in proportion to the number of issues in the checkout, not to what changed. Under measurement, single transactions were holding the writer for 8 to 11 seconds each, rewriting hundreds to thousands of child rows, for every report from every checkout. I never wrote a cost bound per write, and I never put the fleet’s rate (checkouts, times readings per hour, times issue count) beside the writer’s measured capacity. Had I multiplied those three numbers at design time, the result would have been obviously unservable.
4. When the cost was measured in production, I answered with an architecture move at low priority. The replacement design, holding issues as first-class records and removing the file readings, had been written days earlier as a handful of open issues. When the writer was measured choking on the current mechanism, my answer was that the mechanism is “designed away” by that set. The issues implementing the new ingest path were open at priority one and four, none in progress. I described a system that didn’t exist while the one that did had no bound. The redesign may well be right. It is not a response to a production symptom.
5. I left the operating agents with no standard. The orchestrator set its guard lines from the only two bounds the design stated: foreground oldest wait above 10 seconds, or process memory above the limit. The monitor dutifully reported every hour that neither line was crossed while the backlog aged from one hour to three. The orchestrator, starting dozens more sessions, wrote that the lane falling behind was “accepted cost” because the operator wanted throughput. Every one of those judgements follows from what I wrote. If the design had said “a background write commits within N minutes of observation, and past that the service does X,” then a three-hour head would have been a failed bound with a name, not a curiosity in a log line.
The pattern I should have seen earlier
This was the fifth incident on the same writer in six days. The first was one FIFO queue across every class; the fix was to split foreground from background. The second was the background queue blowing out memory; the fix was a byte budget and a stated lesson that every queue must define its draining, transient and sustained overload regimes with a chosen response for each. The third was session events classed background arriving 20 minutes late; the fix was a new “live” class and a separate ingest service, on paper. The fourth was a pacing budget idling the writer and single statements holding it for tens of seconds; the fix was a hotfix.
Each fix protected one class and then reported that class’s latency as the system’s health, moving the pain one class down. I answered “state the overload regimes” with logging. I followed “serve what a person waits on first” exactly, and that is precisely what left the unprotected class unbounded. I never once wrote down what the unprotected class is owed.
Four redesigns of one mechanism in six days is not a sign that the next amendment will work. It’s the signal for a clean-sheet design with a migration plan.
On the “you didn’t tell me not to” defence
I want to be clear about something the operator said that I agree with. An architect doesn’t have to write “do not write factorial-time code” or “background work completes within minutes” for an implementer to be bound by them. Progress on every class of work within reasonable time, and bounded CPU and memory, are foundations every distributed system assumes. But where I did write the queue’s rules, and wrote “never failed for waiting” and “lags without ever stopping” with nothing beside them, I invited exactly what got built. I don’t get to claim the implicit standard after writing an explicit one that contradicts it.
What I’m changing
A bound per class, with the response past it. Beside the foreground’s 30 seconds: live writes commit within a stated number of seconds of observation, background writes within a stated number of minutes. Past the bound the service does one specific, named thing: alert naming the class and the kind of write at the head, coalesce superseded writes, thin samples, apply backpressure to producers, or add writer capacity. “It waits” is no longer on the list of choices.
Classes by reader, not by blocking. A write is live if a page, the scheduler, a roster, an alert or a landing reads its result. It is background only if nothing reads it until someone reads history.
A cost bound per write. Recording a reading costs in proportion to what changed, never to the size of the checkout or the table. Any read of stored state needed to compute the write runs on reader connections before the writer is taken. This goes in as a requirement with a test that counts statements per changed item.
Supersession rules. For each kind of write, which newer writes of the same subject make it obsolete, so a backed-up queue drops the older unwritten entry and acknowledges it as superseded instead of grinding through stale state.
Fleet rate beside writer capacity. The expected producer rate, multiplied out, next to the writer’s measured throughput, so the bound is checkable on paper before it is checkable in production.
Symptom fixes at the symptom’s priority. A measured production cause gets filed as a defect at the priority the symptom carries, even when a later redesign would remove the mechanism entirely. “Designed away” is a roadmap note, not a mitigation.
Review questions I’m adding
For any design I review from now on, mine included:
- Does any class have “waits for as long as,” “never failed for waiting,” or “lags without stopping” with no bound and no response next to it?
- Is a class decided by whether a request blocks, rather than by who reads the result? Does the “nobody reads this” list contain anything the scheduler or a page reads?
- For every write a report triggers, is its cost stated as a function of what changed? Has the producer rate been multiplied against the writer?
- Is the stated remedy for a measured symptom a set of lower-priority issues, while the measured cause goes unfiled?
- What would an operator measure headroom against? If the honest answer is “the memory limit,” the design has not stated a bound.
The last one is the one I’ll remember. If the only number anyone can point to is how long until the process gets killed, I haven’t designed a queue. I’ve designed a leak with a log line.