Course outline · 0% complete

0/25 lessons0%

Course overview →

Latency, bandwidth, and round trips

lesson 3-4 · ~11 min · 9/25

Why fast servers still feel slow

You now know that opening a TCP connection costs a full back-and-forth before any data moves. This lesson names the costs, because they explain a mystery every engineer eventually hits: a server that responds in 2 ms can still feel slow from the other side of the world, and no amount of server power fixes it. Understanding why is how you avoid optimizing the wrong thing.

Two different measurements get mixed up constantly:

  • Latency is the time one message takes to reach the other machine, set mostly by physical distance and the number of routers on the way. Measured in milliseconds (ms).
  • Bandwidth is the volume a connection can carry per second, like the width of a pipe. Measured in bits or bytes per second.

A round trip is one message over and one reply back, and its duration is the RTT (round-trip time). Typical RTTs: a few ms within a city, ~70 ms across a continent, 150–250 ms between continents.

Here is the punchline: before a browser receives the first byte of a page, it spends several sequential round trips — the DNS lookup, the TCP handshake (lesson 3-2), the TLS handshake (unit 7), and finally the request itself. Each one must finish before the next starts, so the waiting adds up multiplicatively with distance.

Code exercise · python

Run this. It computes the minimum time before the first byte arrives, for the same 4-round-trip setup at three distances. Nothing about the server changed between the lines — only the distance did.

Quiz

Your API server answers every request in 2 ms of processing time, yet users in Sydney (RTT 200 ms to your server) wait about 800 ms for the first response. Why?

When bandwidth matters, and when it does not

A useful mental formula for one download:

total time ≈ (setup round trips × RTT) + (size ÷ bandwidth)

The first term is latency's share, the second is bandwidth's. For a small request — an API call returning 2 KB of data — the transfer part is nearly zero and latency is everything. For a 2 GB video, the transfer term dominates and bandwidth is what you would pay to improve. Diagnosing "slow" starts with asking which term you are actually in.

This is also why HTTP clients reuse connections. Keep-alive (the default in HTTP/1.1) keeps the TCP+TLS connection open after a response so the next request to the same server skips both handshakes and pays only one round trip. Browsers go further and issue independent requests in parallel rather than queueing them.

Code exercise · python

Your turn. Compute the total time for three downloads over a connection with 100 ms RTT, 3 setup round trips, and 10 KB-per-millisecond bandwidth (that is 10 MB/s). For each size: transfer = size_kb // kb_per_ms, total = setup + transfer, printed exactly as shown. Notice which term dominates at each size.

Quiz

A dashboard page makes 5 API calls, each waiting for the previous one to finish, to a server with RTT 200 ms. Ignoring transfer time, what is the minimum wait, and what is the standard fix?