← Concepts

阅读中文版本

Nagle Algorithm

The Nagle algorithm delays small TCP writes to coalesce them into full segments, trading latency for efficiency; it interacts badly with delayed acknowledgments and small RPC writes.

http

Definition

Nagle coalescing holds small outgoing segments until outstanding data is acknowledged or a full segment accumulates. It reduces packet overhead for chatty protocols but can add up to a round trip of delay per small message, and combined with delayed ACKs it produces the classic 40-millisecond stall in request-response protocols.

Context and scope

Relevant to interactive TCP protocols, game and terminal traffic, and any request-response system where writes are smaller than a segment. TLS record layer and WebSocket frames inherit the effect.

Common misconceptions

  • Disabling Nagle always speeds things up — it trades bandwidth for latency and can hurt congested links
  • Nagle explains all latency spikes — most come from delayed ACK interaction, not Nagle alone
  • HTTP/2 makes Nagle irrelevant — small frames still hit TCP write paths unless the stack batches them

Anti-patterns

  • Leaving Nagle on for latency-sensitive small-message protocols like trading or game state updates
  • Setting TCP_NODELAY on bulk transfer sockets where segment efficiency matters more than per-write latency
  • Diagnosing 40ms stalls as application logic bugs instead of Nagle plus delayed ACK interaction

Related concepts

Related examples

Authoritative source

https://datatracker.ietf.org/doc/html/rfc896

What it is

When an application writes a few bytes, Nagle asks whether a full segment is ready and whether previously sent data has been acknowledged. If either fails, the bytes wait. The goal is avoiding many tiny packets that each carry disproportionate header overhead on slow links.

How it works

The interaction with delayed acknowledgments creates the trap: the receiver holds its ACK hoping to piggyback data, while the sender holds the small segment waiting for that ACK. Neither timer expires for tens of milliseconds, so request-response round trips inflate. TCP_NODELAY disables the coalescing.

When to use it

Keep Nagle enabled for terminal-style or telemetry streams where bandwidth efficiency matters and a few milliseconds are acceptable. Disable it for latency-critical RPC, multiplayer state, and financial order streams. Measure round-trip distributions before and after, not just averages.

Pitfalls

The fix is protocol-shaped: batching writes at the application level removes both overhead and delay. Blindly disabling Nagle on thousands of chatty connections can raise packet rate and router queue pressure. A single write call per logical message avoids triggering coalescing at all.