Networking Essentials
How to choose between TCP, UDP, HTTP, gRPC and WebSockets, what DNS and TLS do, and how to justify a protocol choice in an interview.
On this page
Every connection between a client and a service, or between two services, runs on a protocol, and the protocol decides how data is delivered, how many messages are in flight at once and who is allowed to send first. In a system design interview you are expected to pick a protocol for each connection in your design and to say why. The choice is usually between TCP, UDP, HTTP, gRPC and WebSockets.
If you choose badly, the symptoms show up in the product. A video call over TCP stutters whenever a packet is lost, and a chat built on repeated HTTP polling wastes most of its requests.
Where these protocols sit
The protocols build on each other. TCP and UDP are transport protocols that move bytes between two machines. HTTP, gRPC and WebSockets are application protocols, and the first two run on top of TCP (HTTP/3 is the exception and runs over UDP, as described below). Before a client can connect at all, it uses DNS to turn a name into an IP address.
TCP and UDP
TCP
TCP delivers a stream of bytes reliably and in order. Before any data moves, the two sides perform a handshake, which costs one round trip. After that, TCP numbers every segment, resends the ones that are lost, and slows down when the network is congested.
TCP is the default for almost everything, because most data must arrive complete and in order: web pages, API calls, database traffic and file transfers. Its costs are the handshake and head-of-line blocking. When one packet is lost, TCP holds back all the data behind it until the lost packet has been resent, even though the later data has already arrived. On a lossy mobile network this shows up as pauses.
UDP
UDP sends independent packets with no handshake, no ordering, no resending and no congestion control. A packet can be lost, duplicated or arrive out of order, and the application is told nothing. In return there is no connection setup and no waiting for lost data.
UDP fits traffic where a late packet is worth nothing: voice and video calls, live game state and live streams. Resending an old video frame only delays the frames that follow it, so it is better to drop it. Anyone who picks UDP must handle loss in the application, for example with their own retries for the messages that matter.
HTTP
HTTP is the request and response protocol of the web. The client sends a request with a method, a path and headers, and the server sends back a status code and a body. Each request is independent, so the server keeps no state between requests, which makes it easy to add servers behind a load balancer. HTTP works with browsers, caches, CDNs and every programming language, and it is the right default for any API that clients call. The server cannot start a message on its own; the client must always ask first.
The three versions differ in how they use the connection:
- HTTP/1.1 handles one request at a time on a connection. Browsers open several connections in parallel to work around this, and each new connection pays for a TCP handshake and a TLS handshake.
- HTTP/2 sends many requests at once over a single TCP connection, as separate streams, and compresses headers. It still suffers from TCP head-of-line blocking, because one lost packet stalls every stream on the connection.
- HTTP/3 runs over QUIC, which is built on UDP. Each stream recovers from loss independently, and the transport and encryption handshakes are combined, so a new connection needs fewer round trips.
gRPC
gRPC is a framework for calling functions on another service. You define the service and its messages in a schema file, and a tool generates client and server code in each language. Messages are sent in a compact binary format over HTTP/2, so they are smaller and faster to parse than JSON. gRPC also supports streaming, in which the client, the server or both send a sequence of messages on one call.
Pick gRPC for calls between your own services, especially when there are many calls, many languages, or a need for strict contracts. The generated clients catch mismatched fields at build time. It is a poor choice for a public API, because browsers cannot call it directly and need a translation proxy, and because the binary messages are harder to inspect with simple tools.
At scale, gRPC has a load balancing problem. A gRPC client keeps one long-lived HTTP/2 connection and sends all its calls through it. A layer 4 balancer chooses a server once per connection, so each client stays on one server and the load becomes uneven. The fix is a layer 7 balancer that understands HTTP/2 and spreads individual calls, or client-side balancing in which the client holds connections to several servers.
WebSockets
A WebSocket is a long-lived, two-way connection between a client and a server. It starts as an ordinary HTTP request that asks to upgrade, and after the server agrees, both sides can send messages at any time without further requests. This is what lets a server push data the moment it exists.
Pick WebSockets when updates must reach the client immediately and often, and when the client also sends frequently: chat, multiplayer games, live collaborative editing and trading screens. For updates that only flow from server to client, server-sent events or long polling are simpler and are covered in the realtime communication lesson.
Every open WebSocket is state held on a server, and this causes the scaling problems. A server can hold only so many connections, so you need many servers and a way to deliver a message to the server that holds the recipient's connection, usually through a publish and subscribe system. Deploying a new version drops the connections, and when thousands of clients reconnect at once they can overload the servers, so clients should reconnect after a random delay. Load balancers and proxies close idle connections, so the client and server must send periodic heartbeat messages.
| Option | Good at | Bad at | Pick it when |
|---|---|---|---|
| TCP Usual default | Reliable and ordered delivery, handled for you. | Handshake cost, and one lost packet delays everything behind it. | Anything that must arrive complete. It is the base for most other choices. |
| UDP | No handshake and no waiting for lost data. | Loss, duplicates and reordering are the application's problem. | Live audio, video and game state, where late data is useless. |
| HTTP | Works with browsers, caches, CDNs and any language, and it is stateless. | The server cannot push, and JSON is larger than binary formats. | Client-facing and public APIs. |
| gRPC | Compact binary messages, generated clients and streaming. | Needs a proxy for browsers, and long-lived connections balance unevenly. | Calls between your own services. |
| WebSocket | Either side can send at any time over one connection. | Each connection is server state, which complicates scaling and deploys. | Frequent two-way realtime messages, such as chat. |
DNS and TLS
DNS
DNS translates a name such as shop.example.com into an IP address. The client asks a resolver, which asks the domain's authoritative servers if it does not already have the answer. Every answer carries a time to live (TTL), and the browser, the operating system and the resolver all cache it until the TTL runs out. DNS can also return different addresses depending on where the client is, which sends users to the nearest region.
Caching is why DNS changes are slow to take effect. If you change a record, clients that cached the old answer keep using the old address until their copy expires, and some resolvers ignore short TTLs and keep it longer. So DNS is not a good way to fail over from a crashed machine. Point the name at something that stays up, such as a load balancer, and lower the TTL ahead of a planned move.
TLS
TLS encrypts a connection and proves the server's identity with a certificate, so nobody on the path can read or change the traffic. HTTPS is HTTP over TLS. The handshake costs one extra round trip with TLS 1.3 and two with TLS 1.2, and it is repeated for every new connection, which is why reusing connections is cheap and opening new ones is not.
Most designs terminate TLS at the load balancer. The balancer holds the certificates and decrypts the traffic, so the app servers do not need them, and the balancer can read the request in order to route it. Traffic inside the private network is then often plain. If policy requires encryption inside as well, the balancer connects to the servers over TLS again, or services authenticate each other with mutual TLS.
Why the first request from a distant user is slow
Each new connection needs round trips before any data moves: a DNS lookup, the TCP handshake, the TLS handshake and then the request itself. Over a long distance each round trip is slow, and the speed of your servers barely matters. The fixes are to reuse connections, to terminate connections at edge locations close to users, to serve static content from a CDN, and to combine several API calls into one.
In the interview
Choose a protocol from the shape of the traffic. Say whether the data must arrive complete and in order, who starts the messages, and who the caller is, then give the protocol and the reason in one or two sentences. For example: "Clients call a REST API over HTTPS because it works from browsers and is easy to cache, services call each other over gRPC for the typed contracts and smaller messages, and notifications to open apps go over a WebSocket." Nobody expects more depth than this unless they ask.
Questions to expect:
- "TCP or UDP for a video call?" UDP, or a protocol built on it. A lost packet that is resent arrives too late to be played, so waiting for it only adds delay. The application can tolerate a small glitch but not a growing delay.
- "Why not use gRPC for the public API?" Browsers cannot call gRPC directly, and outside developers expect simple, inspectable requests that work with ordinary tools and caches. gRPC is a better fit between your own services, where you control both ends.
- "How would you push updates to a user's phone?" If the app is open and updates are frequent, keep a WebSocket. If updates only flow from the server, server-sent events are simpler. For a closed app, use the phone platform's push notification service.
- "How do you scale WebSocket servers?" Each connection is held by one server, so add servers behind a load balancer and use a publish and subscribe system to route a message to the server holding the recipient. Keep state outside the connection and reconnect clients with a random delay.
- "Why did my DNS change not take effect?" Resolvers and browsers cached the old answer and keep it until the TTL expires. Lower the TTL before a planned change, and do not use DNS alone for failover.
- "Where do you terminate TLS?" At the load balancer, so servers do not manage certificates and the balancer can inspect requests for routing. If the traffic inside the network must also be encrypted, re-encrypt to the servers or use mutual TLS between services.
Which protocols would you use for this chat application?
The app calls a REST API over HTTPS for sign-in, history and settings, because those are ordinary request and reply calls that work from any client. Messages in an open conversation go over a WebSocket so that the server can push them as soon as they arrive.
And between your backend services?
I'd use gRPC. The services are all ours, the calls are frequent, and generated clients keep the message formats consistent. Because gRPC connections are long-lived, I'd put a layer 7 balancer in front or balance on the client so that the calls spread across servers.
What happens when a chat server restarts?
Its WebSocket connections drop. Clients reconnect after a random delay, and since the conversation state is stored outside the connection, they can resume on any server.
A multiplayer game sends each player's position 30 times a second. Which transport fits best?
Your services call each other over gRPC, but one server receives most of the calls from each client. What is the likely cause?
You change a DNS record to a new address, but some users still reach the old one an hour later. Why?
What to practise next
Read Load Balancing to see how requests are spread across servers, and Design a Chat App to choose protocols for a full design.