Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Computer and Network Foundations

System Design HLD-01.03

Networking basics: IP, TCP, UDP and ports

How packets find their way, how TCP turns them into a reliable stream, why UDP skips all that, and what a port and a socket are.

Beginner 14 min Last reviewed

The big idea

The internet does not send your web page in one piece. It chops data into small packetsA small chunk of data, typically up to about 1,500 bytes, with a header saying where it comes from and where it is going., and each one makes its own way across dozens of routers. Packets can arrive out of order, arrive twice, or not arrive at all. The network promises nothing.

On top of that unreliable delivery sit two very different choices. TCP adds acknowledgements, retries and ordering, so your program sees a perfect stream of bytes. UDP adds almost nothing, so your program gets speed and has to cope with gaps itself. Knowing which one a system uses tells you how it behaves when the network gets bad, which it always eventually does.

Think of mailing a book one page per envelope. The postal service is IP: envelopes usually arrive, sometimes late or out of order, occasionally lost. TCP is a friend at the other end who numbers every page, asks you to resend missing ones and only hands over the book when it is complete and in order. UDP is just mailing the pages and hoping.

How it works

Step 1: IP moves packets between machines

Every machine on a network has an IP addressA numeric address that identifies a machine on a network. IPv4 addresses look like 203.0.113.7; IPv6 addresses are much longer.. Each packet carries a source and destination address, and each router along the way looks at the destination and forwards the packet one step closer. Routers do not keep track of conversations, and if they are overloaded they simply drop packets. That is why IP is called best effortDelivery with no guarantee: the network tries, but packets may be lost, duplicated, delayed or reordered..

Step 2: Ports pick the program

An IP address gets a packet to a machine, but a machine runs many programs. A portA number from 0 to 65535 that identifies which program on a machine should receive traffic. Web servers usually listen on 443 for HTTPS. picks which one. HTTPS servers listen on port 443, DNS on 53, PostgreSQL on 5432. A connection is identified by four numbers: source IP and port, destination IP and port. The endpoint a program reads and writes through is called a socketThe operating system object a program uses to send and receive network data, bound to an address and port..

Step 3: TCP builds a reliable stream

TCPTransmission Control Protocol: a connection-based protocol that delivers a reliable, ordered stream of bytes over IP. starts every connection with a three-way handshake, so both sides agree on starting sequence numbers:

TCP three-way handshake, then data with a retransmission A sequence diagram between client and server: SYN, SYN-ACK and ACK set up the connection in one round trip. Data follows and is acknowledged. One data packet is lost, no acknowledgement comes back, and the client resends it after a timeout. Client Server SYN (my numbers start at x) 0.5 round trip SYN-ACK (got x; mine start at y) 1 round trip ACK (got y) + first data data: bytes 1 to 1400 ACK 1400 bytes 1401 to 2800 lost no ACK in time: resend 1401 to 2800 + timeout Figure 1. The handshake costs one round trip before data flows. Every byte is acknowledged; anything not acknowledged in time is sent again, so a loss costs extra time instead of missing data.

After the handshake TCP gives four guarantees:

  • Reliable: lost data is detected by missing acknowledgements and sent again.
  • Ordered: sequence numbers let the receiver put bytes back in order, and it will not hand later bytes to the program until earlier ones arrive.
  • Flow control: the receiver says how much it can buffer, so a fast sender does not overwhelm it.
  • Congestion control: the sender starts slowly and backs off when packets are lost, so the network is not flooded.

The price is latency. A new connection costs a round trip before any data moves, and a single lost packet holds back everything behind it until the retransmission arrives. This stall is called head-of-line blockingWhen one delayed or lost item at the front of a queue holds up everything behind it, even items that are ready..

Step 4: UDP just sends

UDPUser Datagram Protocol: sends individual packets with no connection, acknowledgements, retries or ordering. has no handshake, no acknowledgements and no ordering. A program hands it a packet and it goes. If it is lost, nobody notices unless the program does.

UDP sends packets without waiting for acknowledgement A sender emits a stream of packets toward a receiver. One packet is lost along the way, and the receiver simply gets a gap. Sender lost Receiver Figure 2. UDP is fire and forget: no handshake, no acknowledgements. A lost packet is a gap, which a video call can hide better than it can hide a delay.

