Skip to content
All posts
4 min read

backpressure doesn't belong in a browser tab

I put a FIFO queue in the admin panel to stop generations timing out, then deleted it fifteen days later. The queue was covering for a transport problem, and a browser tab is the wrong place to hold capacity for a service four other things can call.

EngineeringSystemsAI

On June 16 I shipped a FIFO queue into a React hook. The thumbnail bot's panel was throwing 500s and 520s whenever I fired a batch of generations, so I capped in-flight runs at three in the browser and held the overflow as cancellable "Queued" cards. It worked on the first try. Fire ten, three run, seven wait, no errors.

Fifteen days later I deleted all of it.

The reasoning was not stupid, which is why it lasted as long as it did. The bot runs paid image work through a concurrency gate: a heavy lane capped at GEN_CONCURRENCY (default 3) for generation and editing, a light lane at 6 for scrapes and imports, everything else FIFO behind them. A generation takes roughly 70 seconds. At the time the panel called the bot synchronously and held the HTTP request open for the whole run, so firing ten meant seven requests sat open doing nothing but waiting for a slot, and Cloudflare cut them at about 100 seconds. Each of those came back as a 520.

So: don't fire what the bot can't run. Put the limit where the clicking happens. That is the shape of a lot of reasonable advice about backpressure, and it was the wrong lesson to take from this failure.

The failure was not too much concurrency. It was holding a 70 second run inside a request that had to stay open the whole time. I know it was not concurrency because the code I deleted to make room for the client queue said so out loud. The comment above the old bulk path read: the bot's queue caps real parallelism at GEN_CONCURRENCY and FIFO-queues the rest, so serializing here only hid that parallelism. The server queue already existed. It was already right. I built a second one in front of it because the second one was the only one the browser could see.

three things wrong with a queue in a tab

It duplicated a number it did not own. The constant in the hook was literally const GEN_CONCURRENCY = 3, a copy of an environment variable on a machine it cannot read. The bot's cap is meant to move: the queue module exports a kick() whose only job is draining the newly free slots right after the cap is raised. Raise it and the panel keeps sending three, forever, with no way to find out it is wrong.

The tab is not the only entrance. Every job carries a source, and the values are discord, site, and local, because generations get fired from a Discord command as often as from the panel. Open the panel on a phone while the desktop is mid-batch and now there are two client queues, each certain it is the only one, each politely allowing three. A cap that only one of several callers enforces limits that caller, and nothing else.

It died with the tab. In-flight cards were local state with no server id attached, so a reload dropped every tile: queued runs were gone, running runs became invisible, and the images turned up later with no card to land in.

the fix was a status code

On July 1 the panel moved to the bot's async contract. A generation request enqueues and returns 202 with the queue id, immediately. That id is also the job id and the finished generation's id, so the panel follows the run over the shared SSE stream (queued, then running) and the completed record merges into the feed under the same id. Nothing is held open, so nothing has to be rationed to keep requests short. The client queue came out in the same commit.

Three weeks later I finished the inversion. The panel now adopts running and queued jobs from the bot's live queue instead of only showing runs it started: reload and the tiles grow back with real elapsed clocks, a run fired from Discord shows up while it renders, and a failed card re-queues on the bot rather than replaying a closure the browser happened to be holding. The panel stopped owning a queue and started rendering one.

I still throttle in the browser. Batch approve and reject run five workers against a shared list, on purpose. The difference is what the limit is protecting. A rating is one cheap round trip and it is idempotent, so five at a time is about not opening two hundred connections at once, and losing the tab costs nothing worth recovering. A generation is 70 seconds of paid model time whose output outlives the page that asked for it. I was using a politeness limit to protect a metered resource, and politeness limits hold only for the callers that agree to them.

The one client-side cap I kept for generations is MAX_COUNT = 8, the most thumbnails the dialog will queue for a single game in one click. That number caps intent rather than capacity: the gap between asking for eight and fat-fingering eighty. It is also the rule I would hand back to myself in June. Limits on what a person meant to ask for belong at the entrance. Limits on what the system can survive belong where the work runs, because that is the only place that can count all of it.

Remiel Shirazi

CTO & Co-founder | AI, Roblox & Piano