Final challenge: follow a request and time it
After this: Count lookups, predict a handler, add up a visit, and work out one server's limit.
This challenge has five steps, and each step is scored on its own. You will count address lookups when the answer is remembered, predict what the request handler prints, add up a first visit, work out one server's limit, and follow its waiting line. The step with code shows the program in Python and in Java; the answer is the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Count the lookups
Count the lookups
The browser keeps each DNS answer for 120 seconds. You open the site at 0 s, 100 s, 150 s and 300 s. How many times does the browser ask DNS?
Predict the handler
The handler from lesson 3 gets two requests. What does the program print?
products = {"42": "Desk lamp", "7": "Blue mug"}
def handle(method, path): if method == "GET" and path.startswith("/products/"): product_id = path[len("/products/"):] if product_id in products: return 200, products[product_id] return 404, "Not found"
status, body = handle("GET", "/products/7")print(status, body)status, body = handle("GET", "/products/42/reviews")print(status, body)import java.util.Map;
class Response { int status; String body;
Response(int status, String body) { this.status = status; this.body = body; }}
public class Main { static Map<String, String> products = Map.of("42", "Desk lamp", "7", "Blue mug");
static Response handle(String method, String path) { if (method.equals("GET") && path.startsWith("/products/")) { String productId = path.substring("/products/".length()); if (products.containsKey(productId)) { return new Response(200, products.get(productId)); } } return new Response(404, "Not found"); }
public static void main(String[] args) { Response r1 = handle("GET", "/products/7"); System.out.println(r1.status + " " + r1.body); Response r2 = handle("GET", "/products/42/reviews"); System.out.println(r2.status + " " + r2.body); }}Time a first visit
A first visit: DNS lookup 20 ms, one round trip to the server 30 ms, three database queries of 8 ms each, and 4 ms of work on the server. How long does the visit take?
One server's limit
A server has 5 workers, and each request takes a worker 20 ms. How many requests can it finish per second?
The waiting line
The server from the last step finishes 250 per second. For 4 seconds, 300 arrive every second. How many are waiting at the end, and about how much longer does a request that arrives then wait?
- Count address lookups with a remembered answer, predict what a handler prints, add up a first visit, and work out one server's limit and its waiting line.