Both Buffers and Streams are used to handle binary data (data that isn't just plain text, like images, video, or zip files). The difference lies in how much of that data they hold at once and how they process it. Buffers are useful when you have all the data upfront and want to process it, while streams are useful when dealing with large amounts of data or data coming from an external source, allowing you to start processing before all the data has arrived.
A Buffer is a small, fixed-size chunk of memory allocated outside the V8 heap. Think of it as a bucket that holds a specific amount of data.
A Stream is a sequence of data that is moved from one place to another over time. Think of it as a garden hose or a conveyor belt.
You need to read a small text file and send its contents over an HTTP response. Would you use a Buffer or a Stream, and why?
If you call fs.readFileSync on a 5 KB file, what type of object do you get back, and how does that differ from using fs.createReadStream?
What happens if you try to pipe a Buffer directly into a writable stream?
Your team added a feature that streams video chunks from disk to clients, but memory usage spikes. How would you investigate whether Buffers are being misused?
During a file upload, the server crashes when the uploaded file is larger than 10 MB. The code currently collects the entire request body into a Buffer before processing. How would you refactor it using streams?
Explain why a Transform stream might be preferable to manually concatenating Buffers when processing a CSV file line by line.
Design a Node.js service that ingests high‑throughput log data from many sources and writes them to a compressed file. Discuss how you would combine streams and Buffers to keep memory footprint low and maintain back‑pressure.
You need to implement a custom protocol over TCP that requires framing messages with a length prefix. How would you use Buffers and streams together to parse incoming data efficiently?
Our current pipeline reads entire JSON payloads into Buffers before validation, causing GC pauses under load. Propose a streaming validation approach and explain trade‑offs.
Our monolith stores large binary blobs in a legacy Buffer‑based API, but we are moving to a microservice architecture that streams data between services. What architectural changes are needed, and how would you handle compatibility during migration?
When scaling a real‑time analytics platform, we must decide between a Buffer‑centric batch processing model and a fully streaming architecture. How would you evaluate the impact on latency, fault tolerance, and operational complexity?
Describe a strategy for standardizing Buffer vs Stream usage across multiple teams to avoid memory leaks and ensure consistent back‑pressure handling in a large codebase.