How TCP Opens a Connection: SYN, SYN-ACK, ACK and Lost Packets
System DesignModel answerLast 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
CLOSEDorLISTENthrough toESTABLISHED, includingSYN_SENTandSYN_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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Why three messages
TCP delivers a numbered byte stream, and each side chooses its own starting number, the Initial Sequence Number (ISN). Setup therefore needs two things in each direction: announce the ISN, and have the peer confirm it. That would be four messages, but the server sends its confirmation and its own announcement together in one segment, giving three.
The exchange
Assume the server has called listen() and the client calls connect().
| Step | Segment | Fields | Client state | Server state |
|---|---|---|---|---|
| 0 | none | none | CLOSED | LISTEN |
| 1 | client to server: SYN | seq = x | SYN_SENT | SYN_RCVD once received |
| 2 | server to client: SYN-ACK | seq = y, ack = x+1 | ESTABLISHED once received | SYN_RCVD |
| 3 | client to server: ACK | seq = x+1, ack = y+1 | ESTABLISHED | ESTABLISHED once received |
Key points on the arithmetic:
- A SYN uses up one sequence number, as if it were a byte of data. That is why the replies carry
x+1andy+1. - The
ackfield means "the next byte I expect from you." The client's first data byte is numberedx+1and the server's isy+1. - The bare ACK in step 3 carries no data, so it does not advance the sequence number.
- Options such as MSS, window scaling, SACK and timestamps are negotiated in the first two segments.
On Linux the server keeps a small entry in a SYN queue after step 1 and moves it to the accept queue after step 3. accept() just pulls from that queue, so the application takes no part in the handshake.
Choosing the ISN
Modern stacks follow RFC 6528: ISN = timer + hash(source IP, source port, destination IP, destination port, secret). The timer makes values advance over time, and the keyed hash makes them hard to predict.
Starting at 0 would be a bad idea for two reasons:
- Stale packets. A delayed segment from an earlier connection on the same address and port pair could land inside the new connection's window and be accepted as valid data. A time-advancing ISN makes that unlikely.
- Spoofing. If an attacker can predict the ISN, they can forge acknowledgments or inject data without seeing the traffic. Classic session hijacks relied on this. An unpredictable 32-bit start forces them to guess.
Lost final ACK
Immediately after the loss:
- The client is in
ESTABLISHED. It received the SYN-ACK,connect()has returned, and it believes the connection is up. - The server is still in
SYN_RCVD. The connection is not in the accept queue, so the application cannot see it.
Recovery: The server has a retransmission timer running for its SYN-ACK, and it resends with exponential backoff. Linux defaults to tcp_synack_retries = 5, with retries at about 1, 2, 4, 8 and 16 seconds, so it gives up after roughly a minute. When the client sees a duplicate SYN-ACK it simply sends the ACK again.
Case A: the client sends data right away. Every segment after the SYN carries the ACK flag with ack = y+1. When the first data segment arrives, the server treats that ACK as completing the handshake, moves to ESTABLISHED, queues the connection for accept() and keeps the data. The lost ACK did no harm.
Case B: the connection is idle. This is typical when the server speaks first (SMTP, MySQL). The retransmitted SYN-ACK triggers a fresh ACK, which normally fixes things within about a second. If every retransmission is also lost, the server drops its half-open entry. The client still believes it is connected, and when it finally sends data the server has no matching connection and replies with RST, which the client sees as a connection reset.
A related trap: if the server's accept queue is full, Linux by default drops the final ACK on purpose. The client looks connected while the application never sees it. From the client's side this is indistinguishable from a lost ACK.
What the handshake proves
When both sides reach ESTABLISHED you know:
- Packets travelled in both directions at that moment.
- Both ISNs are known and confirmed, so the sequence spaces are synchronized.
- Options have been agreed.
- The client's address is probably genuine, since it had to see the SYN-ACK to produce the right
ack.
What it does not prove
- The application is ready. The kernel completes the handshake; the process may be hung, overloaded, or never call
accept(). - Future connectivity. Routes change and links fail.
- Middlebox state lasts. NAT and firewall entries expire when idle, often after minutes, and later traffic is dropped or reset.
- The peer is still alive. A crashed or unplugged host sends no FIN or RST, leaving a half-open connection that an idle side never notices. Linux keepalive defaults (7200 s idle, 75 s interval, 9 probes) are far too slow for RPC systems. Prefer application heartbeats,
TCP_USER_TIMEOUTand request deadlines. - Delivery to the application. A TCP ACK means the peer's kernel has the bytes, not that the program processed them.
- Security. There is no authentication or encryption without TLS.
Costs and edge cases
- The client can send data after 1 RTT; the server sees the first data after 1.5 RTT.
- Each half-open entry occupies server memory until it completes or expires. SYN floods exploit this, and SYN cookies are the standard defense.
- A lost SYN is retried by the client. With Linux's
tcp_syn_retries = 6,connect()fails after about 127 seconds. - A SYN sent to a closed port gets an RST, surfacing as connection refused.
- Simultaneous open: if both sides send SYN at once, both pass through
SYN_SENTandSYN_RCVDand finish with four segments.
Common mistakes
- Saying the ACK number equals the sequence number received, rather than that number plus one.
- Claiming the server is
ESTABLISHEDafter sending the SYN-ACK. - Saying a lost final ACK always breaks the connection, when data from the client usually repairs it.
- Treating a completed handshake as proof that the application or peer is healthy.