When a phone calls an API, the request crosses several layers before any code runs: a name becomes an address (DNS), a reliable connection is opened (TCP), the connection is secured (TLS), and finally the HTTP request is sent. Each layer adds delay and can fail. System design interviews rarely ask you to configure a network, but they expect you to know what happens, why the first request to a service is slower than later ones, and why placing servers near users matters.
Detailed Notes
What Is It?
Computers on a network find each other with an IP address, a number such as 93.184.216.34 (IPv4) or a longer hexadecimal form (IPv6). An IP address identifies a machine; a port (a number from 0 to 65535) identifies which program on that machine should receive the data. HTTP normally uses port 80 and HTTPS uses port 443.
Humans prefer names, so DNS (Domain Name System) is a distributed directory that translates a name like api.example.com into an IP address.
Data travels in small chunks called packets. Two transport protocols decide how packets are handled:
TCP (Transmission Control Protocol) sets up a connection first, numbers the packets, re-sends lost ones and delivers bytes in order. It is reliable but needs a setup round trip.
UDP (User Datagram Protocol) just sends packets with no setup, no re-sending and no ordering. It is faster and lighter but unreliable. Video calls and games use it when a late packet is worthless anyway.
HTTP runs on top of TCP and carries requests and responses (previous lesson). TLS (Transport Layer Security) encrypts the connection; HTTP over TLS is HTTPS.
One more word: a round trip is one message to the other side and the answer back. Round-trip time (RTT) depends mostly on distance, because signals travel at a finite speed. A data-center-to-data-center RTT is about 0.5 ms; across an ocean it is about 150 ms. You cannot make light faster, so the only fix for distance is moving things closer.
Why Do We Need It?
Latency is the sum of many small waits, and most of them are network round trips. Knowing the steps lets you explain things interviewers love to ask about:
Why does the first page view feel slower than the second? (DNS lookup, TCP and TLS setup are paid once, then connections are reused.)
Why use a CDN (content delivery network, servers around the world that keep copies of content close to users)? To cut the distance and therefore the RTT.
Why does a design with ten sequential calls between services feel slow? Ten round trips add up.
Why is HTTPS everywhere? Without TLS anyone on the path can read or modify the traffic.
How Does It Work?
Step 1: DNS resolution
Before connecting, the browser needs an IP address. It asks a DNS resolver, a server run by the internet provider or a public service that does the searching on the client's behalf. If the resolver has not seen the name recently, it asks the hierarchy: a root server points to the server for the top-level domain (.com), which points to the authoritative server that owns example.com, which finally gives the IP.
DNS Resolution: Name to IP AddressRequest flow
Key point. A cold lookup can take dozens of milliseconds; a cached one takes almost none.
The resolver does the work and caches the answer for its TTL, so later lookups skip steps 2-7.
Answers carry a TTL (time to live): how long anyone may cache the answer. Caching at the browser, the operating system and the resolver means most lookups never leave the machine and cost almost nothing. A short TTL (say 60 seconds) lets you move traffic quickly during a failure; a long TTL (a day) reduces lookups but delays changes. DNS can also return different IPs for different users (to send them to the nearest region) or several IPs for one name (a crude form of spreading load).
Step 2: TCP handshake
With the IP known, the client opens a TCP connection through a three-way handshake: it sends SYN ("can we talk?"), the server answers SYN-ACK ("yes, can you hear me?"), and the client sends ACK ("yes"). That costs one full RTT before any data moves.
Step 3: TLS handshake
For HTTPS, client and server then agree on encryption: the server presents a certificate (proof of identity signed by a trusted authority), and both sides derive a shared secret key. Modern TLS 1.3 needs one extra RTT (older TLS 1.2 needed two). From then on all data is encrypted.
Step 4: HTTP request and response
Finally the HTTP request travels and the response returns, one more RTT plus server time.
TCP + TLS + HTTP: Setting Up a ConnectionRequest flow
ClientServer1SYNTCP handshake, round trip 12SYN-ACK3ACK + ClientHelloTLS handshake, round trip 24certificate + key share5Finished + GET /indexencrypted HTTP, round trip 36200 OK + body
Key point. Three round trips pass before the first byte of the response. Connection reuse removes two of them.
Each arrow pair is one round trip. A reused connection skips the first four.
Adding up a first request to a server 150 ms away: DNS (maybe 30 ms) + TCP 150 ms + TLS 150 ms + HTTP 150 ms = about 480 ms before the first byte of useful data, even if the server answers instantly. This is why connection reuse matters. HTTP keep-alive keeps the TCP and TLS connection open for further requests, so later requests pay only 150 ms. HTTP/2 also allows many requests at once on one connection. Connection pooling does the same between your own services: a service keeps a set of open connections to its database instead of reconnecting each time.
Useful rules of thumb
Prefer fewer round trips: batch requests, return needed data in one response, avoid chatty call chains.
Keep data and users close: regional deployments, CDNs, edge caches.
Use UDP-based transport only when late data is useless (live video, voice, games); use TCP otherwise.
Terminate TLS at the edge (a load balancer, next lesson), so internal servers skip the heavy work.
Interview tip. Common mistakes: treating the network as instant and free, forgetting DNS and handshakes in latency math, saying "UDP is faster so use it" without noting it loses data, and not mentioning HTTPS for anything with logins or payments.
Interview discussion. "A user in Mumbai calling servers in Virginia pays about 200 ms per round trip. Even with fast code, three sequential calls cost 600 ms. So I'll serve static content from a CDN and deploy the API in a nearby region."
Real-World Example
A shopper in Sydney opens a store hosted only in Oregon. The first load needs DNS, TCP and TLS setup, each costing about 150 ms RTT, so half a second passes before the page starts. Every image is another request. After the company puts a CDN in front (images served from a Sydney edge) and enables keep-alive, images arrive in tens of milliseconds and the page feels local, though the checkout call still travels to Oregon.
System Design Example
A video call feature: signaling (who is calling whom, which devices) is a small reliable exchange, so it uses HTTPS or WebSocket over TCP. The actual audio and video stream uses UDP because a packet that arrives 300 ms late is useless and re-sending it would only delay newer packets. Mentioning this split shows you pick the protocol from the requirement rather than by habit.
A second example: your service calls a database 2,000 times per minute. Opening a new TCP connection each time wastes at least one RTT plus handshake CPU. A connection pool of, say, 20 reused connections removes that cost.
Trade-off thinking. Encryption costs CPU and a round trip, but skipping it is almost never acceptable for user data. UDP gains speed but moves the burden of loss handling to your code. Short DNS TTLs give agility but more lookups. State which side you pick and why.
Key Interview Takeaway
A request is DNS, then TCP, then TLS, then HTTP, and every step costs round trips over distance. Reuse connections, cut round trips, and move content close to users with CDNs and regional deployments. Use TCP by default and UDP only when late data is worthless.