bigonotes
System DesignNetworking

TCP and UDP

TCP vs UDP: what each guarantees, what it costs, when to pick UDP, and how to work around browsers not allowing raw UDP.

4 min readLast updated

The transport layer (layer 4) decides how data gets from one program to another on top of IP: whether it must arrive, whether order matters, and how fast to send. Other transport protocols exist (QUIC, which runs over UDP and underpins HTTP/3, is the notable one), but for system design you mainly need to know two: TCP and UDP.

TCP: the default#

TCP (Transmission Control Protocol) promises that every byte arrives, in the order it was sent, with no duplicates or corruption. Almost everything you build sits on it: HTTP, database connections, message brokers.

That promise is not free. TCP keeps five mechanisms running to hold it:

  1. Handshake first: client and server exchange SYN, SYN-ACK and ACK before any data moves. That is one extra round trip per new connection.
  2. Acknowledgments and resends: the receiver confirms what it got, and the sender resends anything that goes unconfirmed.
  3. Ordering: bytes are numbered, so the receiver can put them back in sequence.
  4. Flow control: the receiver advertises how much it can buffer, and the sender stays under that.
  5. Congestion control: the sender slows down when the network shows signs of overload, which is what stops the internet from collapsing under load.

UDP: send and forget#

UDP (User Datagram Protocol) does almost nothing beyond addressing. The sender puts a source and destination port on a packet and sends it. No handshake happens, nothing is acknowledged, and nothing is put back in order. If a packet is lost or arrives late, UDP does not notice. The application has to cope.

What you get in return is speed. There is no setup round trip, the header is small, and one lost packet never holds up the ones behind it. That makes UDP the better fit when a late packet is worse than a missing one: a video call would rather skip a frame than freeze while it waits for a resend.

Side by side#

The two protocols sit at opposite ends of the same trade-off.

Question TCP UDP
Setup before data? Three-way handshake None
Lost packet? Detected and resent Gone, unless the app handles it
Arrives in order? Always Not guaranteed
Slows down for the receiver or network? Yes No
Header 20 to 60 bytes 8 bytes
Typical uses Web, APIs, databases, file transfer Voice and video calls, games, DNS, metrics

Picking one#

Start with TCP and move to UDP only with a reason. Good reasons look like this:

  • A late packet is useless, as in voice, video and multiplayer games.
  • Losing a few messages is fine, as with high-volume metrics where one dropped sample changes nothing.
  • Every millisecond of setup counts, as with small one-shot lookups like DNS.

Check one more thing before you choose UDP: who the clients are. Browsers limit it, as the next section explains.

Browsers cannot open UDP sockets#

JavaScript in a browser cannot open a raw UDP socket, for security reasons. The one exception is WebRTC, which uses UDP for peer-to-peer audio and video. It is heavy to set up if all you want is to send small messages.

So for frequent, low-value messages (typing indicators, presence pings, cursor positions) you often split by client:

  • Native apps: send over UDP for the lowest latency.
  • Browsers: use WebSockets (TCP), or long polling over HTTP as a last resort.
  • Server side: combine events instead of forwarding each one. Rather than relaying 200 cursor moves a second, send the latest position 10 times a second, e.g. {"cursor": [412, 96]}.

Example: shared cursors in a collaborative whiteboard. Every participant moves their mouse constantly. Desktop app clients stream positions over UDP. Browser clients use a WebSocket. The server keeps only the latest position per user and sends everyone one combined update every 100 ms, so a lost or skipped position simply gets replaced by the next one.

Key takeaways#

  • TCP guarantees delivery, order and no duplicates, at the cost of a handshake and ongoing overhead.
  • UDP adds almost nothing on top of IP. It is fast, but loss and ordering are the application's problem.
  • Default to TCP. Pick UDP when late data is worthless or small losses are fine.
  • Browsers cannot use raw UDP. Use WebRTC, or WebSockets plus server-side batching.
Revision notes · 2 min

Core idea#

The transport layer (layer 4) decides how data gets from one program to another on top of IP. TCP trades speed for reliability. UDP trades reliability for speed.

Key points#

  • TCP sets up a connection with a three-way handshake: SYN, SYN-ACK, ACK.
  • TCP resends unacknowledged data and keeps messages in order on a connection.
  • Flow control stops a TCP sender from overwhelming the receiver.
  • Congestion control makes a TCP sender back off when the network is congested.
  • UDP has no handshake, no delivery guarantee and no ordering. The application handles loss.
  • QUIC runs over UDP and underpins HTTP/3.

Side by side#

Question TCP UDP
Setup before data? Three-way handshake None
Lost packet? Detected and resent Gone, unless the app handles it
Arrives in order? Always Not guaranteed
Header 20 to 60 bytes 8 bytes
Typical uses Web, APIs, databases Calls, games, DNS, metrics

Use UDP only when#

  • A late packet is useless, as in voice, video and games.
  • Losing a few messages is fine, as with high-volume metrics.
  • Setup time counts, as with small one-shot lookups like DNS.
  • Clients are not browsers, or browsers have a fallback.

Pitfalls#

  • Browsers do not allow raw UDP sockets. WebRTC is the exception, but it is complex.
  • For browsers, fall back to WebSockets (TCP) or long polling (HTTP).
  • TCP is heavier, so batch on the server. Send the latest cursor position 10 times a second, not every move.

Say it in an interview#

  • "TCP by default. UDP for real-time media, games and loss-tolerant telemetry."
  • "Mobile clients use UDP, browsers use WebSockets, and the server batches updates."

Found a mistake? Email ask@bigonotes.com