guide
Scaling HTTP, workers, and time limits
Run HTTP across multiple VMs or processes, offload CPU work to module workers, drain servers cleanly, and bound runaway turns.
Scale HTTP across threads or processes
Start with a standard entry module (server.ts):
import { serve } from "http";
const server = serve({
hostname: "127.0.0.1",
port: 8080,
fetch() {
return new Response("ok");
},
});
await server.ready;
console.log(`listening with ${server.workers} workers`);
Two flags scale serve() without changing application code:
--http-workers=N(1–64) runsNVMs inside one OS process. One listener accepts TCP connections and hands them round-robin to the VMs; keep-alive requests stay on their assigned VM.--http-processes=N(1–64) spawnsNisolated OS processes behind a native acceptor that passes socket file descriptors over Unix sockets withSCM_RIGHTS.
# 4 in-process VMs
bkr --http-workers=4 --allow-net=127.0.0.1:8080 server.ts
# 4 separate OS processes
bkr --http-processes=4 --allow-net=127.0.0.1:8080 server.ts
Each worker evaluates the entry module independently with its own globals and heap. await server.ready resolves on the main server once all N workers have joined the listener.
Both flags allow one serve() call per entry module and cannot be combined with each other or with serve({ tls }).
Graceful shutdown
Server implements Symbol.asyncDispose. Calling await server.stop() (or exiting an await using block) stops accepting new connections and drains active responses before closing. Pass server.stop(true) to close immediately.
import { serve } from "http";
async function runUntil(signal: AbortSignal) {
await using server = serve({
port: 8080,
fetch: () => new Response("ok"),
});
await new Promise((resolve) => signal.addEventListener("abort", resolve));
}
Offload CPU work to a module Worker
new Worker(url, { type: "module" }) starts a native thread with its own boomkat VM, heap, and event loop. Workers inherit the parent's permissions with prompting turned off.
Create sum.ts:
onmessage = ({ data }: MessageEvent<Uint32Array>) => {
let sum = 0;
for (const value of data) sum += value;
postMessage(sum);
};
Spawn it from main.ts:
const worker = new Worker("./sum.ts", { type: "module", name: "sum" });
worker.onmessage = ({ data }) => {
console.log("Sum:", data);
worker.terminate();
};
const numbers = new Uint32Array([10, 20, 30, 40]);
worker.postMessage(numbers, [numbers.buffer]);
bkr --allow-read=. main.ts
Bound turn execution time
Pass --time-limit=ms to run a watchdog thread that interrupts any synchronous JS turn (including module evaluation and synchronous SQLite queries) exceeding the budget:
bkr --time-limit=250 --allow-net=:8080 server.ts
If an HTTP handler trips the limit, the server returns 500 Internal Server Error and keeps serving later requests. Any other runaway turn exits with status 1.