Lably
Skip to content

Nagle's algorithm

Means of improving the efficiency of TCP/IP networks

  • 4 min read
  • 4 chapters

Related on Lably

Through Internet protocol suite, Telnet and Pseudocode

The RFC describes what Nagle calls the "small-packet problem", where an application repeatedly emits data in small chunks, frequently only 1 byte in size. Since TCP packets have a 40-byte header (20 bytes for TCP, 20 bytes for IPv4), this results in a 41-byte packet for 1 byte of useful information, a huge overhead. This situation often occurs in Telnet sessions, where most keypresses generate a single byte of data that is transmitted immediately. Worse, over slow links, many such packets can be in transit at the same time, potentially leading to congestion collapse.

Nagle's algorithm works by combining a number of small outgoing messages and sending them all at once. Specifically, as long as there is a sent packet for which the sender has received no acknowledgment, the sender should keep buffering its output until it has a full packet's worth of output, thus allowing output to be sent all at once.

Algorithm

The RFC defines the algorithm as

inhibit the sending of new TCP segments when new outgoing data arrives from the user if any previously transmitted data on the connection remains unacknowledged.

Where MSS is the maximum segment size, the largest segment that can be sent on this connection, and the window size is the currently acceptable window of unacknowledged data, this can be written in pseudocode as

<b id="mwHg">if</b> there is new data to send <b id="mwHw">then</b> <b id="mwIA">if</b> the window size ≥ MSS <b id="mwIQ">and</b> available data is ≥ MSS <b id="mwIg">then</b> send complete MSS segment now <b id="mwIw">else</b> <b id="mwJA">if</b> there is unconfirmed data still in the pipe <b id="mwJQ">then</b> enqueue data in the buffer until an acknowledge is received <b id="mwJg">else</b> send data immediately <b id="mwJw">end if</b> <b id="mwKA">end if</b> <b id="mwKQ">end if</b>

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication