A Gentle Note on Backpressure
When a fast producer meets a slow consumer, something has to give. The question is what — and backpressure is how a well-behaved system answers it.
Three honest options
- Buffer — absorb the spike, until memory says no.
- Drop — shed load deliberately, and admit it in your metrics.
- Slow down — push back on the producer so it eases off.
Each is a trade-off. The mistake is pretending you didn't make one.
async function* throttle<T>(src: AsyncIterable<T>, max: number) {
let inFlight = 0
for await (const item of src) {
while (inFlight >= max) await tick()
inFlight++
yield item
}
}
The systems I trust most are the ones that are honest about their limits.
