TCP_NODELAY (disable Nagle)
TechniqueNetworking and OSMidSeniorUpdated
Nagle's algorithm coalesces small writes to reduce tinygrams. Combined with delayed-ACK on the receiver, it can stall small request/response pairs for ~40ms waiting for the next ack.
When to Use TCP_NODELAY (disable Nagle)
Latency-sensitive request/response over TCP with small payloads: RPC, Redis, database protocols. Set TCP_NODELAY on the socket; almost every RPC library does this by default.
In Practice
Classic symptom: small HTTP POST shows a 40ms p50 floor that nothing else explains. Cause: client writes header then body in separate calls, Nagle holds the body, server's delayed-ACK waits for more data.
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, 1) fixes it.How TCP_NODELAY (disable Nagle) Comes Up in Interviews
Scenario
Your service POSTs small JSON payloads to a backend over TCP. p50 is great but p90 sits at exactly ~40ms with no other explanation. What is going on?
Classic Nagle plus delayed-ACK interaction: client writes header and body as separate small packets, Nagle holds the second one waiting for an ack, receiver's delayed-ACK waits up to 40ms for more data. Set TCP_NODELAY.
Nagle was useful when telnet sent 1 byte at a time; modern protocols send framed messages and want them out the door immediately. Most RPC libraries set TCP_NODELAY by default; raw socket code often does not.