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 lifeIn the system
The addresses in your own notebookThe browser and OS DNS cache
The library information desk that looks things up for youThe recursive resolver
National directory, regional directory, the chain's head officeRoot, TLD and authoritative name servers
"This address is good for five minutes"The record's TTL
Hello at the door, then a badge checkThe TCP and TLS handshakes
A small branch near you with popular items on the shelfA CDN edge
The receptionist who sends you to an open counterThe 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

HopRough cost (illustrative)Paid whenStop paying it again by
DNS lookupA few ms cached, 20–100+ ms coldFirst visit, or after the TTL runs outSensible TTLs; caches in the browser, OS and resolver
TCP handshake1 round tripEvery new connectionKeep-alive and connection pools
TLS handshake1 round trip with TLS 1.3, 2 with TLS 1.2Every new connectionKeep-alive, session resumption, HTTP/3
Request and response1 round trip plus server timeEvery requestCDN caching, fewer calls per page
Edge to originA region-to-region round tripEvery cache missHigher 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…

SymptomSuspect
Some users reach the old server after a moveCached DNS answers with a long TTL, including the JVM's own cache in calling services.
The browser shows a security warning, your logs show nothingAn expired or wrong certificate: TLS failed before any HTTP was sent.
502 or 503 with no matching error in the app logsThe load balancer found no healthy server, or the app closed the connection early.
Fast in your office, slow for users abroadRound trips over long distances. Check where TLS ends and what the CDN caches.
Service-to-service calls slow at the start, fine laterNew connections per call. Reuse one HTTP client with a connection pool.

Recap