Topic 149 / Advanced
Sidecar and ambassador
Chapter 9.3 · Communication Topologies
What and why
Every service ends up needing the same cross-cutting plumbing — TLS
termination, retries, telemetry, auth token handling — and writing it into every service in
every language duplicates it badly. A sidecar is a second process deployed alongside
each service instance that handles this plumbing out of the service's own code; an
ambassador is a sidecar specialized as an outbound proxy, standing in for a remote
dependency so the service talks to localhost and lets the ambassador handle the real
network trip. Builds on: → 147 (inter-service communication).
Diagram
┌───────────── one host / one pod ─────────────┐
Inbound ───────►│ [Sidecar proxy] ──► [Service code] │
traffic │ (mTLS, auth, │ │
│ inbound metrics) │ localhost call │
│ ▼ │
│ [Ambassador proxy] │
│ (retries, timeouts, │
│ outbound metrics) │
└──────────────────────│───────────────────────┘
▼
remote dependency
How it works
- Deploy the sidecar as a separate process next to each service instance, sharing its
network namespace — the service talks to
localhost, not the real network. - Route inbound traffic through the sidecar first: it terminates TLS, checks auth tokens, and records inbound telemetry before handing the request to the service code.
- Route outbound calls through the ambassador: the service calls
localhost:portas if the dependency were local; the ambassador resolves the real address, applies retries and timeouts (→ 152), and records outbound telemetry. - The service code stays dependency-free of this plumbing — the same sidecar binary runs next to services written in different languages.
- Upgrade the cross-cutting behavior (a new retry policy, a new certificate) by deploying a new sidecar version, without touching or redeploying the service code at all.
Practical example
(illustrative). A payment service written in one language and a
notifications service written in another both get mTLS and retry logic from the same
sidecar binary — neither service's code contains a TLS library call or a retry loop; both
simply call localhost, and the sidecar handles the rest identically for both.
When to use it
- Use when: cross-cutting concerns (security, retries, telemetry) are duplicated across many services, especially across different languages, and keeping them consistent in application code has already proven error-prone.
- Avoid when: there are only a few services, or they're all one language — a shared library handles the same concern with far less operational overhead (one more process per instance, per service).
- Failure scenarios: sidecar version drift across services recreates the same inconsistency it was meant to solve; the sidecar itself becomes added latency and a new failure point on every single call if it's unreliable.
- Simpler alternative: a shared library imported into each service's code — no extra
process, no extra network hop to
localhost, acceptable when all services share a language or runtime.
Practice
(Pattern kind — implement minimal; 20 min, two small processes). Run a
service process that only does business logic, and a second tiny proxy process in front of
it that adds one header (e.g., X-Request-Id) and logs every request before forwarding to
the service over localhost. Confirm the service code never sees or produces that header
itself.
Done when the cross-cutting concern (the header and the log line) exists only in the proxy process, never in the service code.
Key takeaway
Sidecar moves cross-cutting plumbing out of service code into a co-located process; ambassador is the same idea for outbound calls. Done when you can: name three concerns worth moving to a sidecar and explain the extra process/hop cost. Used next in → 150 (service mesh — sidecars, fleet-wide).
References
A pattern language for microservices — see references.md.
Source directory