Jesse's Software Engineering Blog
Understanding TCP and UDP
There are two popular types of protocols for sending data over a network: TCP and UDP. Both protocols are built on top of the Internet Protocol thus both send packets to an IP address.
TCP/IP is the main protocol used on the Internet. When a user requests a page load a TCP connection is made via data packets called a “hand shake”. TCP requires a three-way handshake to make such a connection. Once a connection is created, there is bi-directional communication allowing for the server to send data and the browser to acknowledge that the packets were received. TCP sends data in a stream so if some data is not received TCP can resend that data thus ensuring packet delivery. TCP also ensures that packets are received in the same order in which they were sent.
UDP/IP does not require a handshake for a connection. UDP simply sends the packet with no concern about delivery or errors. This significantly reduces overhead but provides no reliability as to whether the data was ever received. Instead of sending a stream of data, UDP simply sends a single data packet at a time. When sending multiple packets, there is no assurance that packets will be received nor the order they will be received in.
- establishes connection via three-way handshake
- used by HTTP/FTP/SMTP
- retains package order
- acknowledges packet delivery
- larger header size
- significantly slower
- connection less
- used by DNS, DHCP
- no order to packages received
- no acknowledgment of packet delivery
- significantly faster
It’s import to understand the distinction between TCP and UDP in web development because performance is such a high priority. Depending on the task at hand, there may be services available to use UDP opposed to TCP. For example, database connections are generally made using TCP. The data must be received and must retain the correct order. Where as metric tools such as StatsD do not have such requirements and by sending data using UDP application speeds can be drastically improved.