The Transport Layer
Learn about the services that are provided by the transport layer.
The network layer provides node-to-node communication while the transport layer provides the service of application-to-application communication.
The application-to-application communication service is useful because there can be many different applications running in a node that want to communicate with one of the applications running in a different node.
Achieving transport layer service
Application-to-application communication is achieved via the concept of ports, where each node can have many different applications running and listening on different ports for data from the network.
Some of the protocols in the transport layer can also provide more services on top of this fundamental one. We will briefly cover the transmission control protocol (TCP) and the user datagram protocol (UDP), the two most commonly used and well-known protocols of the transport layer.
User datagram protocol
UDP is a simple protocol that applications can use to send messages to each other. These messages are called datagrams.
Similar to the IP protocol, it is a connectionless protocol, which means that an application in a host can send a message to an application on a different host, without setting up a communication channel first. All it needs to do is to specify the IP address of the destination node and the port that the corresponding application is listening to.
Structure of datagram
A datagram is composed of a payload that corresponds to the application message and a header, which contains fields for the source and destination ports, the length of the datagram, and an optional checksum.
Transmission control protocol
TCP is a more complex protocol, which can provide more services. TCP is a connection-oriented protocol, which means that a connection between a client and a server needs to be established before application data can be sent. This is achieved via a 3-way handshake.
The 3-way handshake
The 3-way handshake is used to establish a connection and negotiate several parameters for it.
For example, a crucial parameter that is selected during this phase is the size of in-memory buffers, which will be used to temporarily hold received data before it can be delivered to the application.
This process starts when the client sends a request to initiate a connection (SYN), continues when the server responds with a confirmation of the connection along with a request to initiate a connection in the reverse direction (ACK & SYN), ends when the client responds with a confirmation (ACK). After this process is complete, a connection is established between the two sides that can be used to exchange data.
Data transfer
Data is sent from one node to the other as a stream of bytes. As a result, the application needs to indicate the beginning and end of an application message in this stream, through delimiters or special headers that designate the message size.
Benefits
There are benefits of TCP.
In-order delivery
TCP provides reliable and in-order delivery. This means that any data sent from the sender is guaranteed to be delivered
Achieving in-order delivery: The sender tags the data sent with a sequence number that is aligned with the number of bytes contained in the payload. The recipient sends back acknowledgements, indicating the sequence number up to which the recipient has received the data successfully. As a result, the sender is able to resend data that wasn’t acknowledged after a specific amount of time. The recipient is also able to deliver data to the application in the order they were sent using these sequence numbers.
Flow control and congestion control
TCP also provides flow control and congestion control, which means the rate at which data is transmitted by the sender is managed carefully to avoid overloading the recipient or the overall network.
Flow control is achieved when the recipient includes information in the acknowledgements about how much space is available in their receive buffer so that the sender can reduce the transmission rate when the buffer starts getting full.
Congestion control is achieved through the adjustment of the number of outstanding and unacknowledged packets on the sender side, in accordance with the perceived packet loss.
Packet loss is
Note: We intentionally avoided getting into the
, because it contains a lot of optimisations and different variations that are a full subject on their own. As a result, if one looks at the structure of a TCP packet, it is more complex than that of UDP packets. The header contains a lot of additional fields for the aforementioned functionalities, such as a sequence number, an acknowledgement number, a window size, and so on. details of this acknowledgement protocol S. W. Richard, “TCP/IP Illustrated (Vol. 1): The Protocols,” Addison- Wesley Longman Publishing Co., Inc., 1993.
Pros of UDP over TCP
After the previous analysis, we might think TCP is superior to UDP since it provides more services to the application layer. Of course, this is not true because all of these guarantees come with certain tradeoffs.
-
The fact that TCP requires a connection to be established before any data exchange can occur, makes it more heavyweight and slower compared to UDP.
-
The same applies to the services of reliable and in-order delivery, which can introduce a lot of memory overhead and network traffic due to the need to maintain connection state and message buffers and perform retries for data that is considered lost.
-
Data in TCP is sent and received as a byte stream without any boundary indicators, which means that the application layer needs to add boundary indicators along with the necessary parsing and buffering. Conversely, data in UDP is sent and received as messages with explicit boundaries.
-
UDP also provides better control over when a message will be transmitted on the network, as compared to TCP that must abide by several parameters that control when data sent from an application should be transmitted in the network. For example, Nagle’s algorithm works by combining a number of small outgoing packets and sending them all at once to utilize the network more efficiently, which means that the data sent from an application may not be transmitted immediately.
Deciding which protocol to use
It is useful to understand the pros and cons of each protocol in order to make the right decision when building an application on top of them. Below are some examples of applications and how they could benefit from the characteristics of each protocol:
- A real-time video and audio application (for example, a video conference) might benefit from UDP since it can tolerate occasional packet loss and does not benefit much from packets that arrive extremely late.
- An application that requires a fast exchange of a relatively small request followed by a single response might benefit from UDP because it does not require a connection to be established first, thereby making this interaction very fast.
DNS is an example of such an application and we will study it in the next lesson.
- An application where the recipient needs to receive all the data from the sender and in the right order can benefit from TCP’s reliable and in-order delivery. Examples of such applications include e-mail transfer (SMTP), file transfer (FTP), or transfer of web content (HTTP).
Get hands-on with 1400+ tech skills courses.