Dev.to AI πŸ€– Ai πŸ‘ 2 πŸ“– 5 min read

Fail fast or queue and pray: what to do with the eleventh LLM call

Your service allows ten LLM calls in flight, and all ten slots are busy. An eleventh request arrives. You can reject it now, or hold it until a slot frees up. async-bulkhead-llm rejects it unless you configure a queue.

Your service allows ten LLM calls in flight, and all ten slots are busy. An eleventh request arrives. You can reject it now, or hold it until a slot frees up.

async-bulkhead-llm rejects it unless you configure a queue. The README calls the queue "off by default, deliberately" and says "fail fast beats fail slow". I ran a small simulation to put numbers on that. They support "fail fast beats fail slow". They do not support "fail fast beats queueing".

I wrote earlier about queues hiding overload at an inference gateway. This is the same choice inside one Node process, where it is two options.

The two options

import { createLLMBulkhead } from 'async-bulkhead-llm';

const bulkhead = createLLMBulkhead({
  model: 'claude-sonnet-4',
  maxConcurrent: 10,
  maxQueue: 20,     // default 0: nobody waits
  timeoutMs: 2_500, // default: no limit on the wait
});

With both left out, request eleven is rejected with concurrency_limit before anything is sent to the provider.

maxQueue: 20 lets 20 requests wait for a slot, in arrival order. Request 31 gets queue_limit immediately.

timeoutMs limits the wait for a slot. It does not limit the call. A request still in line after 2.5 seconds gets timeout and leaves the line, so the provider is never called for it.

By queue and pray I mean a queue whose wait can outlast the caller. The provider does the work and nobody is left to read the answer.

The run

  • Ten slots sit in front of a fake provider whose calls all take exactly 2 seconds. At most 5 calls finish each second.
  • Requests arrive at random times, 8 a second on average, for 20 seconds.
  • Each caller gives up after 5 seconds, and the bulkhead is not told. A later answer counts as too late.
  • Nobody retries.
  • There are 20 seeded runs and 3,218 requests, with identical traffic for every setting.
maxQueue timeoutMs Answered in time Answered too late Rejected
0 (default) none 1,809 0 1,409
10 none 2,192 0 1,026
20 none 910 1,482 826
20 5,000 910 1,482 826
20 2,500 2,234 0 984
10,000 none 836 2,382 0

Counts are totals over the 20 runs. In every run, the 10-deep queue and the 2.5-second queue answered more callers in time than fail fast did, and the other three queues answered fewer.

A queue whose wait outlasts the caller answered half as many callers as no queue. Twenty places on ten slots is two rounds of 2-second calls. A request in the last place waits 4 seconds, runs for 2, and finishes a second after its caller gave up. Of the 2,392 calls the provider served, 1,482 finished too late. At 10,000 deep it was 2,382 of 3,218.

The wait limit has to leave room for the call. timeoutMs: 5_000 matches the caller's deadline and changed nothing, because no wait in that queue exceeds 4 seconds and the limit never fired. timeoutMs: 2_500 made the same queue the best row. The limit has to be at most the deadline minus the call time, which here is 5 βˆ’ 2 = 3 seconds. The 10-deep queue needed no limit, since ten places on ten slots is one 2-second round. That holds only while calls take 2 seconds.

Fail fast was not the best row, though the gap is smaller than it looks. Ten 2-second slots can start at most 2,000 calls during the arrival windows. Fail fast started 1,809, because an empty slot stays empty until the next request happens to arrive, and a queue hands it to a request already waiting. Everything a queue did above 2,000 was serving requests still in line after arrivals stopped. That is at least half of the two good queues' lead.

The two good queues paid for the rest of their lead in latency. Every fail-fast answer took 2.0 seconds. The median answer took 3.7 seconds from the 10-deep queue and 4.0 seconds from the 2.5-second one, which also made 697 callers wait 2.5 seconds to be rejected.

Why the default is no queue

The three 20-deep rows differ only in timeoutMs. That one value decides whether the queue beats fail fast or answers half as many callers, and the right value depends on two numbers the library cannot know: how long your callers wait, and how long your calls take.

Fail fast is the only row that depends on neither. That is the case for the default.

