networking25 min

TCP vs UDP

Two fundamental transport protocols and when to use each

0/9Not Started

Why This Matters

Every time you load a webpage, send a message, or stream a video, data travels across the internet using one of two transport protocols. TCP guarantees every byte arrives in order and intact. UDP trades reliability for speed, firing packets without waiting for confirmation.

Choosing the wrong protocol can make a web page fail to load or a video call unbearably laggy. Understanding TCP and UDP is the first step to understanding how all network communication works.

Define Terms

Visual Model

TCPReliable
Client
SYN
SYN-ACK
ACK
Server
UDPFast
Client
Packet
Server
No ACK
1
2
3
send

The full process at a glance. Click Start tour to walk through each step.

TCP establishes a reliable connection with a three-way handshake. UDP skips the handshake and sends packets immediately.

Code Example

Code
// TCP Server in Node.js
const net = require("net");

// Create a TCP server
const server = net.createServer((socket) => {
  console.log("Client connected");

  socket.on("data", (data) => {
    console.log("Received:", data.toString());
    socket.write("Message received!"); // reliable reply
  });

  socket.on("end", () => {
    console.log("Client disconnected");
  });
});

server.listen(3000, () => {
  console.log("TCP server on port 3000");
});

// TCP Client
const client = new net.Socket();
client.connect(3000, "localhost", () => {
  client.write("Hello, server!");
});

client.on("data", (data) => {
  console.log("Server says:", data.toString());
  client.end(); // close connection
});

// UDP would use dgram module:
// const dgram = require("dgram");
// const udpSocket = dgram.createSocket("udp4");
// udpSocket.send("Hello", 3001, "localhost");

Interactive Experiment

Try these thought experiments:

  • Imagine you are downloading a 10 MB file. What happens if a TCP packet is lost? (It gets retransmitted automatically.)
  • Now imagine you are on a video call and a UDP packet is lost. Would you rather re-request the lost frame (causing a pause) or skip it and keep going?
  • Open your browser's developer tools and look at the Protocol column in the Network tab. What protocol do most page loads use?

Quick Quiz

Coding Challenge

Protocol Classifier

Write a function called `classifyProtocol` that takes a use-case string and returns either 'TCP' or 'UDP'. Use these rules: web browsing, email, file transfer, and database queries need TCP (reliability). Video streaming, online gaming, DNS lookups, and voice calls need UDP (speed).

Loading editor...

Real-World Usage

TCP and UDP are the invisible foundation of everything you do online:

  • TCP in action: Every website you visit (HTTP runs over TCP), every email you send (SMTP), every file you download (FTP), and every database query (PostgreSQL, MySQL)
  • UDP in action: Zoom and Google Meet calls, Twitch and YouTube live streams, online multiplayer games (Fortnite, Valorant), DNS resolution
  • Hybrid approaches: Many real systems use both. A game might use TCP for login/chat (must be reliable) and UDP for real-time position updates (must be fast)
  • QUIC protocol: HTTP/3 uses QUIC, which is built on UDP but adds reliability features — combining the best of both worlds

Connections