How a request travels the internet
Before your controller runs, a request has already looked up a name, opened a connection, agreed on encryption and crossed a few networks. Knowing what each hop costs tells you where the milliseconds go and which ones you can stop paying twice.
Analogy cheat-sheet: visiting a shop in an unfamiliar city
You want to buy something from a shop you have never visited. First you find its address, then you go to the door, prove who you are, and place your order, often at a local branch rather than the main store.
| In real life | In the system |
|---|---|
| The addresses in your own notebook | The browser and OS DNS cache |
| The library information desk that looks things up for you | The recursive resolver |
| National directory, regional directory, the chain's head office | Root, TLD and authoritative name servers |
| "This address is good for five minutes" | The record's TTL |
| Hello at the door, then a badge check | The TCP and TLS handshakes |
| A small branch near you with popular items on the shelf | A CDN edge |
| The receptionist who sends you to an open counter | The load balancer |
The same ideas as real code
You can watch every hop from a terminal:
# follow the DNS chain yourself: root, then .com, then the domain's own servers
dig +trace example.com
# just the answer; the number in the second column is the TTL left in seconds
dig +noall +answer example.com
# time each phase of one HTTPS request (each value is measured from the start)
curl -so /dev/null https://example.com/ \
-w 'dns %{time_namelookup}s tcp %{time_connect}s tls %{time_appconnect}s first byte %{time_starttransfer}s total %{time_total}s\n'
When your Spring Boot service calls another service, it pays the same handshakes. Create one client and reuse it so only the first call opens a connection:
// one client for the whole application: it keeps connections open and reuses them
private static final HttpClient HTTP = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2) // falls back to HTTP/1.1 if the server can't
.connectTimeout(Duration.ofSeconds(2))
.build();
// wrong: a new client per call can mean new TCP + TLS handshakes every time
HttpResponse<String> r = HttpClient.newHttpClient().send(req, BodyHandlers.ofString());
The JVM also keeps its own DNS cache (see the networkaddress.cache.ttl security property), which matters when the address behind a name changes.
Insight: what each hop costs
| Hop | Rough cost (illustrative) | Paid when | Stop paying it again by |
|---|---|---|---|
| DNS lookup | A few ms cached, 20–100+ ms cold | First visit, or after the TTL runs out | Sensible TTLs; caches in the browser, OS and resolver |
| TCP handshake | 1 round trip | Every new connection | Keep-alive and connection pools |
| TLS handshake | 1 round trip with TLS 1.3, 2 with TLS 1.2 | Every new connection | Keep-alive, session resumption, HTTP/3 |
| Request and response | 1 round trip plus server time | Every request | CDN caching, fewer calls per page |
| Edge to origin | A region-to-region round trip | Every cache miss | Higher hit ratio, warm edge-to-origin connections |
Round trips depend on distance: well under 1 ms inside a data centre, roughly 5–20 ms within a country, about 50–80 ms across a continent and 100–250 ms across an ocean. Light in fibre cannot go faster, so the fix for distance is to move the handshakes closer to the user.
Trade-off: fresh answers or fewer lookups
A long DNS TTL means fewer lookups and less load on name servers, but when you change the address, users keep going to the old one until their caches expire. A short TTL makes changes and failover quick at the cost of more lookups. Connection reuse has a similar tension: keeping connections open saves handshakes, but each idle connection costs memory on the server and on every proxy in between, so servers close them after a while.
How you'll know it's broken in prod
Most of these hops happen before your application sees anything, so your own logs stay quiet. Use synthetic checks from several regions that time DNS, connect, TLS and first byte separately (the curl -w fields above). Alert on certificate expiry weeks ahead. Watch the load balancer's own 5xx count and healthy-host count next to your application's error rate, and the CDN's hit ratio and origin error rate.
Where to look when…
| Symptom | Suspect |
|---|---|
| Some users reach the old server after a move | Cached DNS answers with a long TTL, including the JVM's own cache in calling services. |
| The browser shows a security warning, your logs show nothing | An expired or wrong certificate: TLS failed before any HTTP was sent. |
| 502 or 503 with no matching error in the app logs | The load balancer found no healthy server, or the app closed the connection early. |
| Fast in your office, slow for users abroad | Round trips over long distances. Check where TLS ends and what the CDN caches. |
| Service-to-service calls slow at the start, fine later | New connections per call. Reuse one HTTP client with a connection pool. |
Recap
- DNS turns a name into an address: browser and OS caches, then a recursive resolver, which asks root, TLD and authoritative servers. Answers are cached for their TTL.
- A new HTTPS connection costs a TCP round trip and a TLS round trip (two with TLS 1.2) before the first request.
- A CDN edge near the user shortens those round trips and serves cached files; uncached calls go on to the load balancer and an app server (see How a request travels for what happens inside it).
- Keep-alive, HTTP/2 and HTTP/3 exist to avoid paying the handshakes again.