Memory exhaustion in Bun.serve() when concurrent requests buffer large bodies
0 reputation · 09 Nov 2023, 09:27 UTC
0 reputation · 09 Nov 2023, 09:27 UTC
I want to run a Bun.serve() HTTP server that accepts concurrent POST requests with multi-megabyte bodies without the process exhausting memory or showing latency spikes that only appear under load. A single request in isolation behaves fine; the concern is specifically the concurrent case.
My understanding is that calling await req.text() or req.json() buffers the entire body in memory per request, so N simultaneous uploads multiply peak RSS by N. It is unclear to me whether Bun applies any internal backpressure or connection-level limits before the handler runs, or whether the only safeguard is streaming via req.body (a ReadableStream). I am also unsure how much of the latency under burst load is garbage collection pressure from these allocations versus the native network layer, and whether this differs between Linux and macOS builds. Assume a recent Bun 1.x release.
Bun.serve() provide any documented option to cap per-request body size or total in-flight buffered bytes before the fetch handler is invoked?req.body the only reliable way to avoid per-connection buffering, and does it interact with Bun's concurrency model differently than buffered reads?31775 reputation · 09 Nov 2023, 11:36 UTC
Bun.serve() provides a bodyLimit option (bytes) that caps per-request body size before the fetch handler runs; exceeding it returns a 413 response immediately.
Streaming the body via req.body (a ReadableStream) reads data incrementally, avoiding a single large allocation per request and reducing peak RSS under concurrency.
bodyLimit to Bun.serve() options if you need a hard upper bound on accepted body size.req.body in your handler and process chunks as they arrive, rather than calling req.text() or req.json() first, to keep memory growth proportional to chunk size.Bun --profile, process.memoryUsage(), or enable GC tracing (--expose-gc + global.gc()) to distinguish GC pauses from network-queuing delays during concurrent load.If you observe latency spikes under concurrent uploads, note the approximate body sizes and concurrency level. This detail determines whether bodyLimit alone suffices or whether streaming is needed to keep RSS bounded.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.