That sounds worse, but for some traffic it is better. In a video call, a packet that arrives 300 ms late is useless: the moment has passed. It is better to skip it and play the next frame than to freeze while TCP retries. Games, voice, live video and DNS lookups all prefer UDP for this reason.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. At 0 percent loss, both protocols finish together.
  2. Raise loss to 10 percent and press Send again a few times. TCP always delivers everything but finishes later, and the delays bunch up behind each loss. UDP finishes on time with holes.
  3. Imagine each packet is a frame of a video call, then a chunk of a bank statement. Which protocol would you choose for each?

Which transport would you choose for each kind of traffic?

Show answers
  • Loading a web page over HTTP/1.1 or HTTP/2: TCP. Every byte of HTML and code must arrive, in order.
  • A multiplayer game sending player positions 30 times a second: UDP. A newer position replaces an old one, so retrying a late packet is wasted effort.
  • Uploading a file to cloud storage: TCP. A file with a missing chunk is corrupt.
  • A video call: UDP. Late audio is worse than a tiny gap, so calls use UDP with their own light recovery.
  • A simple DNS lookup: UDP. One small question and answer: if lost, just ask again. DNS falls back to TCP for large answers.
  • A database client sending SQL queries: TCP. Queries and results must arrive complete and in order.

Going deeper

Slow start

A new TCP connection does not know how fast the path is, so it starts by sending only a few packets and roughly doubles the amount each round trip until it sees loss. This is why a fresh connection is slow for its first several round trips, and why reusing connections, covered in HTTP/1.1, HTTP/2 and HTTP/3 , matters so much.

QUIC: reliability on top of UDP

Because UDP adds so little, it is a good base for new protocols. QUIC, used by HTTP/3, runs on UDP and builds its own reliability, encryption and multiple independent streams, so a lost packet only stalls the stream it belongs to. You will see the effect in HTTP/1.1, HTTP/2 and HTTP/3 .

Trade-offs

OptionWins whenLoses when
TCP Anything that must arrive complete and in order: web pages, APIs, files, databases Real-time media and games, where waiting for a retry is worse than a gap
UDP Real-time voice, video, games and tiny request-response lookups like DNS Data that must be complete, unless you build reliability yourself
QUIC (on UDP) Web traffic on lossy or mobile networks: faster setup, no cross-stream blocking Networks that block or throttle UDP, and older tooling

Common mistakes

  • Assuming a successful send means the data arrived. With UDP nothing is confirmed; even with TCP, the other program may crash before processing.
  • Opening a new TCP connection per request. Each one pays the handshake and slow start again.
  • Building your own reliability on UDP for ordinary data. TCP and QUIC already solve it, well.
  • Forgetting that firewalls must allow the port. A service listening on 8443 is unreachable if only 443 is open.

Check yourself

  1. What does IP guarantee about delivery?

    Show answer

    C. Nothing: delivery is best effort IP forwards packets but may drop, duplicate, delay or reorder them. Reliability is added by TCP or the application.

  2. How many round trips does a TCP handshake add before data can flow?

    Show answer

    B. About one SYN, SYN-ACK and ACK: three messages, about one round trip, after which the client can send data.

  3. On a TCP connection, packet 5 of 10 is lost. What does the receiving program see?

    Show answer

    B. Packets 1 to 4, then a pause, then 5 to 10 in order TCP delivers in order, so 6 to 10 wait in a buffer until 5 is retransmitted. That wait is head-of-line blocking.

  4. What does a port number identify?

    Show answer

    B. A program or service on a machine The IP address finds the machine and the port finds the program listening on it.

Recap

  • IP moves packets between machines by address, with no promise of delivery or order.
  • Ports pick the program on a machine; a connection is two IPs plus two ports.
  • TCP adds a handshake, acknowledgements, retries, ordering and congestion control.
  • TCP's price is latency: a setup round trip, and head-of-line blocking after a loss.
  • UDP just sends, which suits real-time traffic where late data is useless.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up