← Lessons

quiz vs the machine

Gold1410

Networking

The Nagle Algorithm

How TCP coalesces tiny writes to avoid flooding the network with small packets.

4 min read · core · beat Gold to climb

The tiny packet problem

Sending one byte over TCP attaches dozens of bytes of headers, so a stream of single byte writes wastes enormous bandwidth. The Nagle algorithm fixes this by holding back small sends until they can be combined.

The rule

Nagle allows at most one small unacknowledged segment in flight at a time:

  • If there is unacknowledged data and the new data is smaller than a full segment, buffer it.
  • When the outstanding acknowledgment arrives, flush the buffered bytes as one larger segment.
  • A full sized segment may always go out immediately.

This naturally batches bursts of small writes into fewer, fuller packets without any application change.

The interaction trap

Nagle can fight with delayed acknowledgments. The receiver waits to acknowledge, while the sender waits for that acknowledgment before sending more, producing a stall. Interactive and request response protocols often disable Nagle with the no delay socket option to avoid this lag.

Key idea

Nagle batches small writes by allowing only one unacknowledged small segment at a time, which can be disabled with no delay for low latency.

Check yourself

Answer to earn rating on the learn ladder.

1. What does the Nagle algorithm do?

2. Why do interactive applications often disable Nagle?