How-To Guide

How to Tune TCP for Latency

Tune TCP for low latency: congestion control, socket buffers, fast open, nagle and delayed ACK, and measure the effect rather than guessing.

On this page

  1. Introduction
  2. Where TCP Latency Comes From
  3. Congestion Control
  4. Socket Buffers and the Bandwidth-Delay Product
  5. Nagle and Delayed ACK Interaction
  6. Connection Reuse and TCP Fast Open
  7. Measuring, Not Guessing
  8. Common Pitfalls and Conclusion

Introduction

Network latency between services is often blamed on the network, but a meaningful part of it is TCP configuration. The congestion-control algorithm, the socket buffers, whether Nagle’s algorithm is fighting delayed ACKs, and whether a new connection pays a full handshake all add milliseconds that compound across a call graph. Tuning TCP for latency means knowing which knob affects which delay and measuring the change, because a knob that helps one workload hurts another.

Where TCP Latency Comes From

A request over TCP pays for connection setup (a handshake), for sending the request and receiving the response (round trips and the congestion window), and for any retransmissions if packets are lost. On a call graph, each hop adds its share, so a few milliseconds per hop becomes tens across a request.

The dominant factors are usually the number of round trips and the congestion window. A fresh connection pays one or more round trips before data; a cold congestion window limits how much can be in flight; and loss can stall the connection for a retransmission timer. Tuning targets each of these.

The relative weight of each depends on the path: on a low-latency network the handshake dominates and connection reuse is the win, while on a high-latency path the congestion window and buffer sizing dominate. Measure the path before choosing which knob to turn.

# where time goes on a TCP request
connection setup     ~1 RTT (SYN, SYN-ACK) before any data
congestion window    cold start limits in-flight data (slow start)
delayed ACK + Nagle  small writes wait for an ACK -> added delay
retransmission       a lost packet stalls until the timer fires
# per hop; across a call graph the shares add up

Congestion Control

The congestion-control algorithm decides how fast the sender ramps up and how it reacts to loss. Loss-based algorithms (CUBIC, the Linux default for years) treat loss as congestion and back off, which is conservative on low-loss, high-bandwidth paths. Modern algorithms use delay or explicit signals and ramp faster.

For low-latency internal networks with low loss, a modern algorithm such as BBR can send faster without waiting for loss, reducing queueing and tail latency. Test it against your workload: it helps high-throughput low-loss paths and is not a universal win.

# inspect and set the congestion-control algorithm (Linux)
sysctl net.ipv4.tcp_congestion_control          # current
sysctl net.ipv4.tcp_available_congestion_control
sysctl -w net.ipv4.tcp_congestion_control=bbr    # test a modern algorithm
# measure: latency percentiles and throughput before/after on a real path

Socket Buffers and the Bandwidth-Delay Product

To keep a path full, the socket buffer must hold at least the bandwidth-delay product: bandwidth times round-trip time. If the buffer is smaller, the sender stalls waiting for acknowledgements and the throughput drops even on a fast link. Auto-tuning usually handles this, but on constrained environments it is worth checking.

Larger buffers can also increase latency by allowing more queueing (bufferbloat), so the goal is not “as large as possible” but “large enough for the bandwidth-delay product and no more”. Measure the trade-off between throughput and latency rather than maximising one.

# socket buffer and auto-tuning knobs
sysctl net.ipv4.tcp_rmem        # receive buffer min/default/max
sysctl net.ipv4.tcp_wmem        # send buffer min/default/max
sysctl net.ipv4.tcp_autotuning  # kernel auto-tunes within the range
# buffer >= bandwidth x RTT keeps the path full; too large => bufferbloat

Nagle and Delayed ACK Interaction

Nagle’s algorithm coalesces small writes to avoid tiny packets, but combined with delayed ACKs it can add tens of milliseconds: the sender waits to coalesce, the receiver waits to acknowledge, and each waits for the other. For latency-sensitive request/response traffic, disabling Nagle (TCP_NODELAY) is usually right.

Set TCP_NODELAY on request/response sockets so a small message leaves immediately instead of waiting for more data. On bulk streaming where you write large buffers, Nagle matters less. Know which kind of traffic each socket carries and set the option accordingly.

import { createConnection } from 'node:net';

const socket = createConnection({ host, port });
socket.setNoDelay(true);   // disable Nagle: send small writes immediately
// for request/response latency, TCP_NODELAY avoids the Nagle+delayed-ACK stall
// for bulk streaming of large buffers, Nagle matters less

Connection Reuse and TCP Fast Open

A new connection pays a handshake, and on a call graph those handshakes add up. Reusing connections (HTTP keep-alive, a connection pool) removes the setup cost from repeated calls, which is usually the largest single win. Tune keep-alive so idle connections are not closed and reopened needlessly.

TCP Fast Open lets a client send data with the initial handshake on a resumed connection, saving a round trip. It is not universally supported and has security considerations, so treat it as an optimisation to test, not a default. Connection reuse is the reliable win; Fast Open is a bonus where supported.

# connection reuse beats handshake tuning
pool + keep-alive => repeated calls skip the handshake entirely (biggest win)
# tune keep-alive so idle connections are not torn down and reopened
# TCP Fast Open: data with the handshake on resume; not universal; test it

Measuring, Not Guessing

Every tuning change must be measured against the workload it targets, with latency percentiles (p50, p99) and throughput, before and after. A knob that reduces p50 can raise p99, and a change that helps throughput can hurt latency. Without measurement you are guessing and may make it worse.

Measure on the real path, not in a lab, because the network’s RTT and loss profile determine which knob matters. And change one knob at a time so the effect is attributable; a bundle of changes cannot be diagnosed when the result is unexpected.

# measure latency percentiles on the real path
#   client-side: p50/p95/p99 request duration
#   kernel: ss -ti shows per-socket RTT, cwnd, retransmits
ss -ti | grep -A1 ':443'    # inspect cwnd, rtt, retrans on live sockets
# change ONE knob, re-measure, compare percentiles (not just the mean)

Common Pitfalls and Conclusion

Do not enable TCP_NODELAY everywhere without knowing the traffic; it is right for request/response, less relevant for bulk writes. Do not maximise socket buffers and create bufferbloat. Do not assume a modern congestion algorithm always wins; test it. Do not skip connection reuse while tuning handshake details. And do not change several knobs at once.

TCP latency tuning is a few targeted changes measured against the real path: use connection reuse first, set TCP_NODELAY for request/response sockets, choose a congestion algorithm suited to the network, size buffers to the bandwidth-delay product, and measure percentiles after each change. Done deliberately, it removes the milliseconds that a call graph otherwise multiplies.

Sources: Linux: TCP congestion control, RFC 5681: TCP Congestion Control, RFC 7413: TCP Fast Open.

FAQ

What is the single biggest TCP latency win?
Connection reuse. Reusing connections (keep-alive, pooling) removes the handshake from repeated calls entirely, which usually beats any handshake-level tuning. Tune the other knobs from there, measuring percentiles each time.
Why is TCP_NODELAY usually right for request/response?
Because Nagle combined with delayed ACKs can stall small messages by tens of milliseconds: the sender waits to coalesce and the receiver waits to acknowledge. TCP_NODELAY sends the small write immediately, which is what latency-sensitive request/response traffic wants.

Related Examples