Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 65 ยท Building Blocks of the Web Stack

Communication Protocols in System Design

Compare TCP, UDP, HTTP, WebSockets, polling, server-sent events and gRPC, understand round trips and handshakes, and simulate their costs in Python.

Every box in a system diagram is connected to another by an arrow, and each arrow is a protocol: an agreed set of rules for how two programs exchange messages. Choosing the right protocol changes latency, efficiency, reliability and how easy a system is to debug. This lesson walks up the stack from transport protocols to application protocols and shows when to use each.

Throughout, remember the cost that dominates everything: round trips. Light in fiber takes about 1 ms per 100 km each way, so a request to a server 8,000 km away needs at least 80 ms round trip before any code runs. Protocols that need fewer round trips are faster.

The layers you will touch

Protocol layers in one glance

Application HTTP, gRPC, WebSocket, DNS, SMTP.
Transport TCP for reliable streams, UDP for datagrams.
Network IP finds the machine.
Link Ethernet, Wi-Fi move the bits.

Designers mostly choose at application and transport.

1 / 2

TCP and UDP

TCP (Transmission Control Protocol) gives you a reliable, ordered stream of bytes. Before data flows it performs a three-way handshake (SYN, SYN-ACK, ACK), costing one round trip. It numbers every byte, retransmits lost packets, and slows down when the network is congested. Web pages, APIs, databases and file transfers all use TCP because losing or reordering bytes is unacceptable.

UDP (User Datagram Protocol) just sends independent packets with no connection, no retransmission and no ordering. It is faster to start and has less overhead, so it suits data where a late packet is useless: live video and voice calls, online games, DNS lookups, metrics. If you need reliability on top of UDP you build it yourself, which is exactly what QUIC does.

TCP UDP
Connection Handshake first None
Delivery Guaranteed, in order Best effort
Overhead Higher Lower
Typical use Web, APIs, databases Streaming, games, DNS

Add TLS (HTTPS) and you pay another one or two round trips to agree on encryption keys. TLS 1.3 needs one; connection reuse and session resumption can skip even that.

Python Cost of opening a connection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A cold HTTPS request costs three round trips; a reused connection costs one. That is why servers keep connections alive and why a CDN terminates TLS close to the user.

HTTP: the default application protocol

HTTP is a request/response protocol: the client sends a method (GET, POST, PUT, DELETE), a path, headers and optionally a body; the server replies with a status code, headers and a body. It is stateless: each request carries everything the server needs, which makes HTTP services easy to scale out behind a load balancer.

  • HTTP/1.1 sends one request at a time per connection, so browsers open several connections in parallel and pages suffer head-of-line blocking: a slow response blocks those queued behind it.
  • HTTP/2 multiplexes many requests over one TCP connection with compressed headers, but one lost TCP packet still stalls every stream.
  • HTTP/3 runs over QUIC (UDP), so a lost packet only delays its own stream, and connection setup is faster.
Python HTTP/1.1 vs multiplexing: loading 12 resources

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The multiplexed version wins more as the round-trip time grows, because it removes whole rounds of waiting. This is a simplified model, but it shows why upgrading protocols helps far-away users most.

Pushing data to clients

Plain HTTP is client-initiated: the server cannot speak first. For live data (chat, notifications, stock prices, collaborative editing) there are four patterns:

  1. Short polling: the client asks "anything new?" every few seconds. Simple, but most requests return nothing and updates arrive late.
  2. Long polling: the client asks and the server holds the request open until there is something to say (or a timeout), then the client immediately asks again. Fewer empty replies, still one request per message.
  3. Server-sent events (SSE): one long-lived HTTP response through which the server streams events one way. Simple, auto-reconnects, ideal for feeds and notifications.
  4. WebSocket: after an upgrade handshake, a persistent two-way connection with tiny frames. Best for chat, games and collaboration.
Python Messages and overhead: polling vs WebSocket

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Polling faces a trade-off: poll often for low delay and waste hundreds of requests, or poll rarely and deliver late. A WebSocket sends exactly one frame per event. The cost of a WebSocket is that you must manage many long-lived connections, which changes how you scale and balance servers.

RPC and gRPC

Between your own services you often want something stricter and faster than hand-written JSON over HTTP. Remote procedure call (RPC) makes a network call look like a local function call. gRPC is a popular framework: you describe messages and services in a schema file, it generates client and server code, serializes with compact binary Protocol Buffers, and runs over HTTP/2 with support for streaming in both directions. Benefits are smaller payloads, strong typing and generated clients. Costs are that browsers need a proxy to talk to it and binary messages are harder to inspect by eye.

REST over JSON gRPC
Format Text (JSON) Binary (Protocol Buffers)
Contract Convention, optional OpenAPI Required schema
Streaming Awkward Built in
Browser use Native Needs a gateway
Best for Public APIs Internal service calls

Other protocols you will meet

  • DNS turns names into addresses, usually over UDP.
  • SMTP/IMAP move email.
  • MQTT is a tiny publish/subscribe protocol for IoT devices.
  • AMQP and Kafka protocol carry messages to queues and brokers.
  • FTP/SFTP transfer files.

Choosing a protocol

  • Public API consumed by many clients: HTTP + JSON (REST or GraphQL).
  • Internal service-to-service calls with strict contracts: gRPC.
  • Real-time two-way: WebSocket. One-way live feed: SSE.
  • Live audio or video, game state, where late data is useless: UDP or WebRTC.
  • Everything else: boring, well-supported HTTP.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does TCP start with a handshake?

    SYN, SYN-ACK, ACK set up state on both sides so bytes can be ordered and retransmitted.
  2. 2. Which protocol is the best fit for a live voice call where late packets are useless?

    Retransmitting an old audio packet only adds delay; it is better to drop it and keep going.
  3. 3. What problem does HTTP/2 multiplexing reduce compared with HTTP/1.1?

    Many requests share one connection at the same time, removing queued rounds of waiting.
  4. 4. A dashboard needs a one-way stream of updates from server to browser with automatic reconnect. Which fits best?

    SSE is a simple long-lived HTTP stream for one-way events and reconnects automatically.
  5. 5. Why is HTTP described as stateless, and why does that help scaling?

    No server-side memory of earlier requests is required, so servers are interchangeable.

Summary

  • Protocols are the arrows in a design; round trips are the cost that matters most.
  • TCP is reliable and ordered; UDP is fast and unreliable; QUIC rebuilds reliability on UDP.
  • HTTP is stateless request/response; HTTP/2 and 3 multiplex requests to cut waiting.
  • For live updates choose polling, long polling, SSE or WebSocket by direction and frequency.
  • gRPC suits internal, typed, streaming calls; HTTP + JSON suits public APIs.

Next: Proxies: Forward and Reverse