Piping and Backpressure
Backpressure occurs when a writable destination cannot consume data as quickly as a readable source produces it. Node.js streams provide mechanisms for controlling this flow.
The pipe() method connects streams and handles important flow-control behavior automatically. Modern applications can also use pipeline() from node:stream/promises to compose streams while handling errors more safely.
Prefer pipeline for Complex Stream Chains
pipeline() provides cleaner error handling and lifecycle management when connecting multiple streams.
Using pipeline
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
await pipeline(
createReadStream('input.txt'),
createWriteStream('output.txt')
);
console.log('Copy completed');💡
pipeline() connects the streams and provides Promise-based completion and error propagation.Key Takeaways
- Backpressure prevents fast producers from overwhelming slow consumers.
- Use
pipeline()when composing multiple streams. - Stream errors must be handled explicitly.
Knowledge Check
What does backpressure protect against?