Lesson 14 of 18
3. Client and Server

Practice: five levels

3 min read+10 XP

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.

  1. Show a user's order history.
  2. Place a new order.
  3. Replace a user's saved address with a new one.
  4. Remove a book from the wishlist.
  5. Change only the quantity of one cart item.

B. Which status code should the server return?

  1. The request body isn't valid JSON.
  2. The user isn't logged in.
  3. The user is logged in but tries to open another user's order.
  4. The client sent 500 requests in one second.
  5. 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).

  1. How long before the first byte of the response on a brand-new HTTPS connection (TCP + TLS 1.3)?
  2. On a reused connection?
  3. On a brand-new HTTP/3 connection?
  4. 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?
  5. 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} returns 200 with the book as JSON, or 404 if it doesn't exist.
  • POST /cart with {"book_id": "..."} returns 201 and the new cart size; 400 for an unknown book or a body that isn't valid JSON.
  • Any other method (like DELETE /books/42) returns 405.

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."

python
  1. Find the bug. Explain step by step what happens on the user's first four clicks after logging in.
  2. 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:

  1. The main API endpoints for browsing books and managing the cart (method, path, success code).
  2. How you keep users logged in: sessions or tokens? Where is each piece stored, on web and on mobile?
  3. How a user is logged out everywhere after a password change.
  4. Two ways you'd cut latency for users far from your servers.
  5. Which requests the app may safely retry automatically after a timeout, and which it must not.