KatInterview

How TCP Opens a Connection: SYN, SYN-ACK, ACK and Lost Packets

System DesignModel answer
MediumModel answerSystem Designtcpnetworkinghandshake

Last updated

Describe how TCP establishes a connection, including state changes, sequence numbers, a lost final ACK, and the limits of what a completed handshake proves.

Almost every RPC call and client-server exchange in a backend system runs over TCP, so you should know exactly how a connection gets started and where that process can go wrong.

Walk through how TCP sets up a connection with its three-message exchange. Your answer should cover three areas.

1. The exchange and the numbers

  • Name each message the client and server send, in order.
  • Show how the socket state on each side changes at every step, from CLOSED or LISTEN through to ESTABLISHED, including SYN_SENT and SYN_RCVD.
  • Explain how each side picks its starting sequence number, why that number should not simply be 0, and how the sequence and acknowledgment values change from one message to the next.

2. The third message goes missing

  • Suppose the client's final ACK never reaches the server. What does each side believe immediately afterward?
  • How does the situation get resolved if the client starts sending application data right away?
  • How does it get resolved (or not) if the client has nothing to send and the connection sits idle?

3. What a completed handshake does and does not tell you

  • What can each side safely conclude once it reaches ESTABLISHED?
  • What can it not conclude about the application above it, about firewalls or NAT devices in the path, or about whether the peer is still alive later on?

Aim for an answer you could give out loud in a few minutes, with concrete values and the reasoning behind them.

Hints

Hint 1

Each side must announce its own starting sequence number and have it confirmed. That is four logical steps, but the server can merge two of them into one segment.

Hint 2

A SYN occupies one position in the sequence space even though it carries no data, so the acknowledgment value is the peer's number plus one.

Hint 3

After the lost ACK, the two ends disagree: one thinks the connection is open and the other does not. Ask which side has a timer running, and what any later segment from the client carries in its ACK field.

Hint 4

Separate what the kernel did from what the application did. Completing the handshake says nothing about `accept()` having been called, about NAT table entries surviving, or about the peer staying alive.