Card 06/ 11
Following One Request From Browser to Database
Six hops, each one a part from the map, and the response coming back.
One request for a product page passes through the parts of a web system in a fixed order: DNS, the load balancer, an application server, the cache, the database, and back. Following it hop by hop shows where the time goes, and which part to look at when a page is slow.
The setting. A reader in the same country as the data centre opens shop.example.com/products/42 for the first time today. The times inside the data centre come from Colin Scott's 2020 latency figures. The times across the internet are typical assumptions for a home connection, not measurements, and vary widely.
One request for a product page, with a running total
1. DNS lookup
The browser has no cached address for shop.example.com, so it asks its resolver, which answers from its own cache. About 20 ms, assumed. Running total: 20 ms. On the reader's next visit within the TTL, this hop costs nothing.
2. Connect to the load balancer
The browser opens a secure connection to the address it got, which belongs to the load balancer, and sends the request. A round trip across the country is about 20 ms, assumed, and setting up the connection takes a couple of them. About 40 ms. Running total: 60 ms.
3. Forward to an application server
The load balancer picks one of the application servers and passes the request on, inside the data centre. About 0.5 ms. Running total: 60.5 ms.
4. Check the cache
The server asks the cache for product:42. Product 42 has not been read in the last five minutes, so the cache answers that it has nothing: a miss. One round trip, about 0.5 ms. Running total: 61 ms.
5. Query the database
On the miss, the server queries the database for the product, then stores the answer in the cache for the next reader. A round trip plus the query itself, a few milliseconds for a lookup by key; say 3 ms. Running total: 64 ms.
6. Return the response
The server builds the page, and it travels back through the load balancer to the browser, another trip across the country. About 20 ms. Running total: about 85 ms.
Where the time went. Of about 85 ms, roughly 80 were spent between the browser and the data centre, and about 5 inside it. That is typical: the distance to the reader dominates, which is why CDNs exist and why a request that crosses an ocean is slow however fast the code is.
What changes on the next request. A second reader opening the same page within five minutes skips the DNS lookup if their resolver has it cached, and gets a cache hit at step 4, so step 5 disappears. A reader whose page needs 20 database queries instead of one adds 20 round trips at step 5, which is how a fast page becomes a slow one without any single query being slow.
So when a page is slow, walk its request through the six hops and put a time on each: the slow hop is usually the one that crosses the most distance or repeats the most times.
- System Design
- Networking
- Performance
← → move through sections and cards