ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/proxies/tasks/task-12-4-reverse-proxy
TASK

Implementation

Build an HTTP-aware reverse proxy with caching:

  1. Parse incoming HTTP requests
  2. Check cache for valid response
  3. On cache miss, forward to backend
  4. Parse response headers for caching policy
  5. Cache response according to Cache-Control
  6. Return response to client

Support ETag and If-Modified-Since for cache validation.

Sample Test Cases

Cache with max-ageTimeout: 5000ms
Input
{
  "src": "c0",
  "dest": "n1",
  "body": {
    "type": "init",
    "msg_id": 1,
    "node_id": "n1",
    "node_ids": [
      "n1"
    ]
  }
}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
Honor no-storeTimeout: 5000ms
Input
{
  "src": "c0",
  "dest": "n1",
  "body": {
    "type": "init",
    "msg_id": 1,
    "node_id": "n1",
    "node_ids": [
      "n1"
    ]
  }
}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}

Hints

Hint 1
Parse HTTP headers for caching hints
Hint 2
Honor Cache-Control directives
Hint 3
Implement ETag validation
OVERVIEW

Theoretical Hub

HTTP Caching Headers

HTTP provides rich cache control: Cache-Control sets freshness duration, ETag enables validation without full fetch, Vary handles content negotiation. A good proxy respects all of these.

Cache Validation

Instead of fetching a full response, validation asks "has this changed?" using If-None-Match (with ETag) or If-Modified-Since. A 304 Not Modified response confirms the cached copy is still valid.

Key Concepts

reverse proxyHTTP cachingCache-Control
main.py
python
Build Reverse Proxy with Caching - Proxies | Build Distributed Systems