Intermediate LevelNode JS
How does tuning highWaterMark in stream constructors optimize buffer memory consumption?
❓ Interview Prompt: highWaterMark sets the internal buffer threshold in bytes (default 64KB for readable streams). Increasing to 512KB accelerates high-throughput disk I/O; decreasing to 16KB conserves RAM when handling thousands of concurrent client sockets.
'highWaterMark' sets the maximum byte threshold for a stream's internal buffer before backpressure pauses reading:
•
Default: 64KB for readable streams, 16KB for byte streams.
•
Tuning: Increase 'highWaterMark' (e.g. 512KB) for fast local disk reads; lower 'highWaterMark' (e.g. 16KB) for memory-constrained HTTP servers with 10,000+ open sockets.
Code Implementation Example
javascript
import fs from 'node:fs';
// Create stream with custom 256KB buffer threshold
const stream = fs.createReadStream('video.mp4', {
highWaterMark: 256 * 1024 // 256KB buffer
});💡Tuning highWaterMark buffer limits on Readable Streams.
🎯 Interview Key Takeaway
Tune highWaterMark based on stream throughput and memory constraints.
#highWaterMark#Streams#Backpressure#Buffer#Intermediate
Back to all Node JS Questions→