9 Difference between TCP and UDP Protocol - Java Network Interview Question

TCP and UDP are two transport layer protocols, which are extensively used on the internet for transmitting data from one host to another. Good knowledge of how TCP and UDP works is essential for any programmer. That's why the difference between TCP and UDP is one of the most popular programming interview questions. I have seen this question many times on various Java interviews, especially for server-side Java developer positions. Since FIX (Financial Information Exchange) protocol is also a TCP based protocol, several investment banks, hedge funds, and exchange solution provider looks for Java developer with good knowledge of TCP and UDP. Writing fix engines and server side components for high-speed electronic trading platforms needs capable developers with a solid understanding of fundamentals including data structure, algorithms, and networking.

By the way, use of TCP and UDP is not limited to one area, it's at the heart of the internet. The protocol which is the core of internet, HTTP is based on TCP. One more reason, why Java developer should understand these two protocols in detail is that Java is extensively used to write multi-threaded, concurrent and scalable servers.

Java also provides rich Socket programming API for both TCP and UDP based communication. In this article, we will learn key differences between TCP and UDP protocol, which is useful to every Java programmers.

To start with, TCP stands for Transmission Control Protocol and UDP stands for User Datagram Protocol, and both are used extensively to build Internet applications.




Difference between TCP vs UDP Protocol

I love to compare two things at different points, this not only makes them easy to compare but also makes it easy to remember differences. When we compare TCP to UDP, we learn the difference in how both TCP and UDP works, we learn which provides reliable and guaranteed delivery and which doesn't. Which protocol is fast and why, and most importantly when to choose TCP over UDP while building your own distributed application.

In this article, we will see the difference between UDP and TCP in 9 points, e.g. connection set-up, reliability, ordering, speed, overhead, header size, congestion control, application, different protocols based on TCP and UDP and how they transfer data.

By learning these differences, you not only able to answer this interview question better but also understand some important details about two of the most important protocols of the internet.



1) Connection-oriented vs Connectionless
First and foremost difference between them is TCP is a connection-oriented protocol, and UDP is connectionless protocol. This means a connection is established between client and server before they can send data. Connection establishment process is also known as TCP handshaking where control messages are interchanged between client and server. Attached image describe the process of TCP handshake, for example, which control messages are exchanged between client and server.

The client, which is the initiator of TCP connection, sends SYN message to the server, which is listening on a TCP port. The server receives and sends an SYN-ACK message, which is received by client again and responded using ACK. Once the server receives this ACK message,  TCP connection is established and ready for data transmission.

On the other hand, UDP is a connectionless protocol, and point to point connection is not established before sending messages. That's the reason, UDP is more suitable for multicast distribution of the message, one to many distributions of data in single transmission.


TCP handshake process SYN, SYN-ACK and ACK


2) Reliability
TCP provides the delivery guarantee, which means a message sent using TCP protocol is guaranteed to be delivered to the client. If a message is lost in transits then its recovered using resending, which is handled by TCP protocol itself. On the other hand, UDP is unreliable, it doesn't provide any delivery guarantee. A datagram package may be lost in transits. That's why UDP is not suitable for programs which require guaranteed delivery.


3) Ordering
Apart from the delivery guarantee, TCP also guarantees the order of message. The message will be delivered to the client in the same order, the server has sent, though it's possible they may reach out of order to the other end of the network. TCP protocol will do all sequencing and ordering for you. UDP doesn't provide any ordering or sequencing guarantee.

Datagram packets may arrive in any order. That's why TCP is suitable for application which needs delivery in a sequenced manner, though there is UDP based protocol as well which provides ordering and reliability by using sequence number and redelivery e.g. TIBCO Rendezvous, which is actually a UDP based application.



4) Data Boundary
TCP does not preserve data boundary, UDP does. In Transmission control protocol, data is sent as a byte stream, and no distinguishing indications are transmitted to signal message (segment) boundaries. On UDP, Packets are sent individually and are checked for integrity only if they arrived. Packets have definite boundaries which are honored upon receipt, meaning a read operation at the receiver socket will yield an entire message as it was originally sent. Though TCP will also deliver the complete message after assembling all bytes. Messages are stored on TCP buffers before sending to make optimum use of network bandwidth.


5) Speed
In one word, TCP is slow and UDP is fast. Since TCP does has to create a connection, ensure guaranteed and ordered delivery, it does a lot more than UDP. This cost TCP in terms of speed, that's why UDP is more suitable where speed is a concern, for example, online video streaming, telecast or online multiplayer games. If you want to learn more about why UDP is faster than TCP then consider reading The TCP/IP Guide,  A Comprehensive, Illustrated Internet Protocols Reference 1st Edition
by Charles M. Kozierok, one of the must-read books to learn TCP/IP protocol.

 Difference between TCP and UDP



6) Heavyweight vs Lightweight
Because of the overhead mentioned above, Transmission control protocol is considered as heavyweight as compared to lightweight UDP protocol. Simple mantra of UDP to deliver a message without bearing any overhead of creating connection and guaranteeing delivery or order guarantee keeps it light weight. This is also reflected in their header sizes, which is used to carry metadata.


