Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is No-Buffering feature of Node.js

Tags:

node.js

buffer

Everywhere, it mentions as Node.js has No Buffering feature. For instance from Tutorials Point:

No Buffering − Node.js applications never buffer any data. These applications simply output the data in chunks.

But what does it signifies? Node.js has a Buffer class and as per Buffer class definition

Pure JavaScript does not handle straight binary data very well, though JavaScript is Unicode friendly. When dealing with TCP streams and reading and writing to the filesystem, it is necessary to deal with purely binary streams of data.

Which holds true.

So, what is the feature of "No Buffering" in Node.js?

And how does Node.js holds to No Buffering? Can anyone explain with an example?

like image 613
Veer Shrivastav Avatar asked Mar 04 '17 13:03

Veer Shrivastav


People also ask

What does buffer mean in node JS?

What Are Buffers? The Buffer class in Node. js is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data.

What are buffers and stream in node JS?

Buffers in Streams In Node Js, buffers are used to store raw binary data. A buffer represents a chunk of memory that is allocated on our computer. The size of the buffer, once set, cannot be changed. A buffer is used to store bytes.

What is buffer Alloc in node JS?

The Buffer. alloc() method creates a new buffer object of the specified size.

What is difference between buffer and stream?

Buffering is the practice of pre-loading segments of data when streaming video content. Streaming — the continuous transmission of audio or video files from a server to a client — is the process that makes watching videos online possible.


1 Answers

While Node.js allows you work with data streams (ie. chunked data), that does not necessarily mean that "Node.js never buffers data" - that is up to the developer to handle.

Typical streaming is done by creating a callback which receives data over a period of time, does something with that data, or handles it off to another stream.

This means that the consumer of the data can get it more readily without having to wait for the entire operation to complete.

This is what TutorialPoint was eluding to, although poorly said — and for the most part, incorrect.


Bufferring is actually key to working with streamable data. In the sense that the actual data will reside in a Buffer until your Callback pulls the data out of the stream to work with.

This is why Node.JS has Buffer classes and libraries such as stream-buffers, to facilitate stream data access.


A common pain point in dealing with streams & buffers is something called: "Back Pressure"


This is when you have a data stream with a Producer and a Consumer, but due to load issues, your Consumers are falling behind in consuming the data at a fast enough pace compared to the rate of the Producers pumping data into the stream.

This causes the so called "back pressure" effect, and can bring down a system if the Producers are not rate limited.

Back pressure is relevant to the discussion because it is caused by the buffer being filled faster than your callback can push out data back to the user or another stream.

Therefore, bufferring — at some level or another— is essential to the operation of Node.js and is how it handles continuous streams of data. That is why Buffer class exists, as well as Streaming buffers — to facilitate the movement of data between callbacks.

like image 143
bailey.cosier Avatar answered Oct 23 '22 05:10

bailey.cosier