Communication Protocols
How bytes actually move: TCP vs UDP, HTTP/HTTPS, and the real-time options — polling vs WebSockets.
Communication protocols in 30 seconds
- TCP: reliable, ordered, retransmits — costs a handshake and latency. Use it when correctness matters (web, APIs, files).
- UDP: fire-and-forget, no guarantees, just speed. Use it when timeliness beats completeness (live video, voice, gaming).
- HTTPS = HTTP + TLS: encrypts in transit and authenticates the server. Non-negotiable today.
- HTTP/2 multiplexes requests over one connection; HTTP/3 rides QUIC (UDP) to cut setup latency on flaky networks.
- Server push options: short polling (crude), long polling (better), WebSockets (two-way, high-frequency), SSE (simple one-way stream).
- Terminate TLS at the edge — encryption costs CPU, so do it once at the proxy, not on every internal hop by default.
A phone call vs a postcard
Two ways to get a message to someone. You can phone them: first you both say hello and confirm you can hear each other, then you talk back and forth, and if a word gets garbled you ask them to repeat it. Or you can mail a postcard: you write it, drop it in the box, and hope it arrives — no setup, no confirmation, and if it's lost in the post you'll never know.
That's TCP and UDP. The phone call is TCP — a connection you set up first, with confirmation that everything arrives in order. The postcard is UDP — no setup, no guarantees, just fire it off and move on. Neither is 'better'; they're built for different needs, and almost every protocol above them inherits one of these two personalities.
Why this matters in design
Above the application sits a stack of protocols that decide how reliably and how fast data moves. You rarely implement them, but choosing between them — reliable-but-slower vs fast-but-lossy, request-response vs persistent connection — is a real design decision with user-visible consequences.
The layers, stacked
Before comparing options, it helps to see how they stack. Each layer leans on the one below it and adds something new — your application never touches the raw network directly:
+------------------------------+
| HTTP / gRPC / your app | <- the request you actually write
+------------------------------+
| TLS (optional encryption) | <- adds privacy + server identity (= HTTPS)
+------------------------------+
| TCP or UDP | <- reliable stream OR fast datagrams
+------------------------------+
| IP (addressing + routing) | <- gets packets to the right host
+------------------------------+
# Pick TCP vs UDP at the transport layer; add TLS for security;
# then your app protocol (HTTP, gRPC, ...) rides on top.TCP vs UDP
TCP is a reliable, ordered connection: it guarantees every byte arrives, in order, retransmitting what's lost. That reliability costs setup time (a handshake) and some latency. UDP is fire-and-forget: no handshake, no guarantee of delivery or order, just speed. You pick TCP when correctness matters (web pages, APIs, file transfer) and UDP when timeliness beats completeness (live video, voice, gaming — a dropped frame is better than a late one).
# TCP: 3-way handshake first, then a reliable ordered stream
conn = tcp.connect(host, 443) # SYN -> SYN/ACK -> ACK (costs a round trip)
conn.send(request) # lost bytes are retransmitted, kept in order
conn.close()
# UDP: no handshake, no retransmit, no ordering guarantee
udp.send_to(host, 5004, packet) # fire-and-forget; a dropped packet is just gone
# good when a late frame is worse than a missing one (live video, voice, gaming)| Property | TCP | UDP |
|---|---|---|
| Delivery | Guaranteed, retransmitted | Best-effort, may drop |
| Ordering | In order | No ordering |
| Speed/overhead | Slower (handshake, acks) | Fast, minimal overhead |
| Fits | Web, APIs, email, file transfer | Live video/voice, gaming, DNS queries |
Why TCP pays for a handshake first
The 'setup cost' of TCP isn't bureaucracy — it's the price of reliability. Before any data flows, the two sides do a three-way handshake to agree they're both reachable and to sync the sequence numbers that let them reorder packets and detect losses. That's one full round trip of pure setup before a single useful byte moves.
Client Server
| --- SYN seq=x ---> | # 'let's talk, my seq is x'
| <-- SYN-ACK seq=y ack=x+1 --- | # 'ok; my seq is y, I got x'
| --- ACK ack=y+1 ---> | # 'got yours too'
|======== connection established ====| # reliable ordered byte stream
# That round trip is why opening connections is costly -
# and why we reuse them (keep-alive) instead of reconnecting per request.HTTP and HTTPS
HTTP is the request-response protocol of the web, running on top of TCP. HTTPS is HTTP wrapped in TLS encryption — it protects data in transit, verifies you're talking to the real server, and is effectively mandatory today. Worth knowing: HTTP/2 multiplexes many requests over one connection (fewer round trips), and HTTP/3 runs over QUIC (a UDP-based transport) to cut connection-setup latency, especially on flaky mobile networks.
GET /users/42 HTTP/1.1
Host: api.example.com
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=60
{ "id": 42, "name": "Ada" }The gap plain HTTP can't fill — the server can't speak first
Plain HTTP has a built-in limitation that trips up every live feature: it's strictly request-response. The client asks, the server answers, and the exchange is over. The server has no way to say 'hey, something just happened' on its own. But chat messages, live scores, stock ticks, and notifications are all the server wanting to push to the client — the exact thing HTTP can't do by itself.
Everything in the next section is a different workaround for this one gap: either the client keeps asking, or you hold a connection open so the server finally can speak first.
Real-time: polling vs WebSockets
The options, from crude to elegant:
- Short polling — the client re-asks every few seconds. Simple, but wasteful and laggy.
- Long polling — the request is held open until there's news, then immediately re-opened. Better, still one message per round trip.
- WebSockets — one persistent, two-way connection stays open; either side can send anytime. Ideal for truly interactive, high-frequency updates.
- Server-Sent Events (SSE) — a one-way server→client stream over HTTP; simpler than WebSockets when you only need to push.
// Short polling: ask again and again, mostly getting 'nothing new'
setInterval(async () => {
const res = await fetch('/api/messages?since=' + lastId);
render(await res.json()); // wasteful: most responses are empty
}, 3000);
// WebSocket: open once, then the server pushes the instant there's news
const ws = new WebSocket('wss://chat.example.com');
ws.onmessage = (ev) => render(JSON.parse(ev.data)); // arrives with no polling
ws.send(JSON.stringify({ text: 'hi' })); // and you can send anytime| Option | Direction | Latency | Cost | Reach for it when |
|---|---|---|---|---|
| Short polling | Client asks | High (up to the interval) | Wasteful (empty responses) | Simplicity matters, updates are rare |
| Long polling | Client asks, held open | Low-ish | Moderate | You need near-real-time without WebSockets |
| WebSocket | Two-way, persistent | Lowest | One open connection per client | Interactive, high-frequency, two-way |
| SSE | Server → client stream | Low | Light, HTTP-friendly | One-way streams (feeds, token streaming) |
Protocols feel like someone else's problem until a feature stalls, drops, or leaks — then the choice of transport and connection style turns out to be the thing deciding whether it works.
Why it matters
- The TCP/UDP call sets the floor on latency and reliability for everything above it.
- The real-time choice (polling vs WebSockets vs SSE) decides whether live features feel instant or laggy and wasteful.
- TLS placement is both a security boundary and a real CPU cost you budget for deliberately.
If ignored
- Picking UDP where you needed guaranteed, ordered delivery — and quietly losing data.
- Short-polling a chat feature and drowning the server in near-empty requests.
- Running full TLS on every internal hop and paying a CPU tax you never measured.
Where it fits
- Sits at the transport and connection layer, underneath every API call and page load.
- Shapes what API styles and gateways can do — gRPC needs HTTP/2, streaming needs a persistent connection.
- Lives right beside DNS and proxies in the path a request takes before it reaches your app.
Related topics
- API styles: REST/GraphQL ride HTTP; gRPC rides HTTP/2 — this is the layer beneath them
- DNS: the step that resolves a name before any TCP connection opens
- Proxies: where TLS is usually terminated and connections are reused
- Pub/Sub & CDC: the async alternative when you don't want a live connection at all
Interview lens
Questions it unlocks
- TCP or UDP for this feature — why?
- How would you build live updates (chat, notifications)?
- What does HTTPS actually give you over HTTP?
60-second answer
I choose TCP when correctness matters and UDP when timeliness beats completeness, like live media. For server-pushed updates I reach for WebSockets when it's high-frequency and two-way, Server-Sent Events when it's a simple one-way stream, and long polling only as a fallback. HTTPS is non-negotiable — it encrypts in transit and authenticates the server — and I'd terminate TLS at the edge to control its CPU cost. I'd mention HTTP/2 multiplexing and HTTP/3-over-QUIC for cutting round-trip latency.
Common traps
- Using UDP where you actually need guaranteed, ordered delivery.
- Reaching for short polling when WebSockets or SSE fit the real-time need.
- Ignoring the CPU cost and placement of TLS termination.