ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/loadbalancers/tasks/task-20-2-1-http-proxy
TASK

Implementation

Layer 7 load balancing operates at the HTTP application layer, inspecting headers and URL paths to make routing decisions. Unlike Layer 4 (TCP), L7 proxies understand HTTP semantics.

HTTP proxy architecture:

Client → L7 Proxy → Backend
  - Parse HTTP request
  - Inspect headers (Host, User-Agent, Cookies)
  - Inspect URL path (/api/users, /static/images)
  - Select backend based on rules
  - Forward request and return response

Request flow:

1. Client sends: GET /api/users HTTP/1.1
   Host: example.com

2. Proxy parses:
   - Method: GET
   - Path: /api/users
   - Host: example.com

3. Proxy routes to backend pool "api"

4. Backend responds: HTTP/1.1 200 OK
   {"users": [...]}

5. Proxy returns response to client

Example request:

Request:  {"type": "http_request", "msg_id": 1, "method": "GET", "path": "/api/users", "headers": {"Host": "example.com", "User-Agent": "Mozilla"}}
Response: {"type": "http_response", "in_reply_to": 1, "status": 200, "headers": {"Content-Type": "application/json"}, "body": "{"users": [{"id": 1, "name": "Alice"}]}", "backend": "api-1"}

Backend pool configuration:

{
  "backend_pools": {
    "api": {
      "backends": ["api-1:8080", "api-2:8080", "api-3:8080"],
      "algorithm": "round-robin"
    },
    "static": {
      "backends": ["static-1:8080", "static-2:8080"],
      "algorithm": "least-connections"
    }
  }
}

Sample Test Cases

Route HTTP GET to correct backendTimeout: 5000ms
Input
{
  "src": "client",
  "dest": "l7_proxy",
  "body": {
    "type": "http_request",
    "msg_id": 1,
    "method": "GET",
    "path": "/api/users",
    "headers": {
      "Host": "example.com"
    }
  }
}
Expected Output
{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 200, "backend": "api-1"}}
Route based on URL pathTimeout: 5000ms
Input
{
  "src": "client",
  "dest": "l7_proxy",
  "body": {
    "type": "http_request",
    "msg_id": 1,
    "method": "GET",
    "path": "/static/logo.png",
    "headers": {
      "Host": "example.com"
    }
  }
}
Expected Output
{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 200, "backend": "static-1"}}

Hints

Hint 1
Parse incoming HTTP requests to extract Host header and URL path
Hint 2
Maintain a backend pool with health status
Hint 3
Select a backend based on routing rules
Hint 4
Forward the request to the backend and return the response
Hint 5
Handle connection pooling and keep-alive
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

layer 7 load balancingHTTP proxyrequest routingheader inspectionbackend selection
main.py
python
Implement Layer 7 HTTP Proxy - Load Balancers | Build Distributed Systems