The Plain Picture#
Phoning a busy office: the dial, the ringing, the "please hold" menu takes 30 seconds before you can speak. If you have 20 questions, you don't hang up and redial for each one. You ask all 20 on one call.
The Real Names#
Latency = how long one trip takes. Bandwidth = how much data per second the link can carry. A fibre link to the USA can have huge bandwidth and still ~220 ms latency.
Connection reuse / keep-alive (HTTP): keeping one TCP connection open for many requests instead of opening a new one each time.
A connection pool is a set of already-open connections that a program keeps ready and lends out, e.g. 10 open connections to the database shared by all request handlers.
Under the Hood#
Counting round trips for one HTTPS request from Bangalore to a US server (RTT ≈ 220 ms)#
new connection every time reused connection
───────────────────────── ─────────────────
TCP handshake 1 RTT 220 ms (already open)
TLS encryption setup 1 RTT 220 ms (already done)
request + response 1 RTT 220 ms request + response 1 RTT 220 ms
────────────── ──────────────
≈ 660 ms ≈ 220 ms(TLS is explained in Networking 3: DNS, HTTP and HTTPS; with TLS 1.3 its setup is one round trip.)
The same connection reuse is 3× faster — and that's before counting slow start (Ch 7), which makes a new connection slower still for bigger responses.
Connection pools — the backend version#
Opening a PostgreSQL connection costs a TCP handshake, often TLS, plus login and setting up a server-side process — often several milliseconds. A FastAPI server handling 500 requests per second can't afford that per request, and the database can't handle thousands of connections either.
request handlers POOL (e.g. 10 open connections) database ┌──────────┐ borrow ┌─────┬─────┬─────┬─────┬─────┐ │ handler 1│ ─────────────────────▶ │conn │conn │conn │ ... │conn │ ═══════▶ PostgreSQL │ handler 2│ ◀── give back ───────── └─────┴─────┴─────┴─────┴─────┘ │ ... │ └──────────┘
You'll configure exactly this in FastAPI with Databases (coming soon) and see why a pool that's too small makes requests queue up, and one that's too big overloads the database.
The latency-bandwidth formula#
A rough estimate for how long a transfer takes:
time ≈ (round trips × RTT) + (size ÷ bandwidth)A 1 MB download from the US on a 100 Mbps line: size ÷ bandwidth = 1 MB ÷ 12.5 MB/s = 80 ms, but the round trips (handshake + TLS + slow start ramp + request) easily add 700 ms or more. For small and medium transfers, latency dominates, not bandwidth. That's why CDNs (CDN and Blob Storage (coming soon)) put copies of data physically close to users.
Check yourself
4 questionsWhat's the difference between latency and bandwidth?
Show answerHide answer
Latency is how long one trip takes; bandwidth is how much data per second the link carries.
Why does reusing a connection make an HTTPS request to the US ~3× faster?
Show answerHide answer
It skips the TCP handshake and TLS setup — two of the three round trips.
What is a connection pool and why do backends use one for databases?
Show answerHide answer
A set of already-open connections shared by request handlers; it avoids paying connection setup on every request and limits how many connections hit the database.
For a 50 KB API response from far away, which matters more: latency or bandwidth?
Show answerHide answer
Latency — the transfer itself is tiny; the round trips dominate.