Microservice Patterns
Sidecar and Ambassador
Run cross-cutting networking — retries, timeouts, circuit breaking, TLS, metrics — in a helper process next to the application, so every service gets it without changing its code. A fleet of sidecars is a service mesh.
What you will learn
- Explain the sidecar and ambassador patterns and how they differ.
- See resilience added to an application without code changes.
- Understand what a service mesh adds and what it costs.
- Decide between a library and a sidecar.
Before you start
Overview
A motorbike sidecar travels with the bike everywhere but is a separate seat with its own job. A diplomat's ambassador speaks to foreign governments on their behalf, handling protocol so the diplomat does not have to.
A sidecar is a helper process deployed alongside each application instance (the same pod or host), sharing its lifecycle. When the sidecar handles outgoing calls on the application's behalf — adding timeouts, retries, circuit breaking, TLS, routing, telemetry — it acts as an ambassador. Deploy one next to every service and control them centrally, and you have a service mesh (Istio, Linkerd).
Run it
A legacy app calls a partner API that throws errors (t = 8–16 s) and then hangs (t = 22–30 s). In B the same app calls localhost and a sidecar applies timeout, retry and a circuit breaker. Compare failures and p95.
What sidecars typically do
- Resilience: timeouts, retries with budgets, circuit breaking, outlier ejection.
- Security: mutual TLS between services, certificate rotation, authorization policies.
- Traffic: canary and blue-green routing, traffic mirroring, fault injection for tests.
- Observability: uniform request metrics and trace spans for every service, in any language.
- Legacy enablement: give an app you cannot change modern capabilities (the ambassador case).
Envoy as an ambassador
clusters:
- name: partner_api
connect_timeout: 0.25s
type: STRICT_DNS
load_assignment:
cluster_name: partner_api
endpoints: [{ lb_endpoints: [{ endpoint: { address: { socket_address: { address: api.partner.com, port_value: 443 } } } }] }]
outlier_detection: { consecutive_5xx: 5, base_ejection_time: 30s } # circuit-breaker-like ejection
circuit_breakers: { thresholds: [{ max_connections: 100, max_pending_requests: 50 }] }
# route config:
# timeout: 0.3s
# retry_policy: { retry_on: "5xx,connect-failure", num_retries: 2, per_try_timeout: 0.3s }Trade-offs
| Benefits | Costs | |
|---|---|---|
| Consistency | Same policies and telemetry for every service and language. | Policies live outside the code — harder to see when reading the service. |
| Change | Update proxies or rules without touching applications. | Another component per instance to upgrade, debug and secure. |
| Cost | Removes duplicated networking code. | Extra CPU/memory per pod and ~1 ms per hop; a mesh control plane to operate. |
Production considerations
- Do not stack retries in both the library and the sidecar — that is a retry storm waiting to happen.
- Start the sidecar before the app and stop it after the app, or early and late requests fail.
- Budget sidecar resources explicitly; a starved proxy slows everything behind it.
- Adopt a full mesh when you have many services and languages; for a few services a library may be enough.