Practical technical guides / Core IT: networking (TCP)

Write, Write, Read Stalls on TCP: Nagle Meets Delayed ACK, and Where TCP_NODELAY Belongs

Two small writes then a read can wait a full delayed-ACK interval: 41 ms instead of 0.5 ms in the model. Why Nagle and delayed ACK interact, and the fixes in order.

By Sandeep Belgavi · Published 2026-11-07

Write, write, read on one connection
Original explanatory diagram; synthetic values where shown.

Two small writes then a read can wait a full delayed-ACK interval: 41 ms instead of 0.5 ms in the model. Why Nagle and delayed ACK interact, and the fixes in order.

A 40 millisecond floor that should not be there

The stock service at Belgavi's Book Store answers price and availability lookups over a long-lived TCP connection with a small binary protocol. A lookup is a 40-byte header followed by a 60-byte body, and the reply is about 200 bytes. On a quiet test network the round trip is a fraction of a millisecond. In production, the client's p50 latency sits at just over 40 milliseconds no matter how idle the servers are. Adding servers does nothing. Moving the client closer does nothing.

The cause is in the client, and it is two lines long: the request is written with two send() calls, one for the header and one for the body. That pattern, write-write-read, is the classic trigger for an interaction between two old and individually sensible TCP features: Nagle's algorithm on the sender and delayed acknowledgements on the receiver. This guide walks through the interaction from the RFCs, derives the delay, and compares the fixes. The interactive lab lets you change each parameter and watch every segment.

Two good ideas that meet badly

Nagle's algorithm (RFC 896, 1984; restated in RFC 9293 section 3.7.4) stops a sender from flooding the network with tiny packets. RFC 9293 puts it this way: if there is unacknowledged data, "the sending TCP endpoint buffers all user data (regardless of the PSH bit) until the outstanding data has been acknowledged or until the TCP endpoint can send a full-sized segment". In short, at most one small segment may be in flight. A full-sized segment is always allowed.

Delayed ACK (RFC 1122 section 4.2.3.2) stops a receiver from sending an acknowledgement for every segment. "A TCP SHOULD implement a delayed ACK, but an ACK should not be excessively delayed; in particular, the delay MUST be less than 0.5 seconds, and in a stream of full-sized segments there SHOULD be an ACK for at least every second segment." The receiver waits briefly hoping to piggyback the ACK on data it is about to send. Linux defines TCP_DELACK_MIN as HZ/25, 40 ms, and TCP_DELACK_MAX as HZ/5, 200 ms, in include/net/tcp.h, and adapts the timer per connection between them.

Each idea saves packets. Together, on write-write-read:

  1. The client writes the 40-byte header. Nothing is in flight, so Nagle lets it go at once.
  2. The client writes the 60-byte body. The header is unacknowledged and the body is smaller than one segment, so Nagle holds it.
  3. The server receives the header. The request is incomplete, so the application has nothing to send back. The segment is not full-sized and it is the only one, so the receiver starts its delayed-ACK timer.
  4. The timer fires. The server sends a pure ACK for the header.
  5. The ACK reaches the client, which releases the body.
  6. The server receives the body, the request is complete, and the reply goes back carrying the ACK.

With a one-way delay d and a delayed-ACK timer t, the reply arrives after 4d + t instead of 2d. On a LAN with d of 0.25 ms and a 40 ms timer that is 41 ms instead of 0.5 ms. With a 200 ms timer it is 201 ms. Across a 40 ms one-way WAN link it is 200 ms instead of 80.

The fixes, in order of preference

FixWhereEffect on write-write-readCost
Coalesce the request into one writeclient codeone segment, 0.5 msnone; usually the right fix
writev or a buffered writer flushed once per requestclient codeone segmentnone
TCP_CORK around the request (Linux)client socketone segment when uncorkedLinux-only, must uncork
TCP_NODELAYclient sockettwo segments, 0.5 msmore small packets for chatty writers
TCP_QUICKACK on the server (Linux)server socketheld one round trip, about 1 msnot permanent, the kernel can leave quick-ACK mode

