Layer 4 — Transport
Process-to-process: ports, reliability, and congestion control. TCP and UDP live here.
Level: beginner
What is it?
Layer 4 delivers data between two processes (sockets). It adds ports so many conversations share one host. TCP adds a connection, reliability, order, flow control, and congestion control. UDP adds ports and a checksum, then stops.
Why does it matter?
Almost every interview that starts at HTTP walks down to TCP. Ports, the handshake, and 'TCP vs UDP' are the core.
Mental model
The courier who knows which apartment (port) in the building (host) should get the package, and whether you wanted a signature (TCP) or just a toss (UDP).
How it works
- Purpose: end-to-end process communication.
- PDU: TCP segment or UDP datagram. Addressing: 16-bit ports.
- Protocols: TCP, UDP, SCTP, QUIC (which also eats some of TLS and HTTP/3).
- Devices: end hosts; L4 load balancers NAT on the 4-tuple.
- Reliability, ordering, flow control, congestion control — TCP yes, UDP no.
Real-world usage
- Sockets
- Load balancers
- NAT
Common mistakes
- Thinking TCP 'guarantees delivery' if the peer is gone
- Using UDP and forgetting you now own retransmission
Interview questions
What is a 4-tuple?
Source IP, dest IP, source port, dest port. That is a TCP connection.
TCP vs UDP in two contrasts.
Connection + reliability vs datagrams + no retransmission. Ports exist on both.
| TCP | UDP | |
|---|---|---|
| Connection | Yes (handshake) | No |
| Reliable / ordered | Yes | No |
| Congestion control | Yes | App's problem |
| Message boundaries | No (stream) | Yes (datagram) |
| Typical use | HTTP, SSH, DB | DNS, games, QUIC's underlay |