ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/orchestrator/tasks/task-26-2-1-service-mesh
TASK

Implementation

A service mesh adds a sidecar proxy to every service. All traffic flows through these proxies, which transparently handle service discovery, retries, circuit breaking, and distributed tracing without any application code changes.

Implement a node that simulates sidecar proxy behaviour:

// Sidecar intercepts a request and adds tracing context
{ "type": "call", "msg_id": 1, "path": "/api/users/123", "proxy": true }
-> { "type": "proxied", "in_reply_to": 1,
    "proxied_by": "sidecar-a", "trace_id": "<uuid>" }

// Discover healthy instances of a service
{ "type": "discover", "msg_id": 2, "service": "service-b" }
-> { "type": "discovered", "in_reply_to": 2,
    "service": "service-b",
    "instances": [{"host": "10.0.1.1", "port": 8080},
                  {"host": "10.0.1.2", "port": 8080}] }

// Circuit breaker opens after too many failures
{ "type": "call", "msg_id": 3,
  "force_failures": 6, "circuit_breaker": true }
-> { "type": "circuit_breaker_open", "in_reply_to": 3,
    "service": "service-b", "reason": "Too many failures" }

Every request proxied through the sidecar must receive a unique trace_id that can be propagated to downstream services for end-to-end tracing.

Sample Test Cases

Sidecar proxy intercepts trafficTimeout: 5000ms
Input
{
  "src": "service-a",
  "dest": "service-b",
  "body": {
    "type": "call",
    "msg_id": 1,
    "path": "/api/users/123"
  },
  "proxy": true
}
Expected Output
{"type": "proxied", "in_reply_to": 1, "proxied_by": "sidecar-a", "trace_id": ".*"}
Service discovery routes to instancesTimeout: 5000ms
Input
{
  "src": "sidecar",
  "dest": "registry",
  "body": {
    "type": "discover",
    "msg_id": 1,
    "service": "service-b"
  }
}
Expected Output
{"type": "discovered", "in_reply_to": 1, "service": "service-b", "instances": [{"host": "10.0.1.1", "port": 8080}, {"host": "10.0.1.2", "port": 8080}]}

Hints

Hint 1
A sidecar proxy intercepts all inbound and outbound traffic for its service
Hint 2
discover queries the service registry for healthy instances of a named service
Hint 3
Add a trace_id to every proxied request so traces can be correlated across services
Hint 4
Circuit breaker opens when failure count exceeds the threshold; fail immediately while open
Hint 5
Retry with exponential backoff: first retry after base_ms, second after base_ms*2, etc.
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

service meshsidecar proxyservice discoverycircuit breakerretrydistributed tracing
main.py
python
Implement Service Mesh Architecture - The Orchestrator | Build Distributed Systems