I Wrote "Bounds Memory" Over a Constant That Counted Frames
Last week I shipped a change to the controller’s connection handling that let it keep reading an agent’s incoming frames while that connection’s outgoing writes were still waiting. The idea was sound: don’t stall the reader on a slow writer. The design described the behaviour and said nothing about how much read-ahead was acceptable. So I picked a number. I capped the per-connection backlog at a few thousand frames, boxed each decoded frame, and wrote a doc comment saying the constant “bounds what one connection holds in memory.”
It does not. It bounds how many frames one connection holds. A frame can be a few hundred bytes or a few hundred kilobytes, depending on whether it carries a small status report or a large tool result. The constant was a count of variable-size items, and I labelled it a memory bound because that is what I wanted it to be.
What it did
With several machines connected, each allowed thousands of frames of read-ahead, the controller’s resident set climbed until it sat a hair under the pod’s memory limit. There was essentially no file cache; the heap was the read-ahead. After a restart, memory tracked the backlog’s high-water mark exactly: flat while the backlog held steady, stepping up when it grew, never coming back down because the allocator held onto what it had been given. A colleague had spent the previous day trying to explain that resident set and couldn’t, because the only “bound” in the code said it was bounding memory.
What I got wrong
Three distinct mistakes, each of which would have been caught by the next if I had been paying attention.
The design named a behaviour and no budget. When a design asks a component to hold data ahead of a slower consumer, it owes that component a budget, in the unit of the resource it is protecting, and tied to whatever limit the deployment actually enforces. I read “read ahead while writes wait” and treated the missing budget as an implementation detail I could fill in. A memory budget is not an implementation detail. It is exactly the kind of constant that belongs in the design, because it has to be reconciled against limits the implementation can’t see.
I bounded in the wrong unit. Frames were easy to count, so I counted frames. The right thing was to account each frame’s encoded length as it was read, release it as it was written, and stop reading a connection whose share was spent. And the budget should have been shared across all connections, not per connection, so that one agent replaying a large journal couldn’t claim a full share while every other connection claimed theirs too. A per-connection count multiplied by connection count is a bound that grows with load, which is to say it isn’t one.
I wrote a comment that described what I meant rather than what the code did. “Bounds memory” above a count of boxed items of unknown size is not imprecise phrasing. It is a false statement, and it actively misled the person debugging the problem. A reviewer should treat that as a defect, and I should have caught it myself: the moment I typed “memory” I should have asked what the constant actually counted.
The check I’m adopting
For every channel, buffer, queue, cache, or read-ahead that holds items of variable size, I now ask one question first: what unit is the bound in?
If the bound is a count and the items are not fixed-size, the bound does not protect the resource I care about. The follow-up is arithmetic: multiply the count by the realistic maximum item size, then by the number of such buffers that can exist at once (one per connection, per session, per tenant, whatever the multiplier is), and compare the product to the actual limit. If the product exceeds the limit, the bound is wrong and the comment above it is wrong too.
The same test applies beyond memory. A count of files whose sizes vary does not bound disk. A count of retries whose waits vary does not bound time. Anywhere a count stands in for a resource, the count is a proxy, and a proxy over variable-size items is a promise the code can’t keep.
What changes
The fix is going in as a byte-accounted, fleet-wide budget with a value that comes from the design and is checked against the pod’s limit, not from whatever round number felt comfortable in the editor. The doc comment will say what the constant counts. And the next time I find myself writing “bounds memory” over anything, I’ll stop and check whether that is true before I decide whether it is well phrased.