Network Engineering #3 - TCP vs UDP

Last Edited: 9/19/2024

This blog post introduces the concept of TCP and UDP.

DevOps

Now that we’ve figured out how IP addresses are handled and used for communication, we will zoom out and discuss some of the rules governing how computers communicate with each other, which are dictated by the layers above the 3rd network layer. In this article, we will specifically talk about protocols used in the 4th transport layer: TCP and UDP.

TCP

Transmission Control Protocol (TCP) is one of the most widely used protocols in the transport layer. Under TCP, before actual data is sent, the client first sends a SYN (synchronization) flag to the server. Once the server receives it, it sends back an ACK (acknowledgment) flag to the client to confirm the reception of the SYN. The server also sends a SYN flag to the client, and finally, the client sends back an ACK flag to confirm the bidirectional connection. This process of establishing the connection is called the TCP 3-Way Handshake.

TCP

During the exchange, the sequence number and acknowledgment number are sent along with the flags to keep track of how many bytes the client and server have received and sent. The sequence number tracks the bytes sent by the party, while the acknowledgment number tracks how many bytes have been received by the other party. By keeping track of these numbers, both parties can ensure that there are no missing or out-of-order segments. After the connection is established, it is usually kept open until both parties acknowledge the FIN (finish) flag or other flags that require the connection to be closed.

UDP

If you think TCP involves too many processes, you might consider an alternative: User Datagram Protocol (UDP). UDP is a much simpler protocol with no concept of connection. It just attaches minimal information, like port numbers, and sends the data to the server. This means it does not guarantee that the server is ready to receive the data, nor does it ensure that the data will be received fully or in order. However, UDP is much lighter and faster than TCP.

TCP vs UDP

As shown above, there is a tradeoff between speed and reliability. UDP is typically faster but less reliable than TCP. However, as technology has improved, the difference in speed has become almost unnoticeable in many use cases, which puts TCP in a stronger position. This is why many protocols are built on top of TCP.

That said, there are cases where milliseconds matter, such as in trading and gaming. In such scenarios, UDP can be configured with custom programs to handle disordered and corrupted segments. In fact, HTTP/3 uses QUIC, which is based on UDP, to achieve reliable but lightweight communication and is becoming a new standard. Hence, it’s important to be familiar with both protocols and understand their characteristics.

Resources