TCP: How Two Programs Get a Reliable Stream of Bytes
TCP gives two programs ordered, reliable delivery, but its setup and its waiting rules cost time that system designers must plan for.
In short
TCP gives two programs a reliable, ordered stream of bytes, but its setup and waiting rules cost time that system designers must plan for.
12 min readFree
What You Will Learn
Most programs that talk over a network need a dependable way to send data. A message that arrives with pieces missing, or in the wrong order, can break the program. TCP, short for Transmission Control Protocol, is the part of the network stack that solves this problem. It works at the transport layer, the layer that moves data between two programs.
In this lesson you will learn three things. First, how TCP opens and closes a connection. Second, how it makes data reliable and in order. Third, how it slows down so it does not flood the receiver or the network. You will also see why these guarantees cost time, and how system designers reduce that cost. Read the key words first, because the rest of the lesson uses them.
Key words
- Packet
- A small chunk of data sent across a network. Each packet carries addresses and a piece of the data.
- Byte stream
- A long flow of bytes with no built-in message edges. The receiver reads the bytes in order.
- Connection
- An agreed session between two programs. Both sides prepare before data moves.
- Round trip
- The time for a message to reach the other side and for the reply to come back. Often written RTT.
- Acknowledgement (ACK)
- A reply that says which data has arrived safely.
- Sequence number
- A counter that gives each byte a position, so the receiver can put data back in order.
- Retransmission
- Sending the same data again because no acknowledgement came back in time.
- Window
- The amount of data a sender may have out on the network before it gets an acknowledgement.
- Head-of-line blocking
- A delay where one lost piece holds up every piece that was sent after it.
- Keep-alive
- Keeping a connection open after one request, so the next request can use it.
- Connection pool
- A set of open connections that a program keeps ready and lends out for new requests.
What Is the Connection Lifecycle?
TCP is connection-oriented. That means the two sides must agree to talk before any real data moves. The side that starts the connection is the client. The side that waits for connections is the server. The server must already be listening, or nobody answers the request.
The agreement is called the three-way handshake. It has three messages. Each message has a name taken from its flags.
- 1
SYN
The client sends a SYN packet. SYN stands for synchronize. The packet proposes a starting sequence number for the client's bytes.
- 2
SYN-ACK
The server replies with a SYN-ACK. It gives its own starting sequence number and confirms the client's number.
- 3
ACK
The client sends a plain ACK that confirms the server's number. Now the connection is open, and data can flow in both directions.
Closing works like a second, shorter agreement. Each side says it has no more data to send, using a FIN packet, and the other side confirms it. Once both sides have closed, the connection is gone. Closing is not usually where designers feel the cost, though. The setup is.
The handshake is useful because it lets both sides agree on starting numbers. Those numbers synchronize the two sides, and their random starting values guard against spoofing.
Tip
When you read about TCP, keep two ideas apart. Setup is the cost you pay once per connection. Transfer is the cost you pay for each byte. Good designs try to pay setup less often.
Why Does Setup Cost Time?
The handshake takes time. The client sends SYN and waits one full round trip for the SYN-ACK. Then it sends the ACK. Only after that can the program send its first real request. So a brand-new connection adds about one round trip before any useful data is sent.
Imagine a round trip of 50 milliseconds, which is a plausible example for two servers in different cities. A new connection adds about 50 milliseconds before the request even leaves. That delay happens every time you open a new connection. It does not matter how fast the servers are, because the time is spent travelling.
Setup also has a second cost. A new connection starts with a cautious sending pace, so early data moves slowly. You will read more about this in the congestion section below.
Why Reuse Connections?
The cure for setup cost is to avoid making new connections. A keep-alive connection stays open after a reply. The next request uses the same connection, so it skips the handshake.
Early web browsers opened a fresh connection for each file they needed. Because each new connection began cautiously, those browsers responded slowly. Modern browsers now open a few connections at once, or reuse one connection for many files from the same server.
Tip
Reuse fixes setup cost, but it does not fix slow networks. A reused connection still waits for the network to carry each packet. Check both the setup time and the transfer time before you decide what to change.
How Does TCP Make Delivery Reliable?
The network underneath TCP can lose packets, duplicate them, or deliver them out of order. TCP hides these problems from the program. The program sees one stream of bytes that arrives in the right order. Three tools make this possible.
Sequence numbers
Every byte has a position number. The receiver uses these numbers to put pieces back in their original order, even when they arrive late.
Acknowledgements
The receiver tells the sender how far its data has arrived in order. A missing acknowledgement tells the sender that something may be lost.
Retransmission
If an acknowledgement does not arrive within a set time, the sender sends the missing data again. The timer is based on the measured round trip.
Corruption is also handled. Each segment carries a checksum, a small code that the receiver checks.
What Is Head-of-line Blocking?
Ordering has a cost. Suppose the sender transmits pieces 1 to 5, and piece 3 is lost. Pieces 4 and 5 may reach the receiver first. But TCP will not hand them to the program until piece 3 has been resent and received. The later pieces wait in a queue behind the missing one.
This delay is called head-of-line blocking. The first item in line blocks everything behind it. The wait grows with the loss rate and with the round trip time. A network with little loss feels smooth. A lossy network can feel stuck, even when most of the data is fine.
This matters for system design. If several independent messages share one TCP connection, one lost packet can delay all of them. Some newer protocols avoid this by not using a single ordered stream for everything. That is a topic for other lessons, so for now just remember the trade-off.
Flow Control and Congestion Control
Reliability is not the only job. TCP must also avoid sending faster than the receiver or the network can handle. It uses two separate limits to do this. They are often mixed up, so keep them apart.
Flow control protects the receiver. The receiver has a buffer, which is a block of memory for incoming bytes. Each acknowledgement tells the sender how much room is left in that buffer. This value is the receive window. If the receiver's program reads slowly, the window shrinks. When it reaches zero, the sender must pause.
Congestion control protects the network. Routers in the middle have queues with limited space. If too much data arrives, the queues fill up and packets are dropped. TCP keeps a second limit, the congestion window, which it estimates from how the network behaves. The sender may only have the smaller of the two windows in flight.
Compare the two: flow control asks, can the receiver take more? Congestion control asks, can the path take more? A sender must satisfy both.
Slow Start and Backing Off
TCP learns the network's limit by trying. It begins cautiously, then pushes harder until it sees signs of loss. Then it eases off. The main rules follow a pattern. The growth is fast at first, then gentle, and a loss causes a sharp cut.
- 1
Slow start
A new connection starts with a small congestion window. The window grows quickly, roughly doubling with each round trip, until a loss appears or a set threshold is reached.
- 2
Congestion avoidance
After the threshold, growth slows. The window grows by about one segment per round trip, which probes for spare capacity without rushing.
- 3
Backing off on loss
When a loss is seen, TCP treats it as a sign of congestion. It cuts the window, often to about half, and then grows again. This pattern is often described as slow growth and fast cuts.
The result is that a new connection is slow for its first moments. A large transfer speeds up later. A short transfer may never leave the cautious phase. That is why short requests over new connections often feel slow, even on fast links.
Worked Example: Opening a Web Page
Here is what happens when you open a web page in your browser. The browser has already turned the site name into a server address, which the Domain Name System lesson covers. The steps below focus on TCP. The page uses HTTP, a protocol that runs on top of TCP. HTTP sends its requests only after TCP is ready.
- 1
Handshake first
The browser sends a SYN to the server, the server replies with a SYN-ACK, and the browser sends the final ACK. No HTTP request has been sent yet. The browser waits one round trip for this.
- 2
First request
Once the connection is open, the browser sends its HTTP request for the page. The server sends back the response as a stream of bytes. TCP checks each piece, puts the bytes in order, and resends anything lost.
- 3
Later requests
The page needs more files, such as images and scripts. The browser sends these requests over the same open connection. Because the connection is kept alive, these requests skip the handshake.
- 4
Idle time
If the connection sits unused for too long, one side may close it. The next visit then pays the handshake cost again.
The example shows the main idea of this lesson. The first request pays for setup. Later requests reuse the same connection and save that wait. The page feels faster because the browser keeps its connection warm.
What This Means for System Design
Setup latency
A new connection for each call adds at least one round trip before the first request. Between two busy services, that wait can dominate the whole call.
Slow starts
Short bursts over a fresh connection move slowly, because the sending window has not grown yet. Long transfers reach full speed later.
Stalls from loss
On a lossy network, one lost packet can hold up the data behind it. Latency then rises as the loss rate and the round trip time grow.
A simple model helps you see the effect of reuse. Assume one round trip costs 50 milliseconds, and a request costs one round trip once the connection is ready. Also assume the model ignores data size and slow start, so it shows only the setup effect. Compare four requests made two ways.
rtt_ms = 50
requests = 4
# New connection for every request: handshake (1 RTT) + request (1 RTT)
new_each = requests * (1 + 1) * rtt_ms
# One reused connection: handshake once, then 1 RTT per request
reused = (1 + requests) * rtt_ms
print("new connection each time:", new_each, "ms")
print("reused connection:", reused, "ms")
Output
new connection each time: 400 ms reused connection: 250 ms
The model is simple, but it shows the point. The gap grows with the number of requests. In a real system, the gap also depends on the server's load, the network path, and how long connections stay open.
Tip
Before you change code, count how many new connections your service opens per second. If many of them carry tiny requests, pooling or keep-alive is usually the first fix to try.
Check Yourself
Quick check
A checkout service opens a brand-new TCP connection to a pricing service for every tiny lookup. Both servers are idle, yet each lookup feels slow. What is the likely cause, and what should you change?
Quick check
A service sends a large file over a brand-new connection. The first second is slow, and the speed rises later. The network is not busy. Which TCP idea best explains this?
Quick check
A consumer app pauses for several seconds and stops reading from its connection. The producer's sending then stops too, even though the network is healthy. Which mechanism is at work?
Sources
- Transmission Control Protocol - Wikipedia
- TCP congestion control - Wikipedia
- Head-of-line blocking - Wikipedia
- RFC 9293: Transmission Control Protocol (TCP)
- RFC 5681: TCP Congestion Control
- TCP: 3-Way Handshake, Segments, and Flow (justprotocols)
- TCP Three-Way Handshake and Connection Lifecycle (StatusCodeFYI)
- How TCP Works: Three-Way Handshake, Flow Control, Congestion Avoidance (Let's Build)
Last updated on 17 November 2026
Related lessons
- Machine Learning FoundationsData loading: from a file to your program
- System designBack Pressure: How an Overloaded System Tells Callers to Slow Down
- System designWrite-Through: Save to the Cache and Database Together Before Success
- System designSQL Tuning: Making Database Queries Faster by Reading Their Plans
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.