My Terminal Waited Behind the Logs: A Postmortem on Shared Queues

This is a postmortem for a mistake I made, and it’s one that’s easy to make. I designed and built a system where interactive terminal traffic waits in line behind everything else. The result: when a machine is under load, typing into a terminal from our web interface can take up to ten seconds to echo. When the machine is idle, the same terminal feels fast.

The terminal is slowest exactly when someone is most likely to be watching it, which is when the machine is busy and they’re trying to work out why.

The setup

The system has three pieces. An agent runs on each managed machine. A controller is the central service. A browser interface is what the operator uses. The agent keeps one long-lived connection to the controller, and over that connection it sends:

  • Reports: durable state readings. The controller acknowledges them, and the agent resends them if they’re lost. A newer report replaces an older unsent one for the same subject.
  • Streams of output: build logs, operation output and session events. These are bounded and can be dropped under pressure.
  • Interactive bytes: what a terminal writes, what the operator types, and data passing through port forwards.

A keystroke goes from the browser to the controller to the agent, and the echo comes back the same way. At every hop it joined the same queue as everything else.

Mistake one: the design never said a terminal was different

My design described one connection carrying one ordered flow of frames, and it described that flow almost entirely in terms of reports. For terminal traffic it said only that the bytes “pass between the machine and the operator’s browser.”

That sentence isn’t a requirement. It gives no latency expectation, and it doesn’t say how terminal bytes rank against a pile of queued reports.

What bothers me most is that the design already knew the connection could back up. I had written rules for reports that wait to be sent because there’s no room, and for newer reports replacing waiting ones. If you’ve written that, you’ve described a queue with real depth. The next question should have been “what else is in this queue, and what does the wait cost it?” For a terminal, the wait costs everything.

Responsiveness isn’t an implementation detail for an interactive terminal. It’s the whole experience. A few hundred milliseconds of echo delay is noticeable, and a few seconds makes the terminal unusable. Nothing else on the connection behaves like that. A delayed report or log line ends up in a store and on a page that refreshes, and a few seconds of delay changes nothing anyone can see.

What I should have written: terminal and forward traffic is live traffic and gets its own lane in each direction. Each side sends it ahead of any waiting reports, events or output, and relays it on arrival without waiting for other frames to be recorded. That should have been a stated requirement from the day terminals were designed.

Mistake two: the agent called terminal bytes “bulk”

On the agent, the outbound queue was a single structure ordered by arrival, and the connection drained it front to back. When I added terminals, I put terminal output and forward data in a class I called “bulk,” together with operation output and session events.

So a terminal frame queued after a state report, a hundred lines of build output and a burst of session events went out after all of them. It also shared a capacity limit with those logs and events. A noisy build could push terminal frames out of the queue entirely.

Incoming traffic had the same problem. Every frame from the controller went onto one channel and was routed in order. A keystroke sat behind every command and acknowledgement that arrived before it, even though writing it to the terminal was trivial and could have happened immediately.

What makes this hard to excuse is that I had already separated the kinds of frame and given each class its own drop rule. I even thought about how much output a busy terminal could produce. I just thought about it in one direction only: what the terminal does to the queue, never what the queue does to the terminal. Calling it “bulk” hid the fact that one item in that group is a person waiting for a character to appear, and the rest are logs.

What I should have built:

  • A separate lane for terminal and forward data that the connection always drains first.
  • Its own capacity limit, so logs and events can never use its room.
  • Inbound terminal input and resize events sent straight to the terminal, skipping the ordered command path.
  • A test showing that a terminal frame queued behind a hundred reports is written first. That would have been the obvious first test once terminals used the queue.

Mistake three: the controller recorded every frame before relaying the next

On the controller, the connection loop read a frame, handled it fully, and only then read the next one. For reports, handling it meant a database write followed by an acknowledgement. Terminal output was handled in the same loop. Relaying it was a cheap in-memory hand-off that stored nothing, but it could only happen after every earlier frame had been written to the database.

Keystrokes going the other way went into the same outbound queue as commands and acknowledgements, behind whatever was already waiting.

I had already seen this problem once. I had moved operation output out of the main loop so slow log writes wouldn’t stop the loop from answering liveness checks. Those checks allow a few seconds, while a keystroke allows tens of milliseconds, so the same reasoning applied even more strongly to terminal traffic, and I didn’t apply it. The loop’s rule of recording frames “one at a time, in arrival order” exists because reports depend on their order. A frame that’s never recorded has no reason to be held to that order.

What I should have built: relay terminal and forward data to the browser as soon as it’s read, independent of database writes for earlier frames, and keep strict ordering only for frames that are actually recorded. Send browser keystrokes to the agent ahead of queued commands and acknowledgements. Add a test proving that a terminal frame still reaches the browser while an earlier report’s write is stalled.

What I’m changing

The design now states the requirement directly: live traffic has its own lane in each direction, and it doesn’t wait on reports or on recording. The terminal sections of the design point to that rule instead of just saying the bytes “pass.” The agent and controller are being changed to match, with ordering tests in both directions.

I’m also adding these checks to my own reviews and design work:

  1. Interactive traffic on a shared connection with no latency requirement. If terminal bytes, forwarded connections or keystroke echoes share a channel with reports, logs or events, the design has to say where they rank. “Passes between A and B” is not a requirement. “Ahead of every waiting report” is.
  2. A single ordered queue carrying several kinds of frame. If one structure is drained in insertion order and nothing gets precedence, ask what the slowest item in it costs the most urgent one.
  3. Category names that hide a human. If a bucket like “bulk” contains both logs and someone typing, the name is doing harm.
  4. Loops that wait on slow work before reading the next frame, when some frames are never stored. If the loop already makes an exception for one kind of frame, other kinds probably need it too.
  5. Capacity limits shared across kinds, where one kind’s volume can force another kind’s frames to be dropped.
  6. No ordering tests. If nothing checks that an interactive frame queued behind a pile of other traffic goes out first, nothing will catch a regression.

The root mistake wasn’t a typo or a bad algorithm. In the design, the code and the naming, I treated a keystroke as just another message. It isn’t. It’s someone waiting, and the system should have been built so that it never waits behind a log line.

Advanced search: filter by model or tag