TCP: handshake, retransmission, congestion control, and the states that bite

Bytes in flight are bounded by min(cwnd, rwnd). Slow is one of three things: too little in flight for the bandwidth-delay product, too much loss, or waiting on a timer. ss -ti tells you which.

Read the briefing
About 4 min to read 9 drills

What to remember

  1. Throughput ≈ window ÷ RTT. A 64 KB window over 100 ms caps at ~5 Mbps however fat the pipe. That is the bandwidth-delay product.
  2. ss -ti separates loss from a slow application. Rising retrans and shrinking cwnd = loss. Full send queue with healthy cwnd = the peer is slow.
  3. The extra 40 ms is Nagle plus delayed ACK. Header and body in separate writes. Coalesce them or set TCP_NODELAY.
  4. TIME_WAIT belongs to whoever closed first. It is TCP working. Let clients close, and pool connections.
  5. HTTP/2 does not remove TCP head-of-line blocking. One lost segment stalls every stream on that connection. That is why HTTP/3 moved to QUIC.

The big picture

TCP is the protocol behind most "the network is slow" incidents, and the interview version is specific: the handshake and close, how retransmission and congestion control behave under loss, why a receiver window can stall a fast link, the Nagle/delayed-ACK interaction that adds 40 ms to small writes, keepalives and TIME_WAIT, and head-of-line blocking. Reading ss -ti output is the practical skill that ties it together.

The mental model

Bytes in flight are bounded by min(cwnd, rwnd). The sender grows cwnd until loss (or a delay signal) tells it to back off; the receiver advertises rwnd based on how fast the application drains the socket. Latency problems are usually one of: not enough in flight (window too small for the bandwidth-delay product), too much loss (cwnd keeps collapsing), or waiting on a timer (RTO, delayed ACK, keepalive).

Diagrams

The handshake, and who holds TIME_WAIT
client                                    server
  |         SYN  seq=x                      |  LISTEN
  |--------------------------------------->  |
  |    SYN-ACK  seq=y ack=x+1               |  SYN_RCVD
  |  <---------------------------------------|
  |         ACK  ack=y+1                     |  ESTABLISHED
  |--------------------------------------->  |

Closing:
  whoever calls close() FIRST  -> TIME_WAIT (2 x MSL, normal)
  the side that has NOT closed -> CLOSE_WAIT

  CLOSE_WAIT piling up locally is always an application bug:
  the peer hung up and your code never called close().

In depth

Handshake, close, and the states that bite

Open: SYN → SYN-ACK → ACK (one round trip before data; TLS then adds one more in 1.3). Close: each side sends FIN and receives an ACK, so a close is two half-closes. The side that closes first (the active closer) holds the connection in TIME_WAIT for 60 s on Linux to absorb delayed duplicates; that is why a client making thousands of short-lived connections runs out of ephemeral ports, and why servers should generally let clients close. Other states worth recognizing: SYN_SENT piling up means SYNs are not answered (drop or firewall); SYN_RECV piling up means an accept-queue or SYN-flood problem (nstat ListenOverflows, net.core.somaxconn); CLOSE_WAIT piling up means your application is not calling close(), a bug in your code, not the network; FIN_WAIT_2 means the peer has not closed its side.

Reliability, congestion, and flow control

Every byte is sequenced; the receiver acknowledges the next byte it expects. Loss is recovered by fast retransmit (duplicate ACKs / SACK) when there is still traffic to trigger it, or by the RTO timer when there is not: the RTO is derived from smoothed RTT and variance, has a 200 ms floor on Linux, and doubles on each further loss, which is why a single loss at the end of a burst can add hundreds of milliseconds.

