ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/loadbalancers/tasks/task-14-4-layer7
TASK

Implementation

Build an HTTP-aware (Layer 7) load balancer:

  1. Parse HTTP request (method, path, headers)
  2. Route based on Host header (virtual hosts)
  3. Route based on URL path (/api/* -> api-servers)
  4. Implement session stickiness via cookie
  5. Support URL rewriting

Layer 7 enables intelligent routing based on request content.

Sample Test Cases

Path-based routingTimeout: 5000ms
Input
{"src":"c0","dest":"lb","body":{"type":"init","msg_id":1,"node_id":"lb","node_ids":["lb","s1","s2","s3"]}}
{"src":"client","dest":"lb","body":{"type":"http_request","msg_id":2,"path":"/api/users","method":"GET"}}
Expected Output
{"src":"lb","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"lb","dest":"client","body":{"type":"http_response","in_reply_to":2,"msg_id":1,"path":"/api/users","routed_to":"s1","status":200}}

Hints

Hint 1
Parse HTTP headers
Hint 2
Route based on path or host
Hint 3
Support session stickiness
OVERVIEW

Theoretical Hub

Layer 4 vs Layer 7

Layer 4 LBs see only TCP/UDP - fast but limited. Layer 7 LBs understand HTTP - can route by URL, read cookies, modify headers. More powerful but higher overhead.

Session Stickiness

Some apps require all requests from a user to reach the same server (sessions). The LB can track sessions via cookies or source IP. Trade-off: stickiness can cause uneven load.

Key Concepts

Layer 7HTTP routingcontent-based
main.py
python
Build Layer 7 Load Balancer - Load Balancers | Build Distributed Systems