map / networking / tcp

Networking · principle 3 of 6

TCP handshake & congestion

TCP spends one round trip to open a connection and then ramps up its sending window, so new connections are slow by design.

01 · Why it’s true

IP can drop, duplicate, or reorder packets. TCP builds a reliable, ordered byte stream on top: it numbers bytes, the receiver acknowledges what arrived, and the sender retransmits what wasn’t acknowledged.

Before any data flows, the two sides exchange SYN and SYN-ACK to agree on starting sequence numbers — one round trip. And because the sender doesn’t know how much the network can carry, it starts with a small congestion window and grows it as acknowledgements arrive, backing off when it sees loss. New connections are slow by design.

02 · Picture it

Clientopens the connectionServerlistening on a port, e.g. 443Handshake: 1 round trip before any data1SYN — here’s my starting number2SYN-ACK — got it, here’s mine3ACK + first request bytes4a few segments (small window)5ACK — all received6more segments (window grows)requestresponseA new connection costs a round trip and starts slow, which is why reusing connections(keep-alive, pooling) cuts latency.
WHO’S INVOLVEDClientopens the connectionServerlistening on a port, e.g. 443Handshake: 1 round trip before anydata1Client → ServerSYN — here’s my starting number2Server → ClientSYN-ACK — got it, here’s mine3Client → ServerACK + first request bytes4Server → Clienta few segments (small window)5Client → ServerACK — all received6Server → Clientmore segments (window grows)requestresponseA new connection costs a round tripand starts slow, which is why reusingconnections (keep-alive, pooling) cutslatency.
Three-way handshake, then slow start: new connections are slow by design.

03 · In the interview

  • Explaining why connection reuse (keep-alive, pooling) matters for latency.
  • Reading errors: “connection refused” (a reset — nothing listening) vs “connection timed out” (no reply at all).
  • Recognizing packet loss as a latency problem: retransmissions and a shrinking window slow everything down.

04 · Practice question

“Some clients see “connection timed out” to a service, others see “connection refused”. What does each tell you?”

A strong answer covers

  1. Refused: the host answered with a reset — it’s reachable, but nothing is listening on that port (process down, wrong port).
  2. Timed out: no answer at all — a firewall or security group dropping packets, a host that’s down, or a routing problem.
  3. Test a TCP connect from the client side, then check on the server whether the process is listening (ss -ltn).
  4. If it’s intermittent, look for a few bad backends behind a load balancer, or a full listen queue on a busy server.