Congestion control governs cwnd: slow start doubles it each RTT from an initial ~10 segments; on loss (or the delay signal used by BBR) it backs off, and congestion avoidance grows it slowly. CUBIC is the Linux default; BBR models bandwidth and RTT instead of treating loss as the only signal and often does much better on lossy long-haul paths. Flow control is separate: the receiver's advertised window rwnd is bounded by its buffer and how fast the application reads; a zero window means the application is the bottleneck. Single flow throughput is roughly window ÷ RTT, so a 64 KB window over a 100 ms path caps at about 5 Mbps regardless of link speed: this is the bandwidth-delay product, and window scaling exists to fix it.

Small writes, keepalives, and head-of-line blocking

Nagle's algorithm holds a small segment while earlier data is unacknowledged; the peer's delayed ACK typically waits 40 ms before acknowledging (Linux's delayed-ACK floor; the timer can stretch to 200 ms). Together they produce the classic 40 ms stall on request-response protocols that write a header and a body separately. Fixes: write in one call (or use writev), or set TCP_NODELAY, which most RPC libraries now do by default.

Keepalives are not a fast failure detector: Linux defaults to 7200 s idle, then 9 probes 75 s apart, so a dead peer can go unnoticed for over two hours unless the application sets its own intervals or has its own heartbeat. Head-of-line blocking: TCP delivers bytes in order, so one lost segment stalls everything behind it on that connection: HTTP/2 multiplexes streams over one TCP connection and therefore shares this stall, which is exactly why HTTP/3 moved to QUIC over UDP with per-stream delivery. ss -ti shows the live internals per socket: rtt, cwnd, ssthresh, retrans, and the send queue, which is how you tell "loss" from "the application is slow" from "the window is too small".

Myths and facts

Myth: TIME_WAIT sockets are leaked resources to be eliminated.

Fact: They are normal for the active closer; exhaustion is a connection-rate problem solved by pooling.

Myth: Lots of CLOSE_WAIT means the network is broken.

Fact: CLOSE_WAIT accumulates when your application never calls close(); it is an application bug.

Myth: TCP keepalive will detect a dead peer quickly.

Fact: Defaults take over two hours; use application heartbeats or tune the keepalive sysctls per socket.

Myth: A fast link guarantees fast transfers.

Fact: Throughput is bounded by window ÷ RTT; without window scaling and enough buffer, a high-latency path stays slow.

Myth: HTTP/2 removes head-of-line blocking.

Fact: It removes it at the HTTP layer but not at the TCP layer; one lost segment still stalls all streams on that connection. QUIC fixes it.

How pros do it

  • Pool and reuse connections; let clients be the active closer where you can.
  • Set TCP_NODELAY for request-response protocols and write headers and body together.
  • Use application-level heartbeats with your own timeouts instead of relying on TCP keepalive defaults.
  • Read ss -ti (rtt, cwnd, retrans, send-q) before blaming the network; compare a healthy and an unhealthy socket.
  • Watch retransmit and ListenOverflow counters (nstat, sar -n ETCP) as first-class network SLIs.

Questions people ask

Is TIME_WAIT on the server a problem?

Usually not — TIME_WAIT is the side that closed first doing its job, and it only bites when one host closes hundreds of connections a second toward one destination. Each closed 4-tuple is held for 60 s so a late segment cannot land in a new connection on the same ports; at about 470 closes/s to one peer the 28k ephemeral ports run out. Say 'pool connections and let the client close' before any sysctl: tcp_tw_reuse only helps outbound connections, and tcp_tw_recycle no longer exists, for good reasons.

Connection timed out versus connection refused — what is the difference?

Refused means something answered: a RST came back, so the host and the path are fine and nothing is listening on that port (or a firewall rejects rather than drops). Timed out means the SYN got no answer at all — dropped on the path, by a firewall, by a dead host, or by a listener whose accept queue is full — and the client retried on the 1 s, 2 s, 4 s schedule until it gave up. The caveat: from the client alone you cannot tell where the drop is; ss shows SYN-SENT, and the next command belongs on the other host.

How do I tell packet loss from a slow application?

Run ss -ti on the socket and read three numbers. Rising retrans with a shrinking cwnd is loss; a full send queue with a healthy cwnd means the peer or its application is slow; a zero receive window means our own reader is behind. The caveat: one end's view can still be wrong about where the loss is — capture on both ends if the answer matters.

Why is my transfer stuck at a few Mbps on a 10 Gbps link across an ocean?

Because one flow moves at most one window per round trip: 64 KB over 100 ms is about 5 Mbps whatever the link. Check window scaling and the socket buffer sizes (tcp_rmem/tcp_wmem), then loss, because cwnd collapses on every drop; consider BBR for long lossy paths. The caveat: many parallel flows or a nearby cache sidestep the limit, a single flow never does.

What causes a consistent extra 40 ms on small requests?

Nagle plus delayed ACK. The sender holds a small second write until the first is acknowledged, and the receiver's delayed-ACK timer sits on that acknowledgement for about 40 ms, so a header write followed by a body write stalls until the timer fires. Set TCP_NODELAY or write header and body in one call; most RPC frameworks already do, hand-rolled protocols are where it still bites.

Should I switch to BBR?

Test it on the paths where CUBIC underperforms — lossy or long-haul — and expect a real gain there. BBR paces to a model of bottleneck bandwidth and minimum RTT instead of halving on every loss. The caveat: it is fairness-sensitive when mixed with loss-based flows on a shared link, so fleet-wide by default is a decision to argue, not a setting to flip.

Further reading

Drills · 9

Answer in your head, or out loud, before you reveal. Mark each one: "Again" brings it back, "Got it" counts on your desk.

1 Pick the strongest answer

An engineer pings you: "the client for our backend has about 24,000 sockets in TIME_WAIT — that has to be a leak, can we lower a sysctl?" The service opens a fresh TCP connection for every request, reads the response and calls close(), and it runs at about 400 requests/s. What is actually going on, and why that number?

  1. Lowering net.ipv4.tcp_fin_timeout will shorten TIME_WAIT and shrink the count. tcp_fin_timeout governs FIN_WAIT_2 (waiting for the peer's FIN). TIME_WAIT length is compiled in at 60 s on Linux and has no sysctl.
  2. TIME_WAIT always accumulates on the server because the server owns the listening port. TIME_WAIT is held by whichever side actively closes; the listening socket is irrelevant. Servers accumulate it only when they close first (for example on Connection: close).
  3. They are leaked file descriptors from connections the application forgot to close. A TIME_WAIT socket has already been closed by the application; it holds no file descriptor and no user-space state, only a kernel 4-tuple waiting to expire.
  4. The side that sends the first FIN enters TIME_WAIT for 60 s on Linux to absorb late segments for that 4-tuple; the client closes first, so it holds them, and 400 connections/s × 60 s ≈ 24,000. TIME_WAIT belongs to the active closer, lasts a fixed 60 s (TCP_TIMEWAIT_LEN) on Linux, and the steady-state count is simply close rate × 60.
In short

The client closes first, so it holds every closed connection in TIME_WAIT for 60 seconds.

Why

After the active closer sends its final ACK, TCP keeps the 4-tuple in TIME_WAIT for 2×MSL — a fixed 60 s on Linux — so a delayed segment from the old connection cannot be mistaken for part of a new one on the same tuple. Each TIME_WAIT on the client pins one ephemeral port toward that destination, so at 400 connections/s you carry about 400×60 of them. The default ephemeral range (32768–60999) is 28,232 ports, which caps a client near 470 new connections/s to one destination before connect() fails with EADDRNOTAVAIL. The engineering fix is to stop opening a connection per request (keepalive or a pool), not to fight the kernel with sysctls.

The common mistake

Treating TIME_WAIT as a leak and answering with a kernel setting. tcp_fin_timeout does not touch TIME_WAIT, tcp_tw_recycle no longer exists, and the interviewer knows both; the answer they want is "stop opening a connection per request".

Read the docs

2 Read the output

Timed out, not refused. What has this output told you about where the failure is — and what is your next command, on which host?

# on the application host; requests to the database started timing out 10 minutes ago
$ ss -tan '( dport = :5432 )'
State     Recv-Q  Send-Q  Local Address:Port    Peer Address:Port
ESTAB     0       0       10.0.2.15:41790       10.0.9.20:5432
SYN-SENT  0       1       10.0.2.15:41822       10.0.9.20:5432
SYN-SENT  0       1       10.0.2.15:41826       10.0.9.20:5432
SYN-SENT  0       1       10.0.2.15:41830       10.0.9.20:5432
SYN-SENT  0       1       10.0.2.15:41834       10.0.9.20:5432

$ ss -tan '( dport = :5432 )' | awk 'NR>1 {print $1}' | sort | uniq -c
      1 ESTAB
     47 SYN-SENT

$ nstat -az TcpRetransSegs TcpExtTCPSynRetrans
#kernel
TcpRetransSegs                  1893               0.0
TcpExtTCPSynRetrans             1871               0.0
  1. The database is slow to execute queries; SYN-SENT means the server accepted the connection but has not responded yet. SYN-SENT is the client state before any SYN-ACK arrives; the server has not acknowledged the connection at all. Slow queries would show ESTAB sockets with data in flight.
  2. The client has exhausted its ephemeral ports and cannot open new connections. Port exhaustion fails inside connect() with EADDRNOTAVAIL before a SYN is sent; these sockets have ports allocated and SYNs on the wire.
  3. SYNs leave this host toward 10.0.9.20:5432 and no SYN-ACK returns (Send-Q 1 is the unacknowledged SYN; SynRetrans is climbing). The fault is on the path or at the listener — a firewall or security-group change, or a full accept backlog silently dropping SYNs. Next: on the database host, ss -ltn for :5432 (Recv-Q against Send-Q) and nstat -az TcpExtListenOverflows; if clean, tcpdump for the SYN on the server side. SYN-SENT means the handshake never got past step one. The surviving ESTAB connection proves the route existed; only new connections fail, which points at the listener's backlog or a stateful filter dropping new flows.
  4. DNS resolution for the database hostname is failing, so connections cannot complete. Every socket already carries the resolved peer address 10.0.9.20; DNS finished before connect() was called.
In short

The SYNs leave this host and nothing answers, so check the database host's listen queue and firewall next, not this client.

Why

Read ss as a state machine: SYN-SENT with Send-Q 1 is a client that sent a SYN (one unacknowledged segment) and has heard nothing; TcpExtTCPSynRetrans rising confirms it is retrying on the 1 s, 2 s, 4 s schedule. One ESTAB connection to the same peer shows routing worked until recently, so the fault is specific to new flows: a stateful filter whose rules changed, or a listener whose accept queue is full — Linux drops the SYN outright when the backlog is full (regardless of tcp_abort_on_overflow, which only governs the handshake's final ACK), so from the client it looks identical to a firewall drop. The next move is on the server: ss -ltn (Recv-Q at or above Send-Q means the accept queue is full) and ListenOverflows; a server-side tcpdump port 5432 and 'tcp[tcpflags] & tcp-syn != 0' then settles whether the SYN arrives at all.

The common mistake

Explaining the database's slowness. SYN-SENT means the server never acknowledged the connection at all; a slow server shows ESTAB sockets with data queued, and no amount of query tuning answers a SYN.

3 Answer out loud

Flow control versus congestion control: explain how a TCP sender decides how much data to put on the wire at any moment, and then why a single lost packet can freeze every stream on an HTTP/2 connection. Three to six sentences — I am timing you.

In short

A strong answer says the sender may send only the smaller of the congestion window and the receiver window, and one lost packet stalls every HTTP/2 stream on the connection.

What a strong answer does
  • mechanism: Bytes in flight ≤ min(cwnd, rwnd); slow start doubles cwnd per RTT from ~10 segments; loss (or BBR's model) regulates it; states throughput ≈ window / RTT.
  • hol blocking: TCP hands the application an in-order byte stream, so bytes after the hole wait in the receive buffer until the retransmission arrives; all HTTP/2 streams share that one byte stream, so all freeze; QUIC recovers per stream, which is HTTP/3's motivation.
  • communication: Sender-side rule first, then the loss consequence, then the HTTP/2 corollary; precise terms; no filler.
Why

Interviewers listen for the two windows and the min(), for slow start as a per-RTT doubling rather than a fixed delay, and for the fact that head-of-line blocking is a property of TCP's ordered byte stream rather than a flaw in HTTP/2 framing. Mentioning throughput ≈ window / RTT and QUIC's per-stream recovery are the staff-level tells.

The common mistake

Using window, buffer and bandwidth interchangeably, or presenting head-of-line blocking as something HTTP/2 introduced. HTTP/2 inherited it from TCP; HTTP/3 exists because of it.

Read the docs

4 Pick the strongest answer

One of our RPC clients sends each request as two write() calls — a 4-byte length header, then a small body — and waits for the reply before sending the next request over the same long-lived connection. The latency histogram is bimodal: most requests take ~0.5 ms, a large cluster takes ~40 ms. Walk me through what is happening, and what you would change.

  1. The kernel send buffer fills and the application blocks until it drains. Small sequential requests never approach the send buffer; a full buffer shows as write() blocking and a large Send-Q in ss, not a fixed 40 ms mode.
  2. A segment is being lost and retransmitted; 40 ms is the retransmission timeout. Linux's minimum RTO is 200 ms, and random loss would not hit a large, regular fraction of requests; 40 ms is the delayed-ACK timer.
  3. Nagle's algorithm holds the second small segment until the first is acknowledged, and the peer's delayed-ACK timer holds that ACK for about 40 ms (Linux's delayed-ACK floor; the timer can stretch to 200 ms); set TCP_NODELAY, or coalesce header and body into one write. The write-write-read pattern with Nagle on the sender and delayed ACK on the receiver is the textbook 40 ms stall; disabling Nagle or sending one segment removes it.
  4. Slow start restarts on every request; 40 ms is the time to reopen the congestion window. Slow start grows cwnd per RTT and cannot insert a fixed 40 ms stall on a 0.5 ms path; a small request fits inside the initial window anyway.
In short

Nagle waits for the ACK of the first write and the peer delays that ACK by 40 ms; set TCP_NODELAY or send header and body in one write.

Why

Nagle's algorithm (RFC 896) says: while there is unacknowledged data in flight, buffer small writes until an ACK returns. The header goes out immediately; the body is then held because the header is unacknowledged. The peer has received one small segment, so its delayed-ACK logic waits for a second segment or its timer (typically 40 ms on Linux) before acknowledging. Sender waits for receiver, receiver waits for sender: a 40 ms stall per request. TCP_NODELAY disables Nagle; alternatively write header and body with one writev() so a single segment carries the whole request. Every serious RPC framework sets TCP_NODELAY for exactly this reason.

Read the docs

5 Find what's broken

At ~1,000 requests/s this proxy logs connect() to 10.0.1.10:8080 failed (99: Cannot assign requested address) while connecting to upstream, and ss -tan state time-wait | wc -l on the proxy shows about 55,000. A teammate wants to widen ip_local_port_range. Which lines are actually causing it, and what do you change?

upstream api_backend {
    server 10.0.1.10:8080;
    server 10.0.1.11:8080;
    keepalive 64;
}

server {
    listen 80;
    server_name api.internal;

    location / {
        proxy_pass http://api_backend;
        proxy_http_version 1.0;
        proxy_set_header Connection "close";
        proxy_set_header Host $host;
        proxy_connect_timeout 5s;
        proxy_read_timeout 30s;
    }
}
In short

Lines 13 and 14 turn off upstream keepalive; use HTTP/1.1 and clear the Connection header so nginx reuses connections.

What's broken
  • Line 13: proxy_http_version 1.0 — HTTP/1.0 has no persistent connections, so nginx opens a new TCP connection to the upstream for every request and the keepalive 64 pool is never used.
  • Line 14: proxy_set_header Connection "close" forces the upstream connection closed after each response, defeating keepalive even under HTTP/1.1.
Why

nginx's upstream keepalive directive only works when the proxied protocol allows persistent connections (proxy_http_version 1.1) and the Connection header passed upstream is cleared (proxy_set_header Connection ""); either line 13 or line 14 alone disables it. Without reuse, nginx is the active closer of ~500 connections/s toward each upstream, each holding a TIME_WAIT for 60 s, so ~30,000 ephemeral ports per destination are pinned against a range of 28,232 — connect() fails with EADDRNOTAVAIL, which nginx logs as errno 99. keepalive 64 (line 4) is a per-worker idle-connection cap and is fine; the timeouts (lines 16–17) are reasonable. net.ipv4.tcp_tw_reuse is a band-aid that only helps outgoing connections with timestamps enabled; the real fix is reuse.

Read the docs

6 Scenario: what do you do next?

An internal orders API (JVM, 8 pods behind an L4 balancer; clients open a new connection per request) has a latency SLO of 300 ms at p99. Since this morning's traffic peak, p99 is 1.1 s. The latency histogram shows a sharp spike at ~1.0 s and a smaller one at ~3.0 s; p50 is unchanged at 12 ms. Pod CPU is 45%. No deploy today.

Step 1. You have five minutes before the incident channel expects a hypothesis. What do you check first?

  1. Raise the clients' timeout from 2 s to 5 s so fewer requests fail while you investigate. That hides the symptom and makes users wait longer; it adds no information about what is dropping connections.
  2. Start a one-hour tcpdump on a client host and wait for the next spike. A capture is a valid later step, but the kernel already keeps the counters that answer this; start with the cheap, immediate evidence.
  3. Restart all eight pods — a latency spike with unchanged CPU is usually a leak. No evidence points at a leak, restarting destroys the state you need, and a full accept queue refills within seconds of a restart.
  4. On an orders pod: ss -ltn for the listening port (Recv-Q against Send-Q) and nstat -az TcpExtListenOverflows TcpExtListenDrops — offsets of exactly +1 s and +3 s are the SYN retransmission schedule (1 s, then +2 s), so test the hypothesis that new connections' SYNs are dropped at the listener. Reading the shape of the data first: +1 s and +3 s with an unchanged median is the signature of dropped SYNs retried by the client's timer, and two counters on the server confirm or kill the hypothesis in seconds.

Step 2. On every pod, ss -ltn shows LISTEN 129 128 *:8080 and TcpExtListenOverflows grows by ~2,000/min during peaks. The JVM's accept loop pauses 200–400 ms during GC. What do you do now?

  1. Set net.ipv4.tcp_abort_on_overflow = 1 so clients get an immediate RST instead of waiting. It does not even apply here: a SYN arriving at a full accept queue is dropped regardless, and the sysctl only decides whether the handshake's final ACK is dropped or answered with a RST. Where it does apply it converts latency into hard connection errors; it is a debugging aid, not a mitigation.
  2. Mitigate now: scale the deployment out (more accept loops share the SYN rate) and raise the listen backlog (net.core.somaxconn plus the server's configured backlog) so bursts queue instead of dropping; investigate why accept() stalls afterwards. Both actions cut user impact within minutes and are reversible; the GC/accept-loop root cause is real but belongs to the postmortem.
  3. Increase net.ipv4.tcp_syn_retries on the clients so their SYNs eventually get through. More retries means clients wait longer (1 + 2 + 4 + … s) before failing; requests already succeed on retry — slowly. It lengthens the tail.
  4. Raise net.ipv4.tcp_max_syn_backlog — the SYN queue is overflowing. The evidence names the accept queue (ListenOverflows; Recv-Q 129 against Send-Q 128), not the SYN queue; tcp_max_syn_backlog sizes the wrong queue.

Step 3. p99 is back to 180 ms. Each pod sees ~600 new connections/s at peak because clients open a connection per request. For the postmortem, which action item do you propose?

  1. Move the API to UDP to avoid handshakes. Throws away the reliability and ordering the API depends on to dodge a queue-sizing problem.
  2. Make clients reuse connections (HTTP keepalive or a pool), which removes most SYNs; size the backlog to the burst (e.g. 1024) and alert on ListenOverflows > 0 as a saturation signal; verify by replaying peak traffic and confirming the 1 s mode is gone. Removes the demand that overflowed the queue, keeps a guardrail alert, and says how success will be proven — mechanisms, not intentions.
  3. Ask the client teams to be careful about opening too many connections. Unenforceable and unmeasurable; postmortem actions are mechanisms with owners, not reminders.
  4. Increase the clients' connect timeout to 10 s so the retransmitted SYN always succeeds. Accepts a multi-second tail as normal; the SLO is 300 ms.
In short

Check the pods' listen queue and overflow counter first: +1 s and +3 s are the times a dropped SYN is sent again.

Why

Latency modes at exactly +1 s and +3 s with a normal median are TCP telling you that SYNs were dropped: the client's SYN retransmission timer starts at 1 s and doubles. Linux silently drops a SYN when the listener's accept queue (the backlog, shown as Send-Q on a LISTEN socket) is full — tcp_abort_on_overflow does not change that; it only decides whether the handshake's final ACK is dropped or answered with a RST when the queue is full. ss -ltn plus ListenOverflows confirm it in one command. The interview shape: hypothesize from the numbers, confirm with cheap kernel counters, mitigate reversibly (scale out, larger queue), then fix the demand (connection reuse) and add a guardrail alert.

Read the docs

7 Pick the strongest answer

Your validator fetches full blocks (~1.5 MB) from peers over fresh TCP connections. From a peer in the same region (RTT 2 ms) a block arrives in ~15 ms; from a peer across an ocean (RTT 160 ms) it takes ~1.2 s and the validator starts missing its slot. Both paths are 10 Gbps with zero loss. Why is the far peer 80× slower on the same link speed — and what would you change?

  1. Only packet loss slows TCP down; with zero loss the transfer must run at line rate, so the loss counters are wrong. With zero loss, slow start still gates window growth to one doubling per RTT; the cost is round trips, not drops.
  2. Switching block propagation to UDP removes the delay because there is no handshake. Dropping the handshake saves one RTT of about eight; the propagation delay per round trip remains, and any reliable transport (QUIC included) still ramps its window per RTT.
  3. 10 Gbps moves 1.5 MB in ~1.2 ms, so the remote datacenter must have a slow uplink or a congested transit provider. Link rate is only an upper bound; a single flow moves at most one window per RTT, and the window starts tiny.
  4. A fresh connection's throughput is bounded by cwnd / RTT, and slow start grows cwnd from ~10 segments (~14.6 KB) by doubling once per RTT — 1.5 MB needs about 7 round trips, so 7 × 160 ms ≈ 1.1 s regardless of link speed. Keep peer connections long-lived and warm (tcp_slow_start_after_idle=0) and prefer low-RTT peers on the slot-critical path. Latency is round trips × RTT: slow start's per-RTT doubling makes a cold 1.5 MB transfer cost ~7 RTTs, and until the window is large the 10 Gbps link is irrelevant.
In short

A new connection starts with a small window and doubles it once per round trip, so 1.5 MB needs about seven round trips at 160 ms each.

Why

The bandwidth-delay product says a path can hold bandwidth × RTT bytes in flight — 10 Gbps × 160 ms is 200 MB — but a new TCP connection may have only cwnd bytes outstanding, starting at 10 segments (RFC 6928). Slow start doubles cwnd each RTT: 14.6 KB → 29 → 58 → 117 → 234 → 468 → 936 KB, so 1.5 MB cumulative completes on roughly the 7th round trip: ≈ 1.1 s at 160 ms against ≈ 14 ms at 2 ms, plus one RTT of handshake. The same arithmetic governs any chatty protocol: cost = round trips × RTT. Mitigations: reuse connections so cwnd stays open (and disable tcp_slow_start_after_idle, which otherwise resets cwnd after one RTO of idleness), raise the initial window, use BBR, or — the operational answer — choose peers by RTT for the slot-critical path.

Read the docs

8 Quick recall

Quick one: a request/response protocol shows a fixed 40 ms stall between two small writes. Which socket option do you set?

In short

Set the TCP_NODELAY socket option to turn off Nagle's algorithm.

Accepted

TCP_NODELAY · tcp_nodelay · Tcp_Nodelay

Why

TCP_NODELAY disables Nagle's algorithm, so the kernel sends small segments immediately instead of holding them until in-flight data is acknowledged; RPC frameworks set it to avoid the Nagle/delayed-ACK 40 ms stall. The alternative is to write header and body in one call so there is only one segment to send.

9 Pick the strongest answer

Two services in the same rack talk over TCP with a measured RTT of 0.3 ms. A packet capture shows that the last segment of a three-segment response was lost once and retransmitted — but only after ~200 ms, not after a few round trips. Why did TCP wait that long?

  1. TCP retransmits as soon as one ACK is missing, so the 200 ms must have been the receiver's fault. The sender cannot know an ACK is missing without either duplicate ACKs (which need later segments) or a timer expiry; when the last segment is lost only the timer exists.
  2. Fast retransmit needs duplicate ACKs, which need later segments to arrive; a tail loss produces none, so the sender falls back to the RTO timer — and Linux floors RTO at 200 ms (TCP_RTO_MIN) even when SRTT + 4·RTTVAR is far smaller. This is the tail-loss problem: with nothing behind the hole there are no duplicate ACKs or SACK blocks, so recovery waits for the timer, whose Linux minimum is 200 ms regardless of how small the RTT is.
  3. RTO is set equal to the measured RTT, so the RTT must have spiked to 200 ms. RTO = SRTT + 4·RTTVAR (RFC 6298), clamped to a minimum of 200 ms on Linux (1 s in the RFC). A 0.3 ms RTT still yields a 200 ms RTO.
  4. The receiver's delayed-ACK timer (40 ms) held the ACK, which is why the retransmission came late. Delayed ACK delays acknowledgement of data that arrived; it does not affect when the sender retransmits data that was lost — and 40 ms is not 200 ms.
In short

A lost last segment gets no duplicate ACKs, so the sender waits for the timer, and Linux never sets that timer below 200 ms.

Why

Loss recovery has two paths: fast retransmit, triggered by three duplicate ACKs (or SACK information) generated by segments that arrived after the hole, and the retransmission timeout. When the lost segment is the last one in flight, nothing behind it can generate duplicate ACKs, so only the RTO can fire. RFC 6298 derives RTO from SRTT and RTTVAR, but Linux clamps it to at least 200 ms (TCP_RTO_MIN), so a sub-millisecond datacenter RTT still pays a 200 ms penalty on tail loss. Tail Loss Probe (RACK-TLP) helps when several segments are in flight; with a single outstanding segment its probe timer deliberately adds the same 200 ms to allow for a delayed ACK. This is why short RPCs show a suspicious p99.9 mode near 200 ms, and why some operators lower rto_min per route (ip route ... rto_min 5ms) inside the datacenter.

Read the docs

Next lesson TLS: handshake, chains and trust, SNI, rotation, mTLS, and reading openssl s_client
Drill · TCP
0:00

Loading…