What are TCP and UDP: two ways to send data over a network

Why does a web page load piece by piece while a video call keeps going on a bad line? What TCP and UDP are, how they differ, and when to use each.

You open a webpage and watch it appear in pieces: the frame first, then the images. Then you join a video call, and if the network stutters, the other person’s face breaks into squares, their voice gets distorted, but the call doesn’t drop. Those two experiences differ so much because they go through two different protocols at the lower layers of the network. A protocol, put simply, is a set of conventions both sides must follow to understand each other. The real question is: why doesn’t the computer use a single way of sending for everything?

The same problem: packets and an unpleasant truth

The Internet doesn’t send a whole file intact. It splits data into small pieces called packets, each carrying the sender’s and receiver’s addresses, then releases them into the network. These packets travel through many intermediate devices, each deciding on its own where to forward the packet next, so two consecutive packets can take two different paths.

As a result, the network brings three inconveniences no one can avoid: packets can be lost, packets can arrive out of order, and packets can arrive late. No protocol can erase those three, because physical links always fail sometimes. So the real question isn’t “how to make the network perfect”, but “when the network isn’t perfect, how do we want it to behave”. TCP and UDP are two different answers to that question.

TCP: sending like a certified letter

TCP, short for Transmission Control Protocol, is the protocol that takes on the job of turning an unreliable link into a reliable stream of data. Its way of working is a lot like a polite conversation.

Before sending data, the two sides shake hands: the sender asks if you can hear me, the receiver says yes, the sender confirms. After that each data packet is numbered. The receiver sends back an acknowledgment for each packet; any packet not acknowledged after a period of time gets resent by the sender. If packet 3 arrives before packet 2, the receiver holds packet 3 and waits, so the software above always reads data in the correct order. TCP also adjusts speed: when the network shows signs of congestion, it slows down on its own so it doesn’t make things worse.

The diagram below shows a TCP session from opening to closing, with acknowledgments and retransmissions in the middle.

sequenceDiagram
    participant A as "Sender"
    participant B as "Receiver"
    A->>B: "Open connection"
    B->>A: "Agree"
    A->>B: "Packet 1"
    B->>A: "Received 1"
    A->>B: "Packet 2 (lost)"
    A->>B: "Resend packet 2"
    B->>A: "Received 2, data complete"
    A->>B: "Close connection"

The price of this guarantee is time and effort. Each packet must wait for acknowledgment, each loss must wait out the timeout before resending, and the software above can’t receive data out of order. For a webpage, that price is entirely reasonable: you want to read the right content, a few hundredths of a second slower doesn’t matter. But for a call, waiting for the right order means delayed audio, and the human ear notices immediately.

UDP: sending like a quick note

UDP, or User Datagram Protocol, does almost the opposite: it wraps the data, writes the address, then sends. No handshake, no numbering, no acknowledgment, no retransmission. If a packet is lost, it’s lost. If it arrives out of order, the software above deals with it.

Sounds like a lazy protocol, but that’s exactly its strength. Dropping all the ceremony, UDP has low latency and doesn’t hold old data waiting for new data. In a video call, losing an audio packet a few milliseconds long means the listener just hears a tiny gap; but if you make them wait for that packet to be resent, the whole sentence falls behind and the conversation falls apart. Better to lose a little than to be late. Online games, live streaming, and the domain name system (DNS, the service that turns website names into machine addresses) all choose UDP for this reason.

One thing needs to be clear, as it’s easy to misunderstand: UDP guarantees nothing, but that doesn’t mean an app using UDP is a sloppy app. Many apps add their own mechanisms on top of UDP, keeping only what they really need. Guarantees, when needed, can be built at a higher layer; but time already lost can’t be recovered.

Two paths in one picture

The diagram below summarizes the path of data through the two protocols so you can compare them visually.

graph LR
    APP["Application"] --> TCP["TCP: numbering,<br/>acknowledgment, retransmission"]
    APP --> UDP["UDP: packet and address"]
    TCP --> OK["Data stream<br/>in correct order"]
    UDP --> FAST["Packet goes immediately,<br/>lost is lost"]
    OK --> NET["Internet"]
    FAST --> NET

There’s a subtle point: TCP and UDP aren’t two different layers, but two parallel choices at the same layer. The application picks one of the two, just like you choose to send a certified letter or a postcard depending on what you’re sending. Both sit on top of IP, the protocol that handles addressing and forwarding packets between networks, and both sit below application-layer protocols like HTTP for the web or RTP for audio and video.

When to choose which

The general rule is simple: if losing a piece of data ruins the whole result, use TCP; if a piece arriving late is already useless, use UDP. Downloading files, sending email, browsing the web: TCP. Video calls, gaming, domain name lookups: UDP. But in practice there are cases in between, and that’s when engineers have to weigh things instead of choosing by habit.

For example, some modern systems run on UDP but implement their own recovery mechanisms, to keep latency low while not losing too much data. Conversely, when the network is very poor, some apps choose TCP because they’d rather be slow than wrong. No choice is right for every case; there’s only the choice that fits what you’re sending and how users will feel when something goes wrong.

To see the trade-off clearly, look at an illustrative example, not real measured numbers. Suppose in one minute 100 packets are sent and the network loses 2. With TCP, all 100 packets arrive, but you pay with waiting time for retransmission. With UDP, 98 arrive and 2 vanish, but no one waits for anyone.

The chart below illustrates that ratio; the numbers are just for intuition, not measurements.

pie showData
    title "Illustration: 100 packets when the network loses 2"
    "Arrive immediately (UDP)" : 98
    "Lost, not resent (UDP)" : 2

With TCP, the number would be 100 packets arriving but with added waiting time, and that waiting time is exactly what we have to weigh. This is where the example starts to diverge from reality: in a real network, packet loss affects the speed of the whole data stream, so the cost of TCP is usually a bit larger than this simple picture suggests.

Worth remembering

TCP and UDP aren’t the good version and the bad version of the same thing. They’re two honest answers to two different needs: one buys certainty with time, the other buys time by accepting loss. When you see a webpage loading gradually or a call breaking up, you’re seeing that exact trade-off, chosen deliberately from the start.