Buffers

Connection buffers network input and output to reduce the number of system calls when reading or writing messages. Write buffers are also used for constructing WebSocket frames. A Websocket frame header is written to the network each time a write buffer is flushed to the network. Decreasing the size of the write buffer can increase the amount of framing overhead on the connection.

Buffer is essentially just a []byte with some methods to make it usable with other components. Like, you can write to it (which appends to the slice) and you can read from it (which returns data from it). Think of it as an in-memory file.

Stream & Buffer

A data stream as the name suggests is a stream of data. We can visualize this as the stream of water flowing from one end to another. Each droplet of water is the packet of data flowing in the stream and we can resize this data packet as per our needs.

If we have a massive file to deal with, we can process the file’s data one packet at a time instead of loading the entire file in the memory. Also, we can write to a file or data store one packet at a time.

Buffer, on the other hand, is a temporary region of volatile memory (RAM) where data can be stored before consuming it. We can read from or write to a data buffer using streams.