Streams and Backpressure
As a Mid-level engineer, explain Streams and Backpressure. What problem does it solve and how would you describe it in an interview?
Answers use simple, clear English.
Quick interview answer
At Mid-level depth: start with the problem, then mechanism, then a short example. Readable pushes data; Writable signals backpressure when internal buffer exceeds highWaterMark.
Detailed answer
At Mid-level depth: start with the problem, then mechanism, then a short example. Readable pushes data; Writable signals backpressure when internal buffer exceeds highWaterMark. Respect drain event before resuming writes to avoid memory blowup. Core: Readable pushes data; Writable signals backpressure when internal buffer exceeds highWaterMark. Respect drain event before resuming writes to avoid memory blowup. Real-time example: ETL job pipes HTTP download → gzip transform → S3 upload without loading entire file into RAM. Pros: Constant memory for large files; composable pipeline with pipe(). Cons: Error handling across piped streams needs explicit destroy/abort logic. Common mistakes: Ignoring write() return false; not awaiting 'drain' on backpressure. Best practices: Use pipeline() from stream/promises for automatic cleanup on error. Audience level: Mid-level.
Full explanation
Readable pushes data; Writable signals backpressure when internal buffer exceeds highWaterMark. Respect drain event before resuming writes to avoid memory blowup.
Real example & use case
ETL job pipes HTTP download → gzip transform → S3 upload without loading entire file into RAM.
Pros & cons
Pros: Constant memory for large files; composable pipeline with pipe(). Cons: Error handling across piped streams needs explicit destroy/abort logic.
Common mistakes
Ignoring write() return false; not awaiting 'drain' on backpressure.
Best practices
Use pipeline() from stream/promises for automatic cleanup on error.
Follow-up questions
- How would you test Streams and Backpressure?
- What metrics prove Streams and Backpressure is healthy in prod?
- How does Streams and Backpressure change at 10× traffic?