What the run does not show

  • Real call durations. Every call takes exactly 2 seconds. Real LLM calls vary widely, and I have not run varied durations.
  • Retries. Rejected callers went away. Callers that retry add to the arrival rate.
  • Callers that can wait. Without the 5-second deadline nothing is too late, and the 10,000-deep queue completes all 3,218 requests. A batch job is that case, and the library's batch profile queues.

The script

This ran on Node 22 with async-bulkhead-llm 3.19.1. The clock is virtual and the arrivals are seeded, so it prints the same table on every run.

// overload.mjs: six queue settings against the same overloaded traffic.
// Setup: npm install async-bulkhead-llm   (Node 22+)
// Run:   node overload.mjs
// Node 22 prints an ExperimentalWarning for mock timers. It is harmless here.
import { mock } from 'node:test';
import { createLLMBulkhead, LLMBulkheadRejectedError } from 'async-bulkhead-llm';

const CALL_MS = 2_000;        // every fake provider call takes exactly this long
const DEADLINE_MS = 5_000;    // a caller gives up this long after arriving
const RATE_PER_S = 8;         // average arrivals; ten 2-second slots finish 5 a second
const ARRIVE_FOR_MS = 20_000;
const SEEDS = 20;

const settings = [
  {}, // fail fast, the default
  { maxQueue: 10 },
  { maxQueue: 20 },
  { maxQueue: 20, timeoutMs: 5_000 },
  { maxQueue: 20, timeoutMs: 2_500 },
  { maxQueue: 10_000 },
];

// A virtual clock. Timers fire only when the loop at the bottom advances
// them, so the run is exact and takes about a second.
mock.timers.enable({ apis: ['setTimeout'] });
let now = 0;
const settle = () => new Promise((resolve) => setImmediate(resolve));
const fakeProvider = () => new Promise((resolve) => setTimeout(resolve, CALL_MS));

// Seeded random (Poisson) arrival times, so every setting gets the same traffic.
function arrivalTimes(seed) {
  let a = seed;
  const random = () => { // mulberry32
    a = (a + 0x6d2b79f5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
  const times = [];
  for (let t = 0; ; ) {
    t += (-Math.log(1 - random()) * 1000) / RATE_PER_S;
    if (t >= ARRIVE_FOR_MS) return times;
    times.push(Math.round(t));
  }
}

const request = { messages: [{ role: 'user', content: 'hello' }], max_tokens: 256 };
const totals = settings.map(() => ({ requests: 0, inTime: [], late: 0, rejected: {} }));
const runs = [];

for (let seed = 1; seed <= SEEDS; seed++) {
  settings.forEach((queue, i) => {
    const bulkhead = createLLMBulkhead({ model: 'claude-sonnet-4', maxConcurrent: 10, ...queue });
    const total = totals[i];
    const run = { arrivals: arrivalTimes(seed), next: 0, open: 0 };
    run.send = async () => {
      const arrivedAt = now;
      total.requests++;
      try {
        await bulkhead.run(request, fakeProvider);
        const took = now - arrivedAt;
        if (took <= DEADLINE_MS) total.inTime.push(took);
        else total.late++; // the provider did the work, the caller had left
      } catch (err) {
        if (!(err instanceof LLMBulkheadRejectedError)) throw err;
        total.rejected[err.reason] = (total.rejected[err.reason] ?? 0) + 1;
      }
      run.open--;
    };
    runs.push(run);
  });
}

while (runs.some((run) => run.next < run.arrivals.length || run.open > 0)) {
  for (const run of runs) {
    while (run.arrivals[run.next] === now) {
      run.next++;
      run.open++;
      run.send();
    }
  }
  await settle();
  now++;
  mock.timers.tick(1);
  await settle();
}

console.table(
  totals.map((total, i) => ({
    maxQueue: settings[i].maxQueue ?? 0,
    timeoutMs: settings[i].timeoutMs ?? 'none',
    requests: total.requests,
    'answered in time': total.inTime.length,
    'median of those (s)': total.inTime.sort((x, y) => x - y)[total.inTime.length >> 1] / 1000,
    'answered too late': total.late,
    rejected: JSON.stringify(total.rejected),
  })),
);
πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.