Sometimes the API server log shows that a response was sent successfully, yet the client hits a timeout.
Meanwhile, a log collection agent may drop some logs without raising any error.
The difference between the two comes down to
which transport-layer protocol the application runs on
.
In the Application Layer series, we saw that processes send messages through sockets.
This series covers the
transport layer
, which sits beneath the socket and actually delivers those messages to the other process.
In this first post, we look at what services the transport layer provides and how TCP and UDP divide those services between them.
The transport layer provides logical communication between application processes running on different hosts.
Logical communication
means that, from the application's point of view, it looks as if it were directly connected to the other process.
In reality messages pass through many routers and links, but the application does not care about the path; it only writes to and reads from a socket.
Transport-layer protocols are implemented
only on hosts at the network edge
.
The sender splits application messages into small pieces, adds a transport-layer header to create
segments
, and passes them to the network layer.
The receiver interprets the headers of segments received from the network layer, reassembles the message, and delivers it to the right socket.
Routers only look as far as the network layer when forwarding, so they do not interpret the transport-layer header inside a segment.
Think of siblings living in two houses who exchange letters.
The postal service that carries letters from house to house is the
network layer
, and the person in each house who collects letters for the post office and hands out received letters to the siblings is the
transport layer
.
The key point of this analogy is that the services the transport layer can provide are
constrained by the services of the layer below
.
If the postal service does not guarantee a delivery deadline, the person handing out letters cannot guarantee one either.
However, some services the lower layer does not provide, such as asking for a lost letter to be resent, can be built by the transport layer itself.
The transport layer provides logical communication between processes on both hosts, while routers process only up to the network layer and forward datagrams carrying segments
Uses sequence numbers, acknowledgments, timers, and retransmission to deliver data in order without loss. It builds reliability on top of unreliable IP.
Connection management
Synchronizes the state of both sides with a 3-way handshake before sending data.
Flow control
Adjusts the sending rate so the receiver's buffer does not overflow.
Congestion control
Reduces the sending rate when the network is congested so multiple connections share the link fairly.
matter more than reliability.
There is no connection setup delay, transmission does not suddenly slow down because of congestion control, and the application can implement exactly as much reliability as it needs.
DNS queries and responses are short, so connection setup would cost more than the exchange itself, and QUIC in HTTP/3 implements its own reliability and congestion control on top of UDP.
If the log collection agent from the start of this post uses UDP, silently dropping some logs is expected behavior.
Conversely, if an API running over TCP times out, TCP may be repeatedly retransmitting and waiting.
๐
If you send logs or metrics over UDP, document in your runbook that
loss does not produce an error
.
Audit logs or payment events that must not be lost should use TCP-based transport or a message queue with acknowledgments.
You can see the difference between the two protocols yourself with nc (netcat), which comes preinstalled on macOS.
Open two terminals and run the server in one and the client in the other.
Lines typed in the client appear in the server terminal.
If you run only the client without starting the server first, the connection is refused and the client exits immediately.
This is because TCP uses a handshake to confirm that the other process exists before sending data.
With UDP, the client keeps running even without a server, and the data you type can vanish without any notice.
Since there is no connection setup step, it sends without checking whether the other side is ready to receive.
nc on Linux uses the same options, and on Windows you can run the same experiment with ncat, included with Nmap.
The transport layer extends communication between hosts into
logical communication between processes
.
Because IP below provides only best-effort delivery, UDP adds just port-based delivery and error detection, while TCP builds reliability, connection management, flow control, and congestion control itself.
When your application has data loss or latency problems, first check which transport protocol it uses, and consider what that protocol does and does not guarantee.
The next post looks at
multiplexing and demultiplexing
, how the transport layer uses port numbers to separate the data of multiple processes.