Can anyone explain use case, when default buffering is really useful?
IMHO, it just make to place additional code every time, to disable it:
I'm going to assume that you mean "delay" instead of "buffering" here.
It's on by default because all (modern) operating systems have it on by
default. Node doesn't do anything to enable Nagle, it just makes a specific
syscall to turn it off when you use .setNoDelay().
1. If one send data with single block (render template, and return all
page), then Nagle will not help, and will cause double buffering.
There is no "double buffering" - the TCP stack will always buffer your data
so it can retransmit your data when delivery fails. The (in certain
circumstances) problematic part is - literally - the delay. When nagle is
enabled the tcp stack will sometimes hold your data for a small amount of
time (typically in the order of 100ms) before putting it on the wire.
Please note that when you send a not-very-small amount of data (such that
it fills up at least 1 TCP packet - typically around 1.5kb) your data will
be transmitted without any delay.
2. If one send data as stream, there are 2 subcases:
- small messaging - delay must be disabled to deliver each message ASAP
If you are sending small messages over a persistent connection, that is
indeed the situation where you might want to use setNoDelay. Note that if
you send a small amount of data and tear down the connection afterwards
(such as sending a small http response) the delay doesn't occur.
- big files via pipes - they already have internal buffers (and it's
more effective to increase file buffers)
Pipes and unix sockets do not use (or even support) the Nagle algorithm. So
it's off by default already.
I don't pretend on 100% coverage. That's just the most used cases, IMHO.
So, looks like it's better to disable Naggle by default.
We are not going to change anything about this.
It's not a religion question, "do you beleive in nagle power or not". So,
abstract talks will not make anything useful.
It answered your question "why is nagle on by default" quite adequately imo
Just give one example from the real life, when nagle in node is useful.
If you know.
With nagle enabled the entire message will be sent in a single tcp packet.
With nagle off, 2 packets will be sent.