I believe the following is the best way to work with Nagle's algorithm /
It is described in this RedHat manual and the verdict is:
TCP_NODELAY = 1, always.
- If you can batch data for sending by creating a buffer manually, or using
writev(), prefer that.
- If you cannot (e.g. "when using different libraries that provides abstractions for layers" from the above manual):
TCP_CORK = 1, then write the data, then set
TCP_CORK = 0.
- This builds a packet in kernel space and then flushes it out.