Backend server development often uses TCP and UDP without much thought. HTTP APIs and WebSocket run on TCP. Voice/video streaming and DNS use UDP. When workloads with different reliability and performance requirements — chat servers, ad servers — enter the picture, understanding the transport layer becomes necessary.
Transport Layer
The transport layer handles data delivery between applications. While IP finds the route to a host, the transport layer determines which process on that host receives the data. Port numbers serve this purpose.
Both TCP and UDP run on top of IP. The difference is the choice between reliability and speed.
TCP
TCP (Transmission Control Protocol) is a connection-oriented protocol. It establishes a connection before sending data and retransmits on loss.
Connection Establishment
TCP establishes connections through a 3-way handshake.
The client sends a SYN packet along with its sequence number. The server responds with SYN-ACK, providing the server-side sequence number. The client sends ACK to complete the connection. The exchanged sequence numbers serve as reference points for tracking order in subsequent data transfer.
Connection Termination
Connection termination uses a 4-way handshake. TCP operates in full-duplex mode, so each direction must be closed separately.
When one side sends FIN, it signals “no more data to send.” The peer responds with ACK, finishes sending its remaining data, then sends its own FIN. The initiator enters TIME_WAIT after sending the final ACK, allowing time for delayed packets that may still be in the network.
Segmentation
Data sent by the application is split into segments by TCP. A single send() call transmitting 4KB gets divided into multiple segments sized to the MSS (Maximum Segment Size).
The receiving TCP reassembles arriving segments in sequence number order and delivers them to the application. Segmentation and reassembly happen transparently — the application sees only the original continuous byte stream.
Reliability Guarantees
Segments can be lost or arrive out of order as they traverse the network. TCP guarantees data delivery through two mechanisms.
Ordering: Each segment carries a sequence number. The receiver reassembles data in original order using these numbers. Even when segments arrive out of order, the application receives sorted data.
Retransmission: After sending data, the sender waits for an ACK. If no ACK arrives within the RTO, Retransmission Timeout, it resends the data.
ACK numbers use cumulative acknowledgment. “ACK 3” means “everything before 3 has been received; expecting 3 next.”
Normal Flow
When segments arrive in order, the receiver sends an ACK with the next expected number. ACK 2 means “received 1, expecting 2 next.”
Loss Scenario
The receiver cannot advance the ACK number past 3 because Segment 3 is missing, even after Segment 4 arrives. It repeats ACK 3. When the sender sees 3 duplicate ACKs, it retransmits immediately without waiting for a timeout. Once the receiver gets the retransmitted Segment 3, it combines it with the buffered Segment 4 and sends ACK 5.
Flow Control
Exceeding the receiver’s processing capacity causes data loss. TCP prevents this with the sliding window mechanism.
The receiver advertises its available buffer size as the receive window, rwnd. The sender does not send more unacknowledged data than the rwnd allows.
Awaiting ACKs:
Segments 3 and 4 have been sent. Segments 5 and 6 are inside the window but not yet transmitted. Segment 7 onward is outside the window and cannot be sent.
After receiving ACK 3 — window slides right:
When ACK 3 returns, the window shifts one position right. Segment 3 moves out of the window as complete. Segment 7 enters the window. This repeats with each ACK. When the receiver’s buffer fills up, it sends rwnd=0 to pause transmission. When buffer space opens, it advertises a new window size to resume.
The sliding window overcomes the limitations of Stop-and-Wait, where only one packet can be in transit at a time. With sliding windows, multiple packets within the window range transmit continuously while awaiting ACKs.
Two retransmission strategies exist. Go-Back-N retransmits all packets from the lost one onward. Simple to implement but causes unnecessary retransmissions. Selective Repeat retransmits only the lost packets. Requires receiver-side buffering but improves network efficiency. With the SACK (Selective Acknowledgment) option, TCP retransmits only the segments that were actually lost, behaving like Selective Repeat.
Congestion Control
While flow control protects the receiver’s capacity, congestion control protects network path capacity. When the network is congested, router buffers overflow and packets are lost.
TCP manages a variable called the congestion window, cwnd. The actual transmission rate is determined by the smaller of rwnd and cwnd.
Slow Start
At connection start, network capacity is unknown. cwnd begins at the initial window (IW). RFC 5681 caps IW at 2 to 4 segments depending on MSS size; the graph below simplifies this and starts at 1 MSS. cwnd increases by at most 1 MSS for each ACK that acknowledges new data. cwnd doubles each RTT (Round-Trip Time) — exponential growth.
TCP uses slow start while cwnd is below ssthresh (slow start threshold) and congestion avoidance once cwnd exceeds it; when the two are equal, either is allowed. ssthresh starts arbitrarily high and is lowered each time congestion is detected. The graph above assumes ssthresh is 16. It is reached at RTT 4, after which growth becomes linear. At RTT 9, three duplicate ACKs signal packet loss and cwnd drops to half.
Congestion Avoidance
After ssthresh, cwnd increases by only 1 MSS per RTT — linear growth. Transmission volume increases gradually until packet loss is detected. This is the AIMD strategy — Additive Increase, Multiplicative Decrease. It probes available bandwidth gradually without overloading the network.
Fast Retransmit
Two ways to detect packet loss: timeout (RTO expiry) and duplicate ACKs. Timeouts can take hundreds of milliseconds to seconds.
Fast retransmit triggers immediate retransmission when 3 duplicate ACKs arrive, without waiting for timeout. Duplicate ACKs signal that “data after the lost packet is arriving, but something in between is missing.”
Fast Recovery
Returning to slow start after fast retransmit causes a sharp throughput drop. Fast recovery prevents this. On the third duplicate ACK, the sender lowers ssthresh to max(FlightSize / 2, 2 MSS) and retransmits the lost segment. FlightSize is the amount of data sent but not yet acknowledged. Instead of dropping cwnd to 1 MSS, it sets cwnd to ssthresh + 3 MSS, because three duplicate ACKs mean three segments have already left the network and sit in the receiver’s buffer. Each further duplicate ACK adds 1 MSS to cwnd. When an ACK for new data arrives, cwnd is set back to ssthresh and congestion avoidance continues. The net effect is that cwnd resumes linear growth from half the pre-loss amount in flight.
Arriving duplicate ACKs indicate the network is not completely blocked — some packets are getting through. No need to retreat all the way to slow start.
Loss detected by timeout (RTO expiry) is handled differently. ssthresh drops by the same formula, max(FlightSize / 2, 2 MSS), but cwnd falls to 1 MSS (the loss window) regardless of IW, and slow start begins again. Once cwnd reaches the new ssthresh, congestion avoidance takes over. If the same segment times out again after being retransmitted by the timer, ssthresh is held constant. Duplicate ACKs show that some packets are still getting through; a timeout means no ACKs are coming back at all, so TCP backs off further.
The figure’s ssthresh = cwnd/2 is a simplification that assumes the data in flight fills cwnd. RFC 5681 explicitly says to use FlightSize rather than cwnd, since in some implementations cwnd can grow well beyond rwnd.
Implementation Variants
The four core algorithms share the same skeleton, but specific behavior varies by implementation.
TCP Reno: The first implementation to integrate slow start, congestion avoidance, fast retransmit, and fast recovery. Handles one packet loss per window efficiently.
TCP NewReno: Addresses Reno’s limitation. When multiple packets are lost within a single window, the ACK that follows a retransmission covers only part of the lost range. This is called a partial ACK. Reno ends fast recovery on a partial ACK, while NewReno stays in fast recovery and retransmits the remaining lost packets one by one.
TCP CUBIC: The default congestion control algorithm in Linux. Uses a cubic function instead of RTT-proportional linear increase during congestion avoidance. Utilizes available bandwidth faster on high-bandwidth, long-distance networks.
BBR, Bottleneck Bandwidth and RTT: A model-based algorithm developed by Google. Determines transmission rate based on measured bandwidth and RTT rather than packet loss. Estimates the actual bottleneck bandwidth of the network path and achieves maximum throughput without excessively filling router buffers. A substantial share of Internet traffic is reported to use BBR.
UDP
UDP (User Datagram Protocol) is a connectionless protocol. It transmits data immediately without a handshake.
Structure
The UDP header is 8 bytes: source port, destination port, length, and checksum. Compared to TCP’s minimum 20-byte header, the overhead is minimal.
No ordering. No retransmission. Lost packets are the application’s responsibility. No flow control or congestion control either.
Why It Exists
TCP’s reliability comes with latency. The 3-way handshake takes at least 1 RTT. Retransmission adds more delay. Congestion control may throttle transmission speed.
For real-time workloads, this latency matters more than reliability. In a voice call, audio arriving 0.5 seconds late via retransmission disrupts the conversation. In a game, a stale position update arriving late is useless. In these cases, dropping lost data beats retransmitting it.
Comparison
When to Choose Which
TCP fits when:
- Data integrity is essential: web traffic (HTTP/HTTPS), file transfer (FTP), email (SMTP)
- Database communication: query results must arrive correctly
- API calls: lost requests or responses are unacceptable
UDP fits when:
- Real-time streaming: video, voice calls (VoIP)
- Online gaming: position and state updates
- DNS: small request/response exchanges that need speed
- IoT sensor data: periodic transmission, some loss acceptable
QUIC: The foundation protocol for HTTP/3. It implements TCP-like reliability — retransmission, ordering — and TLS encryption on top of UDP. It reduces the combined latency of TCP’s 3-way handshake plus TLS handshake while preserving reliability. Unlike TCP, which is implemented in the OS kernel and difficult to modify, QUIC operates at the application level, enabling faster iteration.
It is easy to overlook the transport layer in backend development. But why HTTP runs on TCP, why WebSocket chose TCP, and why DNS uses UDP all start here. In the end, the protocol choice comes down to whether the workload needs reliability more or needs to cut latency more.
References
- TCP Congestion Control Algorithms — why AIMD converges to fairness, and how Reno, CUBIC, and BBR differ