Notes
Three-way handshake
Before any data, TCP does a three way handshake. The client sends a SYN with its starting sequence number and options: the maximum segment size, and a window scale. The server answers SYN ACK with its own number, acknowledging the client's. The client acknowledges back. Our capture showed exactly these three packets. The cost: one full round trip before the first byte of data, which across continents is a hundred milliseconds or more.
C > S Flags [S] seq x mss 65495, wscale 10 S > C Flags [S.] seq y, ack x+1 C > S Flags [.] ack y+1 S = SYN, . = ACK one round trip before any data can flow
Sequence numbers
Every byte gets a sequence number. Our client sent fourteen bytes, numbered one to fourteen. The server's reply acknowledged fifteen, meaning I have everything up to fourteen, send fifteen next, and carried its own twenty bytes of answer. These numbers let the receiver put segments back in order, drop duplicates, and tell the sender exactly what arrived.
C > S [P.] seq 1:15, length 14 "hello shoprung" S > C [P.] seq 1:21, ack 15, length 20 ack 15 = "I have bytes up to 14, send 15 next" P = push to the app now
Retransmits
When a segment is lost, its acknowledgement never comes. The sender keeps a timer and retransmits when it expires, waiting longer each time. If later segments arrive, the receiver repeats its last acknowledgement, and three duplicates trigger a fast retransmit. Retransmits are invisible to the application, but they show up as latency spikes. nstat and ss minus t i show retransmit counts, a key signal of a lossy or congested path.
- sender keeps a timer per unacknowledged data
- timeout → retransmit, with growing waits (backoff)
- duplicate ACKs → fast retransmit
- retransmits = latency spikes
- $ nstat -az | grep -i retrans
- $ ss -ti shows retrans count
Flow and congestion
TCP has two brakes. Flow control: the receiver advertises a window, how many bytes it can accept, so a fast sender cannot flood a slow reader. Congestion control: the sender estimates what the network can carry. It starts small, doubling every round trip, slow start, and backs off when it sees loss. Linux uses CUBIC by default, and BBR is common too. The lesson: a brand new connection starts slowly, another reason to reuse connections.
- flow control: receiver advertises a window ("I have room for N bytes")
- congestion control: sender guesses network capacity
- slow start: begin small, double each round trip
- loss → back off (CUBIC, BBR)
- new connections start slow
Closing
Closing takes a FIN from each side, each acknowledged. In our capture the server closed first after replying. The side that closes first enters TIME WAIT for about a minute on Linux, so stray old packets cannot confuse a new connection on the same ports. After two hundred short connections, ss showed two hundred and twenty sockets in TIME WAIT. That is normal, but at high rates without connection reuse it eats ports.
S > C Flags [F.] server done C > S Flags [F.] client done + ACKs the side that closes first waits in TIME-WAIT after 200 short connections: 220 sockets in TIME-WAIT (lasts about 60 s on Linux)
Socket states
ss shows every connection's state. LISTEN is a server waiting. SYN SENT is a client still waiting for an answer, a sign the far side is not responding. ESTAB means connected. CLOSE WAIT means the other side closed but your application never closed its end, and piles of them usually mean a bug leaking connections. TIME WAIT is the closer waiting out the minute. ss minus s gives totals.
- LISTEN waiting for SYNs
- SYN-SENT client waiting
- ESTAB data flowing
- CLOSE-WAIT peer closed, app has not: often an app bug
- TIME-WAIT closer waiting
- $ ss -tan state time-wait
- $ ss -s totals
Refused vs timeout
Two failures look alike but mean different things. Connecting to a port with no listener failed in two and a half milliseconds with connection refused: the machine answered with a reset. Connecting to an address that never answered timed out at our three second limit. Refused says the host is there but the app is not. Timeout says packets vanished: a firewall, a wrong route or a dead host. Always choose explicit, short connect timeouts.
port with no listener: ConnectionRefused 2.46 ms (server sent RST) unreachable 192.0.2.99: timeout at 3,003 ms (nothing came back) refused: wrong port or app down; timeout: firewall, route, dead host
Reuse connections
Put it together. Every new connection costs a handshake round trip, a slow start, a TLS handshake on top, and a TIME WAIT socket afterwards. That is why HTTP keep alive, database connection pools and long lived gRPC channels exist: they pay those costs once. Episode ten measures exactly how much a reused HTTPS connection saves.
- each new connection: a round trip for SYN, slow start, TLS on top, TIME-WAIT after HTTP keep-alive, DB pools, gRPC channels reuse one episode 10 measures it: HTTPS new connection vs reused
Common mistakes
Mistakes. No connect or read timeouts, so a client hangs forever. A new connection for every request. Fighting TIME WAIT with unsafe kernel tweaks instead of reusing connections. Ignoring a growing pile of CLOSE WAIT sockets. Blaming the application for latency that is really retransmits. And assuming a successful ping means the port works.
- no connect/read timeouts
- new connection per request
- "fixing" TIME-WAIT with unsafe kernel tweaks
- ignoring CLOSE-WAIT leaks
- blaming the app for retransmit latency
- ping OK = port OK
Say it like this
In the interview: SYN, SYN ACK, ACK exchanges initial sequence numbers and options, so data starts after one round trip. Every byte is numbered and acknowledged, lost segments are retransmitted, and flow and congestion control set the pace. FIN from each side closes it, and the side that closed first waits in TIME WAIT. In practice that means reusing connections, setting timeouts and watching retransmits.
- "SYN, SYN-ACK, ACK sets both sequence numbers, so data starts
- after one round trip. Bytes are acknowledged and retransmitted
- if lost, with flow and congestion
- control. FIN closes; the closer
- sits in TIME-WAIT. So I reuse
- connections and set timeouts."