Microservices Patterns

May 30, 2026 | 5 min read

Service Discovery

def discover_service(name: str, registry: dict) -> str:
    endpoints = registry.get(name, [])
    # simple round-robin
    idx = hash(time.time()) % len(endpoints)
    return endpoints[idx]

Load Balancing

def weighted_random(weights: list[float]) -> int:
    total = sum(weights)
    r = random.random() * total
    cumsum = 0.0
    for i, w in enumerate(weights):
        cumsum += w
        if r <= cumsum:
            return i
    return len(weights) - 1

Inter-Service Communication

def call_service(endpoint: str, payload: dict) -> dict:
    # HTTP client logic remains in CPython
    response = http_post(endpoint, payload)
    # Result processing is compiled
    return compiled_validate(response)