Opscraft
The TCP handshake · Lesson 8 of 8 · 4 min read

Interview questions and model answers

"Explain the TCP three-way handshake."

The client sends a SYN with a random initial sequence number and options like MSS, window scaling and SACK. The server replies SYN-ACK, acknowledging the client's ISN plus one and sending its own ISN and options. The client ACKs the server's ISN plus one, and both are ESTABLISHED. It's three steps because each side has to send its ISN and get confirmation the other received it; random ISNs prevent spoofing and confusion with old connections.

"What's the difference between FIN and RST?"

FIN is a graceful close of one direction after all data has been sent, acknowledged, and followed by the peer's own FIN — it leads to TIME_WAIT on the side that closed first. RST aborts the connection immediately, discarding data; it's what you get when connecting to a port with no listener, when a middlebox kills an idle flow, or when an app aborts. The client sees "connection refused" or "connection reset by peer".

"Your server has thousands of sockets in CLOSE_WAIT. What does it mean?"

The remote side has closed those connections but our application hasn't called close() — it's a socket leak in the app, often in an error path or a pool that doesn't evict dead connections. It will eventually exhaust file descriptors. I'd identify the process with ss -tanp state close-wait, look at the code path or thread dumps, fix the leak, and restart as a short-term mitigation.

"Lots of TIME_WAIT on a client. Is that bad?"

TIME_WAIT is normal for the side that closes first; it protects against delayed segments. It's a problem only when it exhausts ephemeral ports — many short-lived connections to the same destination — causing "Cannot assign requested address". The fix is connection reuse: keep-alive and pooling, with tcp_tw_reuse as a supplement.

"Clients get connection timeouts but the server shows no SYNs in tcpdump. Where's the problem?"

Upstream of the server: security groups or NACLs, routing, the load balancer, or DNS resolving to the wrong address. I'd capture on the client to confirm SYNs leave, use VPC Reachability Analyzer and Flow Logs for REJECTs, and check the LB's listener and target registration.

"What is a SYN flood and how do you defend against it?"

An attacker sends many SYNs without completing handshakes, filling the server's SYN queue of half-open connections. Defences are SYN cookies, which avoid storing state until a valid ACK returns, larger backlogs, putting ALB/NLB/CloudFront in front, and AWS Shield, which mitigates common L3/L4 floods automatically.

"Why does TLS add latency, and how does HTTP/3 help?"

TLS's handshake happens after TCP's: TLS 1.2 adds two round trips and TLS 1.3 one, on top of TCP's one. HTTP/3 runs over QUIC on UDP, which combines the transport and TLS 1.3 handshakes into a single round trip — zero on resumption — and also removes TCP head-of-line blocking.

"How does traceroute work?"

It sends packets with increasing IP TTL values. Each router that decrements the TTL to zero drops the packet and returns an ICMP time-exceeded message, revealing that hop and its latency. Using TCP SYN probes on port 443 gets through firewalls that block the default UDP or ICMP probes.