Coalescing is the real fix because it removes the second small write instead of changing how TCP treats it. In Python that means building the whole request in a buffer and calling sendall once; in Java, wrapping the socket stream in a BufferedOutputStream and calling flush() once per request; in C, writev with both parts.

import socket, struct

def send_lookup(sock: socket.socket, isbn: bytes, store: int) -> bytes:
    body = struct.pack('!13sI', isbn, store) + b'\x00' * 43   # 60-byte body
    header = struct.pack('!4sI', b'BKST', len(body)) + b'\x00' * 32  # 40-byte header
    sock.sendall(header + body)          # one write, not two
    return sock.recv(4096)

TCP_NODELAY disables Nagle on that socket. Almost every RPC library already sets it, because request-response protocols are exactly the case Nagle hurts. It is the right setting for interactive traffic, but it does not remove the extra packet: two writes still become two segments. A client that writes a request one field at a time with TCP_NODELAY sends a burst of tiny packets, which is what Nagle was invented to stop.

TCP_CORK on Linux holds partial segments until you uncork, so the header and body leave together. The tcp(7) man page notes a 200 millisecond ceiling, after which queued data is sent anyway, and that the option "should not be used in code intended to be portable".

TCP_QUICKACK makes the receiver acknowledge immediately, but the man page is clear that it "is not permanent": the kernel moves in and out of quick-ACK mode on its own. It is a mitigation for servers you cannot change clients of, not a fix.

Why some cases never stall

Two details explain why the stall can come and go with message sizes:

  • Full-sized segments go out under Nagle. If the body is 3,000 bytes, the client sends the header, then two full 1,448-byte segments at once, and holds only the 104-byte remainder. The receiver now has two full segments unacknowledged, so the "ACK at least every second segment" rule makes it ACK at once, and the remainder follows after one round trip, about 1 ms on the LAN in the lab. Raise the receiver's threshold to every third segment and the 41 ms stall returns.
  • A later write may find the first one acknowledged. If the body is written 50 ms after the header, the header's ACK has already arrived and Nagle sends the body immediately. The stall needs the second write to come while the first is still in flight.

What I measured, and what I did not

The companion example includes a loopback probe that times 40 write-write-read round trips with and without TCP_NODELAY and with one coalesced write. On Windows 11 loopback all three had medians below 0.1 ms: the stall did not appear. Loopback paths can behave differently from a real network interface, so a missing stall on loopback is not evidence that production is safe. I had no Linux host or second machine for this guide, so the 41 ms and 201 ms figures come from the rule model in the lab and the companion Python code, built from the RFC rules and the Linux constants above. To check a real service, capture a few requests with tcpdump or Wireshark and look for a pure ACK roughly 40 or 200 ms after the first small segment, followed immediately by the client's second segment.

Diagnosing it in production

  • Latency clusters at a fixed floor such as 40 ms or 200 ms, independent of load, and drops to near zero when the client batches.
  • Packet captures show the pattern: small client segment, pause, pure ACK from the server, second client segment, then the reply.
  • ss -ti on Linux shows the connection's ato, which ss(8) documents as the ACK timeout used in delayed-ACK mode, in milliseconds.
  • The fix test is cheap: switch the client to one write per request in a canary and compare p50.

Failure modes after the fix

  • A library upgrade reintroduces a second write, for example by writing a length prefix separately. Keep a test that counts writes per request, or a canary latency alert at the old floor.
  • TCP_NODELAY set globally on a bulk sender increases packet counts and CPU. Set it on request-response sockets, not everywhere.
  • Message framing with a flush after every field defeats a buffered writer. Flush once per message.

The lab models one exchange with no loss, no congestion window and a fixed delay; it is a tool for reasoning about the rule interaction, not a replacement for a packet capture.

Interactive experiment

Nagle and delayed ACK: a write-write-read timeline. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/nagle_delayed_ack. Views are my own and do not represent any employer, past or present.