"Lags Without Ever Stopping" Is a Bug, Not a Design: How My Write Queue Stalled for Three Hours
I built a write queue for our app. Recently it held about 13,500 background writes, and the oldest had been waiting more than three hours. Our first status update called the store “saturated, not hung.” That wording was too kind. For a whole class of its work, the system had stopped making progress. For about twenty minutes the oldest write in the queue got one second older every second, which means the head of the queue wasn’t moving at all.
This post covers my part in that: what I built, why it fell short, and what I’m changing. A separate write-up covers the design side. Most of this failure was in the implementation, though, and I want to own that part directly.
The setup
All of our app’s persistent state goes through a single database writer connection. A few days before the incident I put a priority queue in front of that writer. Each write is one of two kinds:
- Foreground: a request is waiting on it.
- Background: everything else, including machine status reports, session records, and snapshots of project files coming in from many agents.
The rules were that foreground writes go first, background writes are paced while foreground work is waiting, and background writes otherwise get the writer to themselves. The design also said a background write waits as long as the queue keeps it behind other work, and is never failed for waiting. It gave no limit on how long that wait could be.
I took that literally. My module comment said, approvingly, that background work “lags without ever stopping.” I should have asked what keeps the lag bounded. Nothing did.
What I got wrong
1. I built an unbounded in-memory queue with no response to overload
The queue had no limit on how many background writes it could hold or how long one could wait. It lived in process memory, so a restart wiped it. Past 60 seconds of age it logged one warning line, and that was the only reaction. It sent no alert, merged no duplicate writes, dropped nothing, changed no pacing, and pushed no backpressure onto producers. The one limit it had was a byte budget on frames read ahead, and that bounds memory, not time.
The design didn’t ask for a bound, but that’s not an excuse. When producers I don’t control feed a consumer I do control, bounding the queue is part of doing the job, not an extra feature someone has to request. If the queue can’t keep up, that has to be a fault that reaches the alerting path, not a log line nobody reads.
2. I wrote a recorder whose cost scaled with the whole dataset, not with what changed
The write that filled the queue was the recorder for project-file snapshots. Each time an agent reported the files in one checkout, my code did all of this in one transaction on the single exclusive writer:
- read the stored snapshot back in full, bodies included, to keep the change times of unchanged entries;
- deleted the stored snapshot, cascading to all of its child rows;
- re-inserted every entry, one statement at a time;
- ran a series of derived reconciliations (cleanup, settling finished work, re-checking holds and alerts, bumping counters), each one another read and write on the same connection;
- committed.
So recording one snapshot cost as much as the whole project, roughly 800 tracked items, whether one entry had changed or none. Each of these transactions took 8 to 11 seconds, and around 30 checkouts were reporting. My code comment even justified doing the reads on the writer: since only one connection writes, nothing can slip in between the read and the replacement. That was correct as far as it went, and it had a large cost I never measured. I didn’t check this write’s cost against the fleet’s reporting rate when I wrote it, when I put the queue in front of it, or when I added batching later.
The obvious approach is to record the difference: read on the reader pool, work out what changed, and hold the writer only for the actual change.
3. I put derived work inside the recording transaction
The reconciliation steps in item 2 ran on every report from every checkout, even when the snapshot hadn’t changed anything they depend on. Every one of them held the exclusive connection, and none of them needed to.
4. My pacing cap left the writer idle during a backlog
I applied the background pacing budget (half of every second) as a hard cap, even when no foreground work was waiting. With more than 13,000 writes queued, the writer sat idle half the time. I fixed this on our development branch two days before the incident. The fix wasn’t included in either of the two hotfixes that went to production after it, and I didn’t make sure it was.
5. I tested order, not progress
My tests checked priority order, pacing, and reporting. None of them pushed writes in faster than the queue could drain and asserted that the oldest write’s age stayed under a limit, or that some defined response kicked in. I tested that the queue behaved correctly and never tested whether it kept up.
The pattern I missed
This wasn’t the first incident involving this writer. Over the previous week:
- A single shared writer queue made every write wait seconds. The fix was this priority queue.
- The background queue grew fast enough that the process was killed for using too much memory. The fix was the byte budget, which limits memory but not wait time.
- Live session events were classed as background and showed up 20+ minutes late. A separate live class was designed but not built.
- A bug report found the pacing cap idling the writer and single statements holding it for 10 to 26 seconds. The fixes were the pacing change (which never shipped to production) and batching.
Each fix made the cheap writes faster and left the expensive write as it was. Batching cheap writes did nothing for one that took 8 to 11 seconds on its own. Six days earlier we had written down “hold the exclusive connection only for the exclusive step; do reads elsewhere,” and this recorder still read on the writer. I kept treating symptoms because I hadn’t measured where the writer’s time actually went.
What it cost
- Machine reports reached the store 15 to 45 minutes after they were observed, and one was more than an hour behind.
- Many commands were answered without the state they should have reflected, and the logs filled with warnings saying so.
- A running session was picked up before its dispatch record existed, because the two writes were stuck in different places in the queue.
- Controller memory grew with the queue, to about 1 GiB, heading toward the memory limit within roughly a day.
What I should have built
- A bounded queue. Each class gets a maximum age: minutes for background, seconds for live. Past that, the queue raises a real alert that names the class and the kind of write at the head. It also applies a defined response: merge writes that a newer write on the same subject replaces, thin out samples, or hold back producers’ background lanes. A queue that’s too old is a fault.
- A recorder that writes only the difference. Read the stored state on the reader pool before taking the writer, compute what changed, and hold the writer only to apply those changes. Run derived reconciliation only when the change affects something it depends on. If a newer snapshot arrives while an older one for the same checkout is still queued, the newer one replaces it before it’s written.
- A cost estimate before shipping. For every write a report triggers, work out how many statements it runs as a function of report size, multiply by the expected reporting rate, compare that with what the writer can handle, and write the result down with the change.
- Tests that check progress. Simulate an hour of incoming writes faster than the drain rate and assert that the oldest write stays under the limit or the defined response fires. Assert that the recorder’s statement count scales with the number of changed files, not the size of the checkout.
- Fixes that reach production. When a fix for a production stall is on the development branch, it goes into the next hotfix, or the hotfix says why it doesn’t.
How I’m changing how I work
The main lesson: some requirements are too basic to appear in any spec. Nobody should have to write “don’t use a factorial-time algorithm” or “the system must make progress in reasonable time.” Those are the minimum for any reasonable implementation. If a spec says something will “wait as long as needed,” my job is to ask how long that can be, and to design for the answer.
Here’s what I’ll check before I merge, and what I’d ask any reviewer to check in my code:
- Does a comment say a queue “lags without ever stopping,” “waits for as long as,” or “is never failed for waiting”? Then what bounds it?
- Is a warning log the only reaction to a queue’s age?
- Inside a transaction on the exclusive writer, is there a read, a scan over history, or a derivation that doesn’t need exclusivity?
- Does a recorder delete and re-insert everything on every report? What does it cost when nothing changed?
- Does any test push writes in faster than they drain and assert a limit on age, rather than only checking order?
- Is there a known fix for a current production problem that hasn’t reached the production branch?
The queue did exactly what I wrote it to do. The problem was that I wrote it to tolerate never catching up. I’m sorry for the hours of stale data this caused.