7) Header size
TCP has bigger header than UDP. Usual header size of a TCP packet is 20 bytes which are more than double of 8 bytes, header size of UDP datagram packet. TCP header contains Sequence Number, Ack number, Data offset, Reserved, Control bit, Window, Urgent Pointer, Options, Padding, Check Sum, Source port, and Destination port. While UDP header only contains Length, Source port, Destination port, and Check Sum. Here is how TCP and UDP header looks like:

TCP Header Format


UDP Header Format 



8) Congestion or Flow control
TCP does Flow Control. TCP requires three packets to set up a socket connection before any user data can be sent. TCP handles reliability and congestion control. On the other hand, UDP does not have an option for flow control. See TCP/IP Illustrated - The Protocol Volume 1  By W. Richard Stevens to learn more about how TCP does flow control

9 Difference between TCP and UDP Protocol



9) Usage and application
Where do TCP and UDP are used on the internet? After knowing key differences between TCP and UDP, we can easily conclude, which situation suits them. Since TCP provides delivery and sequencing guaranty, it is best suited for applications that require high reliability, and transmission time is relatively less critical.

While UDP is more suitable for applications that need fast, efficient transmission, such as games. UDP's stateless nature is also useful for servers that answer small queries from huge numbers of clients. In practice, TCP is used in finance domain e.g. FIX protocol is a TCP based protocol, UDP is used heavily in gaming and entertainment sites.




10) TCP and UDP based Protocols
One of the best examples of TCP based higher end protocol is HTTP and HTTPS, which is everywhere on the internet. In fact, most of the common protocol you are familiar of e.g. Telnet, FTP and SMTP all are based on Transmission Control Protocol. UDP don't have anything as popular as HTTP but UDP is used in a protocol like DHCP (Dynamic Host Configuration Protocol) and DNS (Domain Name System). Some of the other protocol, which is based on User Datagram protocol is Simple Network Management Protocol (SNMP), TFTP, BOOTP and NFS (early versions).

Btw, while working in TCP/UDP based application on Linux, it's also good to remember basic networking commands e.g. telnet and netstat, they help tremendously to debug or troubleshoot any connection issue.


That's all about the difference between TCP and UDP protocol. Always remember to mention that TCP is connection oriented, reliable, slow, provides guaranteed delivery and preserves the order of messages, while UDP is connectionless, unreliable, no ordering guarantee, but a fast protocol. TCP overhead is also much higher than UDP, as it transmits more metadata per packet than UDP.

It's worth mentioning that header size of Transmission control protocol is 20 bytes, compared to 8 bytes header of User Datagram protocol. Use TCP, if you can't afford to lose any message, while UDP is better for high-speed data transmission, where loss of a single packet is acceptable e.g. video streaming or online multiplayer games.


Further Reading
TCP/IP Illustrated - The Protocol By W. Richard Stevens
The TCP/IP Guide: A Comprehensive, Illustrated Internet Protocols

9 comments :

Anonymous said...

Paragraph not finished by some mistake:
...Some of the other protocol, which is based over User Datagram protocol is <- missing part :)

Also thanks for this article!

Anonymous said...

One more difference is TCP is a stream oriented protocol, while UDP is a message oriented protocol.

Assassin Kitten said...

[quote]One more difference is TCP is a stream oriented protocol, while UDP is a message oriented protocol.[/quote]
Isn't that the other way around?

Anonymous said...

The content is good and makes up for some of the basic TCP/IP and UDP Interview Questions. If you need really hard interview questions based upon TCP protocol, here are some of them, I don't know answer of them and I dobut many people will know.

1) What is the lowest TCP port number?
2) The TCP frame has an URG pointer field, when is it used?
3) Can the RST packet have a payload?
4) When is the "flow" field in IPv6 used?
5) What does the IP_FREEBIND socket option do?
6) What does the PSH flag actually do?
7) The TCP timestamp is implicated in SYN cookies. How?
8) Can a "UDP" packet have a checksum field set to zero?
9) How does TCP simultaneous open work? Does it actually work?
10) What is a stupid window syndrome?
11) What are the CWE and ECE flags in TCP header?
12) What is the IP ID field and what does it have to do with DF bit? Why do some packets have a non-zero IP ID and a DF set?
13) Can a SYN packet have a payload?
14) Can a SYN+ACK packet have a payload?
15) Linux uses two queues to handle incoming TCP connections: the SYN queue and the accept queue. What is the length of the SYN queue?
16) What happens if the SYN queue grows too large and overflows?

osman boy said...

perfect :
here summary :
Both TCP and UDP work at transport layer TCP/IP model, but have very different usage.

The most important differences are:

Reliability:
TCP: connection-oriented
UDP: connectionless
Ordered:
TCP: order of message receipt is guaranteed
UDP: order is not guaranteed
Protocol weight:
TCP: heavyweight, because of the connection/ordering overhead
UDP: lightweight, very few overhead
Packets:
TCP: streaming, data is read as a "stream," with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call.
UDP: datagrams, one packet per one read call.
Typical protocols which use TCP are HTTP, FTP and SMTP. Examples of protocols using UDP are DNS and DHCP.

Praveen Rana said...

I wasn't aware of this website. Now I have it on the top of my bookmarks list.Thanks a lot creator(s) of this.

Javin Paul said...

@Praveen, your welcome, glad you like it. Please share with your friends, it does make a difference.

Anonymous said...

nice...and thanks for sharing this much of info..i like it..

RamyaKrishna Jasti said...

Thanks.this article gives better information.but we want some information about programming side.Thank you

Post a Comment