3. Client and Server
Practice: five levels
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Pick the method and the success status code.
- Show a user's order history.
- Place a new order.
- Replace a user's saved address with a new one.
- Remove a book from the wishlist.
- Change only the quantity of one cart item.
B. Which status code should the server return?
- The request body isn't valid JSON.
- The user isn't logged in.
- The user is logged in but tries to open another user's order.
- The client sent 500 requests in one second.
- The load balancer waited 30 seconds and the app server never answered.
Level 2: Calculate
A user in Mumbai talks to a server in the US. The round trip is about 200 ms (assumed).
- How long before the first byte of the response on a brand-new HTTPS connection (TCP + TLS 1.3)?
- On a reused connection?
- On a brand-new HTTP/3 connection?
- The app makes 20 API calls one after another. How long does it take if every call opens a new connection, vs if they all reuse one connection?
- The company adds a server in Mumbai with a 20 ms round trip. Redo question 4 for new connections and for reuse.
Level 3: Build
Using only Python's standard library (http.server), build a tiny bookstore API:
GET /books/{id}returns200with the book as JSON, or404if it doesn't exist.POST /cartwith{"book_id": "..."}returns201and the new cart size;400for an unknown book or a body that isn't valid JSON.- Any other method (like
DELETE /books/42) returns405.
Then write a client with urllib.request and check all of the above.
Level 4: Debug
The bookstore runs two app servers behind a round-robin load balancer. Users complain: "I keep getting logged out every other click."
- Find the bug. Explain step by step what happens on the user's first four clicks after logging in.
- Fix it. Give two different fixes.
Level 5: Design
The bookstore now has a website and a mobile app, 10 app servers, and plans to split into a few services (catalogue, cart, payments).
Deliver:
- The main API endpoints for browsing books and managing the cart (method, path, success code).
- How you keep users logged in: sessions or tokens? Where is each piece stored, on web and on mobile?
- How a user is logged out everywhere after a password change.
- Two ways you'd cut latency for users far from your servers.
- Which requests the app may safely retry automatically after a timeout, and which it must not.