#1020·pingora

h1: body read buffer lives for the whole request and is 64 KiB regardless of Content-Length

Author: mariojcrCreated Sep 25, 2026Updated Sep 25, 2026
LabelsPerformance

What happens

BodyReader (pingora-core, protocols/http/v1/body.rs) has two behaviours that cost memory:

  1. Downstream: after the request body has been fully read (each chunk is copied out by read_body_bytes), the read buffer, min(remaining, 64 KiB), stays allocated until the request ends. With long-lived responses this is 64 KiB per in-flight request that is no longer used. An LLM completion streamed for tens of seconds is one case, and so is any slow response to a request that had a body.
  2. Upstream: a body with a known Content-Length gets a BODY_BUFFER_SIZE (64 KiB) read buffer regardless of its length. init_content_length sizes the buffer to body_buf_size, and upstream Partial reads are not capped, so a 200-byte response reserves 64 KiB.

As far as we can tell, 64 KiB is there as the per-read size for large bodies. The buffer is reused across reads, so a large transfer takes fewer reads, and for short requests the lifetime does not matter. That cost only shows up when requests are long-lived.

Numbers

A proxy built on Pingora with early request body buffering (#816), one worker thread, 2,000 concurrent streamed responses and 100 KB request bodies, running with glibc MALLOC_MMAP_THRESHOLD_=65536:

  • RSS per open request: 219 → 165 KiB (−25 %).
  • 10,000 open requests: 2.27 → 1.67 GB.

We also saw a CPU improvement for small requests (21.1 → 18.7 µs). That comes from the allocator setting, which sends each 64 KiB buffer through mmap, so it may not apply elsewhere.

Possible fix

  • Drop body_reader.body_buf in read_body_bytes once body_done(). The chunk has already been copied, and pipelined bytes have already been moved to body_buf_overread by finish_body_buf.
  • For a known Content-Length, allocate min(CL + 1 KiB, body_buf_size), and bound upstream Partial reads the same way.

What stays the same:

  • Large bodies: for bodies of 64 KiB or more, the buffer size and the read size are unchanged; only the release at the end of the body applies to them.
  • Excess detection (60a7bcc, "Discard extra upstream body and disable keepalive"): the upstream read still goes past Content-Length, now by up to 1 KiB. One extra byte is enough for has_bytes_overread(), so the connection is still not reused. Excess that arrives in a later read was not detected before either.

The only behaviour change: when an upstream sends more than 1 KiB past its Content-Length, only 1 KiB of the excess is read instead of up to 64 KiB. The rest stays unread on a connection that is closed anyway.

A branch with the change and tests is here: https://github.com/mariojcr/pingora/tree/h1-body-buffer. The tests cover four cases: the buffer is released at the end of the body, a pipelined request is kept, a small body gets a small buffer, and excess is still detected. pingora-core tests pass on main.

Would you be open to a PR along these lines, or would you prefer a different approach?