DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · TRANSPORT · 32 OF 187

TCP Explained: Handshake, States, Retransmits

TCP from a real capture: the three-way handshake (SYN, SYN-ACK, ACK) with its MSS and window options, sequence numbers and acknowledgements, retransmits, flow and congestion control, connection teardown with FIN, TIME-WAIT (220 entries after 200 short connections), refused (2.46 ms) versus timed out (3,003 ms), and how keep-alive and connection pools save handshakes.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • SYN, SYN-ACK, ACK
  • seq and ack
  • retransmits
  • window and slow start
  • FIN, TIME-WAIT
  • refused vs timeout

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.

tcpdump (ours, trimmed)
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.

Our data
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.

Reliability
  • 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.

Two brakes
  • 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.

Teardown (ours)
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.

States you will see
  • 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.

Our client
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.

Why pools matter
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1Describe the TCP three-way handshake.

Client sends SYN with its initial sequence number; server replies SYN-ACK with its own and acknowledges; client sends ACK. Data can flow after one round trip.

Q2What is TIME-WAIT and why does it exist?

The state the side that closes first stays in (about 60 s on Linux) so delayed packets from the old connection cannot be mistaken for a new one.

Q3Many sockets in CLOSE-WAIT: what does it mean?

The peer closed the connection but the local application never called close; usually a connection leak in the app.

Q4What is the difference between flow control and congestion control?

Flow control protects the receiver via the advertised window; congestion control protects the network by limiting the sender's rate (slow start, CUBIC, BBR).

Q5Connection refused vs connection timed out?

Refused: the host replied with RST, nothing listens on that port. Timed out: no reply at all, usually a firewall, routing problem or down host.

Q6How do you see TCP retransmits on Linux?

nstat -az | grep -i retrans for totals, ss -ti for per-connection retransmit counts and RTT, or tcpdump to see repeated segments.

Q7Why use connection pooling?

It avoids a TCP handshake, slow start and often a TLS handshake per request, and avoids piles of TIME-WAIT sockets and port exhaustion.