Sidecar
Escrito por Darjus Kazakevic · revisado el
Write TLS, retries and tracing once, for every language in the fleet.
Every service needs TLS, retries, metrics and service discovery, but none of them is what the service is for. As libraries, they exist once per language and drift apart in version. A sidecar runs them in a second container in the same Kubernetes pod. The app makes a plain call, and the sidecar does the rest.
A service mesh, such as Istio or Linkerd, is a sidecar proxy in every pod plus a control plane that configures them all. The cost is one more process per pod: memory, CPU, and about a millisecond on each hop.
En esencia — Deploy shared plumbing (TLS, retries, metrics) as a separate process beside each service instance, so the app stays simple and the platform team owns it.
Cuándo recurrir a él
Recurre a él cuando
- Services in several languages each reimplement TLS, retries and tracing
- The platform team must update plumbing without touching service code
- You need mutual TLS between all services, with certificates rotated for you
Reconsidéralo cuando
- One service in one language, where a library is cheaper to run
- Latency-critical paths that cannot afford an extra local hop
- A small cluster where running a mesh costs more than it saves
Conceptos clave
- The sidecar container
- A second container in the same pod. It shares the pod's network (localhost) and its lifecycle, so it starts, scales and stops with the app. Not every sidecar is a proxy: log shippers and config reloaders are sidecars too.
- Service mesh
- Sidecar proxies in every pod (the data plane), plus a control plane that pushes routing rules, retry policies and certificates to all of them. Istio uses Envoy as its proxy; Linkerd uses its own Rust proxy.
- Mutual TLS
- Both sides of a connection prove who they are with certificates. The mesh issues and rotates a certificate for each workload, so traffic between services is encrypted with no app code.
- Traffic interception
- The mesh adds iptables rules so the pod's traffic flows through the proxy. The app calls http://payments as usual and does not know the proxy exists. Istio's ambient mode moves this proxy out of the pod.
Reglas de diseño
- Move plumbing out, never business logic
- A sidecar owns what every service needs and none is for: TLS, retries, telemetry, discovery. Logic that differs between services belongs in the service, where its team can read and test it.
- Own each retry and timeout in one layer
- If the sidecar retries 3 times and the app also retries 3 times, one failure becomes 9 calls. Choose the layer that owns retries and timeouts, and remove them from the other.
- Start the sidecar before the app
- If the app starts first, its first calls fail because the proxy is not ready. Use Kubernetes native sidecar containers, or Istio's holdApplicationUntilProxyStarts setting.
- Budget the cost per pod
- Each proxy needs memory and CPU, often 50 MB or more per pod for Envoy. At 2,000 pods that is 100 GB. Measure it before you add a mesh for a single feature.
Errores habituales
- Retries in both the app and the sidecar. During an outage, each failing call becomes nine.
- The app starts before the proxy is ready. The first requests after every deploy fail.
- Business logic in proxy filters (Lua, Wasm). The service team cannot see or test it.
- A full mesh added only to get mTLS for three services, then a quarter spent learning to run it.