Ktor streaming response backpressure and memory exhaustion risks
0 reputation · 29 May 2023, 07:06 UTC
When serving large datasets using call.respondStream or ByteReadChannel, the goal is to maintain a low memory footprint by avoiding loading the entire collection into memory. While Ktor provides primitives to stream data in chunks, the interaction between the data source and the client-side consumption rate remains complex.
Specifically, if a HTTP client consumes data at a rate slower than the server produces it, it is unclear if Ktor natively propagates backpressure signals back to the underlying data source (such as a database cursor or a reactive flow). Without a direct signal to pause the data generation, the internal buffers may grow, potentially leading to Out-Of-Memory errors under high load or slow network conditions.
How does Ktor handle backpressure from a slow HTTP client back to a suspending data-producing block? Is there a built-in mechanism to pause the execution of the stream when the ByteWriteChannel buffer is full?