In: Computer Science
Your company is developing two web apps, one for voice calls over the internet (VoIP), and another for streaming music. Between TCP and UDP, explain which protocol you would use for each app and why.
We'll cover the two major
transport protocols UDP and TCP.Before
we do that, let's talk about
transport protocols in general to
understand why we need them in the first
place. On the Internet every network
packet follows this five layer structure.
We have the application layer, the
transport layer, the network layer, the
link layer and the physical layer.
Remember that UDP and TCP are part of
the transport layer.Have you ever
wondered why it's possible that two
applications can use the same internet
connection at the same time. Well that's
because of what the transport layer
provides. It allows multiple applications
to use one network connection
simultaneously much like street names
have house numbers. The transport layer
creates about 65,000 ports on your
computer per network connection. These
ports can be reserved and used by
applications on your computer and one
application can use multiple boards at
the same time.
We have two
major transport protocols UDP and
TCP and they both have their own
characteristics. Let's look at UDP first
One of the biggest advantages of UDP is
that it's packet sizes are smaller than
TCP about sixty percent. Even UDP headers
are eight bytes and TCP headers are 20
bytes so that's a big difference.
UDP is connectionless. This means that
you don't have to create a connection
first before sending out data and
finally you have more control over when
data is being sent out. Now I do realize
that this may sound a little vague to
you but once we get tcp into the mix
this will become clearer because data
corruption is a common occurrence on the
Internet. UDP has a primitive form of
error detection it's back. It's carry a
16-bit checksum but it is not that
reliable when UDP does detect corruption
it will not try to recover from it. In
most cases the corrupted segment we'll
just be discarded in some cases it will
keep the corrupted segment but turn on a
warning flag for the application. UDP
does not attempt to compensate for lost
packets. Every packet gets sent out once
if it gets dropped on its way to the
receiver top lock it's gone. UDP does not
guarantee in order packet delivery.
Packets won't necessarily arrive in the
application in the order that they were
sent.
There's no congestion control in UDP
even if your networks really busy.
UDP will just try to cram those packets
in there. This is usually a bad strategy
because on a congested network packets
get dropped more often. In conclusion UDP
may be lightweight but it's not that
reliable this is where TCP comes in. The
transmission control protocol has
certain features that make it more
reliable than UDP however it also has a
bigger communication over at than UDP
since TCP is connection based we have to
negotiate a connection first. Before we
can do
this procedure is known as the three-way
handshake. First the initiator will ask
the acceptor if it wants to set up a
connection the acceptor will reply to
this request and when the initiator
receives this reply it will send a
packet to the acceptor that acknowledges
that the connection has now been
established. A similar song-and-dance
takes place when they close down a
connection. Now that we have this
connection we can implement all kinds of
nice features like delivery
acknowledgments when data gets sent from
one host to another the receiver will
acknowledge that it got that data. This
is one of the reasons why TCP segments
carry a number. Another feature that TCP
offers is retransmission. When a sender
doesn't get a delivery acknowledgment
within a certain amount of time it will
assume that the packet got lost on its
way so it will send it again
because segments are numbered in TCP it
can also implement in order delivery
although packets may still arrive out of
order. TCP will rearrange them before
sending them to the application. Other
TCP enhancements include congestion
control. This feature will delay
transmission of data when the network is
congested it eases the strain on the
network and helps minimize packet loss.
TCP enforces a small change to error
detection. Well there is no technical
improvement of the error detection
feature the checksum has now been made
mandatory for ipv4 as well as ipv6. For
UDP segments the checksum is only
mandatory for ipv6 packets. Let's look at
the downsides of fusing TCP. TCP segments
need bigger headers than UDP segments. As
you may remember UDP headers are about
60% smaller than TCP headers, a side
effect of TCPS congestion control
mechanism is that data doesn't always
get sent out immediately. This is of
course done on purpose to ease network
congestion but sometimes that's not what
you want take.
Skype for example when
you're making a voice call you kind of
want it to feel like a real-time
conversation right. Well what TCPS
congestion control does is when the
network is congested it deliberately
introduces latency so your real-time
conversation will not feel as real-time
as you want it to be. As you can see
congestion control can be either a
nuisance or an enhancement, it all
depends on the context really. Finally
TCP has a bigger overhead. All those
retransmissions and packet
acknowledgments and all that jazz. Well
that's overhead sometimes you don't want
every packet to arrive. Sure you want
packets to arrive but if certain packets
drop out you don't care that much for
example to stream HD video you need lots
of bandwidth but if you use TCP for
you need even more because you have to
send around acknowledgments you have to
do retransmissions and besides video
streaming can deal with a certain amount
of packet loss. They have ways to
compensate for that. In this case it
makes more sense to use UDP than TCP.
Both protocols are also different on a
conceptual level. We call UDP
message-oriented which means that
applications send their data and
distinct chunks. Think of it as snail
mail email or text messaging. Now TCP on
the other hand is stream oriented. It is
used as a continuous flow of data. Under
TCP your application does not choose how
data is sliced into packets. Just let it
know whenever anything needs to be sent
and then TCP will slice that into
packets and recompose everything. On the
other end if you'd use lots of small
packets but it could also choose to go
with few big packets it just makes
whatever works best at that time. Rest
assured though you don't have to worry
about how the packets are sliced all you
need to know is that on the receiver end
everything gets recomposed into a stream
that you can read from so which one's
better UDP or TCP it all depends on the
type of application that you're building.
Let's look at text communication
performance wise. This isn't a big
challenge because you don't need a lot
of bandwidth to send some text around.
After all under TCP everything will work
fine with UDP however you could get text
in the wrong order or text you get lost
along the way. There is no guarantee
anything could go wrong there because
UDP doesn't do read transmissions and it
doesn't guarantee in order delivery when
it comes to text communication. TCP
clearly takes the cake now
I don't know about you but when I
download files off the internet
I'd like to have all the parts including
the chunks that didn't make it through
the first time. TCP offers retransmission
UDP does not. We're also going to need in
order to deliver
because seriously what good is a
scrambled JPEG for similar reasons.
Remote access protocols like SSH also
use TCP. If you need delivery
acknowledgments UDP isn't going to do
you any good if you insist on using UDP
you could implement some sort of
acknowledgement system in your
application layer for important packets
but most of the time it's not worth the
hassle. With multimedia streaming things
are a little more ambiguous it could
either be UDP or TCP traditionally
people have chosen UDP because it has
less overhead it doesn't do that
congestion control thing that introduces
latency and we can compensate for a
certain percentage of lost packets. More
recently people have started to use TCP
for multimedia streaming if the
bandwidth is there you might as well
enjoy the benefits that TCP brings. Also
some firewalls blocked UDP altogether
for security reasons and then you're
forced to use TCP. UDP can also be used
for small question-and-answer
transactions such as DNS lookups. The
main benefit here is that you don't have
the overhead of creating and closing a
connection every time you need something
as is the case with multimedia streaming
bandwidth intensive apps that can
tolerate some amount of packet loss.
Thnk you