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 responseRequest 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 clientExample 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
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()