Node.js fixes backpressure miscalculation by switching to byte‑accurate accounting

Illustrative photo: Node.js fixed an HTTP backpressure bug using byte-accurate accounting.

Node.js has merged a core patch that corrects a long‑standing backpressure bug: the HTTP OutgoingMessage class now counts buffered data in bytes rather than UTF‑16 code units, fixing under‑reporting for multi‑byte characters.

The mis‑counted metric

In the original implementation, `string.length` was used to update the internal counter that drives backpressure. Because `length` reports UTF‑16 code units, a character such as “é” (one code unit) was considered a single byte, while it occupies two bytes in UTF‑8. For CJK text the discrepancy can reach three‑fold, so the buffer appeared half‑empty and `response.write()` kept returning true even after the high‑water mark was exceeded.

A minimal reproduction shows the effect: writing 50 copies of “é” to a message with a `highWaterMark` of 100 registers an output size of 50 (the code‑unit count) and returns `true`, while the actual payload is 100 bytes and should have caused `write()` to return `false`.

What the fix changes

The patch, submitted as nodejs/node#66039, makes the accounting use the byte length supplied by callers (the new `size` argument) and falls back to `Buffer.byteLength(chunk, encoding)` when that argument is missing. It also adds the byte size of the HTTP header that is prepended on the first write, ensuring the total count reflects everything sent on the wire.

All other behaviours remain untouched: ASCII strings, Buffer instances, and `latin1` writes continue to work exactly as before. The only observable difference is that `write()` can now correctly return `false` for multi‑byte bodies that previously slipped past the threshold.

Performance impact

Benchmarking shows the extra `Buffer.byteLength()` call runs only on the buffered path and only when a length isn’t already provided. On a typical keep‑alive server about 83 % of `_writeRaw()` calls go straight to the socket, bypassing the extra calculation entirely. End‑to‑end throughput for multi‑byte bodies (2.8 kB and 70 kB) was unchanged across three runs, while a micro‑benchmark that forces every write to buffer recorded roughly a 25 % overhead on that specific operation – a path that only activates once the buffer is already full.

Analysis: why accurate backpressure matters

Accurate backpressure accounting is essential for high‑throughput Node.js servers that serve international content. When the buffer size is underestimated, the runtime delays pausing the underlying TCP socket, allowing memory to grow unchecked and potentially leading to out‑of‑memory crashes under load. The bug primarily affected applications that streamed UTF‑8 data with non‑ASCII characters – for example, APIs returning localized JSON, image metadata, or real‑time chat messages in East Asian scripts. By switching to byte‑accurate metrics, the fix restores the contract between `write()` and the `drain` event, letting developers rely on standard flow‑control patterns without adding custom size calculations. The change also underscores a broader lesson for Node.js developers: `string.length` is unsuitable for I/O budgeting. Wherever memory or network usage is measured, `Buffer.byteLength()` or an explicit byte count should be used. The patch demonstrates that even core primitives can harbor subtle performance bugs, and it shows how community contributions – even those assisted by AI coding tools – can quickly surface and resolve such issues when rigorous testing and manual verification are applied. Overall, the fix improves stability for multilingual services, reduces the risk of hidden memory bloat, and provides a clear example of why byte‑level accounting remains the correct approach for networked applications.

Cover: Carl Lender from Sunrise, USA · CC BY 2.0 · Wikimedia Commons. Layout and headline text by Signal Titans.

Source: Why Node.js HTTP backpressure must count bytes, not string length · Why Node.js HTTP backpressure must count bytes, not string length · Ali Ahmed

Share this article
FacebookThreadsLinkedInX

Leave a Reply

Your email address will not be published. Required fields are marked *