Synchronous Communication
Introduction
Communication Patterns Overview
Pattern
Use Case
Trade-offs
HTTP/REST Communication
Basic HTTP Client
import httpx
from typing import TypeVar, Generic
from pydantic import BaseModel
from dataclasses import dataclass
T = TypeVar("T")
@dataclass
class ServiceConfig:
base_url: str
timeout: float = 30.0
max_retries: int = 3
class HTTPServiceClient:
"""Base HTTP client for service-to-service communication."""
def __init__(self, config: ServiceConfig):
self.config = config
self.client = httpx.AsyncClient(
base_url=config.base_url,
timeout=httpx.Timeout(config.timeout),
)
async def get(self, path: str, params: dict | None = None) -> dict:
response = await self.client.get(path, params=params)
response.raise_for_status()
return response.json()
async def post(self, path: str, data: dict) -> dict:
response = await self.client.post(path, json=data)
response.raise_for_status()
return response.json()
async def put(self, path: str, data: dict) -> dict:
response = await self.client.put(path, json=data)
response.raise_for_status()
return response.json()
async def delete(self, path: str) -> None:
response = await self.client.delete(path)
response.raise_for_status()
async def close(self):
await self.client.aclose()
# Usage
class UserServiceClient(HTTPServiceClient):
"""Client for User Service."""
async def get_user(self, user_id: str) -> dict:
return await self.get(f"/users/{user_id}")
async def create_user(self, email: str, name: str) -> dict:
return await self.post("/users", {"email": email, "name": name})
# In Order Service
user_client = UserServiceClient(
ServiceConfig(base_url="http://user-service:8000")
)
user = await user_client.get_user("123")Request/Response Correlation
Error Handling in Service Calls
gRPC for High-Performance Communication
Protocol Buffers Definition
gRPC Server Implementation
gRPC Client
REST vs gRPC Comparison
Aspect
REST/HTTP
gRPC
Service Discovery
Client-Side Discovery
DNS-Based Discovery
Load Balancing
Client-Side Load Balancing
Health-Aware Load Balancing
Practical Patterns
Timeout and Deadline Propagation
Request Hedging
Practical Exercise
Exercise: Build a Resilient Service Client
Key Takeaways
What's Next?
Last updated