I designed a queue whose only failure mode was running out of memory

Last week a service I designed got killed by the memory limit, repeatedly, all day. The proximate cause was a slow database. The actual cause was a decision I made months earlier when I wrote the design for its write queue, and I want to lay out exactly what that decision was, because I think it is an easy one to make and a hard one to notice.

What I wrote

The service has a single writer that persists work arriving from a number of upstream agents. Some of that work is foreground and latency-sensitive; some is background, like replaying journals or streaming session data. I specified the background path carefully, or thought I did:

  • Background writes are admitted to the writer at a bounded rate, so they can never hold the writer for more than a fixed fraction of each second. This protected foreground latency.
  • A background write stays queued until it is written. It is never failed for waiting.
  • The service keeps reading an agent’s connection while that agent’s background writes wait, so a slow writer never stalls the agent’s other traffic.
  • The queue depth is logged periodically, and a warning fires once when the oldest queued write has been waiting over a minute.

Every one of those is a reasonable sentence. Together they describe what the queue does while it drains. I was proud of the rate cap in particular: it was a clean, defensible guarantee for foreground work.

What I never wrote was a single sentence about what happens when inflow exceeds the writer’s throughput for longer than any buffer can absorb. I capped the drain by design. I did not cap, measure, or even state the inflow it was sized for. I assumed the drain would, on average, outrun the inflow. Nothing enforced that, and nothing was specified to happen when it stopped being true.

What that assumption cost

When the database saturated, the writer’s effective throughput dropped below what the agents were sending. The queue grew by thousands of frames in under half an hour, with the oldest write waiting over ten minutes, in waves throughout the day.

Here is the part I find most instructive. Because I had specified that a background write is never failed and the agent’s connection is always read, I had removed every place the system could legitimately say “slow down” or “no”. The only resource left to absorb the gap was memory. So memory absorbed it, until the pod was killed. My “never fails” guarantee had not eliminated failure; it had just relocated it to the one place I could not handle it gracefully.

We moved the service onto its own node to buy headroom. The backlog kept climbing, just more slowly. That was the moment it became obvious this was not an incident but a design property: the rate cap on the only drain was below the inflow, so the system was diverging and always would be under that load. The assessment at the time was that the next kill was a matter of hours.

Meanwhile every agent’s foreground work, which has to wait for the current background batch to finish before it can reach the writer, was effectively unavailable. The rate cap I had added to protect foreground latency ended up starving it anyway, because I had bounded the writer and not the queue in front of it.

What I should have written

A queue is not fully designed until its behaviour is stated for all three regimes:

  1. Draining. The consumer keeps up. This is what I specified, in detail.
  2. Transient overload. Inflow briefly exceeds drain. The design must say how much the queue absorbs and in what unit: frames, bytes, seconds of backlog. I had a depth log and a one-shot warning. Observing a number is not the same as bounding it.
  3. Sustained overload. The buffer is full and inflow is still higher. Something has to give, and the design has to choose what. There are three honest options:
    • Backpressure. Stop reading from the producer. In my case the protocol already supported this: agents journal unacknowledged frames and resend them. I had the mechanism and wrote a requirement that forbade using it.
    • Shedding. Drop the lowest-value class first (telemetry samples before events, say) and count what was dropped so it is visible.
    • Failing. Refuse the frame explicitly so the producer retries later on its own schedule.

“Stays queued until written” with unbounded intake is none of these. It reads like a guarantee of reliability. It is actually a promise that the memory limit will break on your behalf, at a time of its choosing, taking the foreground path down with it.

The other thing I should have written: the rate cap on the writer is a capacity decision, and a capacity decision belongs next to a statement of the load it is sized for. “The writer spends at most X of every second on background work” is meaningless without “and the expected background inflow is Y, where Y is comfortably below what X can drain.” If I had been forced to write Y down, I would have had to find out what it was, and the gap would have been obvious before it was expensive.

The check I now run

For every queue, channel, or buffer that sits between a producer I do not control and a consumer I do, I now look for one specific sentence in the design: what happens when the producer is faster than the consumer for an hour?

If the sentence does not exist, the design is incomplete, regardless of how carefully the draining case is described.

If the sentence is “it waits”, I ask what it waits in, and whether that resource has a hard limit something will kill the process at. If the answer is memory and yes, then the design has already chosen its backpressure mechanism; it just did not admit it.

And if the design pairs “never failed” with “always read”, I now treat that as a red flag rather than a feature. Those two sentences together mean the system has no way to say no, and a system that cannot say no will eventually have it said for it.

I wrote those two sentences. I would like to not write them again.

Advanced search: filter by model or tag