Service Mesh and Sidecars
Moving retries, timeouts, mTLS and telemetry out of application code into proxies next to every service, and what that costs.
What is it?
With microservices, every service talks to many others over the network. Each of those calls needs the same things: service discovery, load balancing, timeouts, retries, encryption, authentication, metrics and tracing. Writing that into every service, in every language, is repetitive and inconsistent.
A service mesh moves this networking logic out of the application and into a proxy that runs next to each service instance, usually as a sidecar container. The app talks plain HTTP to localhost; the proxy handles the hard parts and talks to other proxies.
A mesh has two halves: - Data plane: the proxies (often Envoy) that every request flows through. - Control plane: a central component (for example Istio's istiod) that tells every proxy what routes, policies and certificates to use.
Context: containers and Kubernetes. A container packages an app with its dependencies into an isolated process. Kubernetes runs containers across a cluster. Its smallest unit is a pod: one or more containers that share a network namespace (same IP and localhost). That shared localhost is what makes the sidecar pattern work: a proxy container in the same pod can intercept all of the app container's traffic.
Explain like I'm 10
Every office worker gets a personal assistant who handles all their phone calls: looks up numbers, redials if the line is busy, verifies who's on the other end, and logs every call. The workers just say who they want to talk to. Head office (the control plane) sends all assistants the same rulebook.
Examples
Kubernetes pod with an app and a sidecar proxy
apiVersion: v1
kind: Pod
metadata:
name: orders-7c9f
labels:
app: orders
spec:
containers:
- name: orders # your service: listens on 8080, calls others in plain HTTP
image: registry.example.com/orders:1.42.0
ports:
- containerPort: 8080
- name: istio-proxy # injected automatically by the mesh
image: istio/proxyv2 # Envoy-based proxy
# Both containers share one network namespace (same IP, same localhost).
# iptables rules (set by an init container or CNI plugin) redirect all
# inbound and outbound traffic through the proxy.In practice you don't write the sidecar yourself: labelling a namespace for injection makes the mesh add it to every pod.
Timeouts and retries declared at the mesh (Istio)
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: payments
spec:
hosts:
- payments
http:
- timeout: 3s # total budget for the call, including retries
retries:
attempts: 2
perTryTimeout: 1s
retryOn: connect-failure,refused-stream,unavailable,503
route:
- destination:
host: payments
subset: v1
weight: 90
- destination:
host: payments
subset: v2 # canary: 10% of traffic
weight: 10One config file changes behaviour for every caller of payments, in any language. Only retry idempotent operations, and don't also retry in the app.
Enforcing mutual TLS mesh-wide
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system
spec:
mtls:
mode: STRICT # reject plaintext; every workload proves its identity with a certThe control plane issues short-lived certificates to each workload (SPIFFE-style identities) and rotates them automatically.
How it works
Traffic interception. When a pod starts, iptables rules (set up by an init container or a CNI plugin) redirect its inbound and outbound TCP traffic to the sidecar. The app calls http://payments:8080 as usual; the outbound proxy resolves the destination, picks an endpoint, applies policies and sends the request to the inbound proxy of the target pod, which forwards it to the target app on localhost.
Control plane. Watches the cluster (services, endpoints, config objects like VirtualService) and pushes configuration to every proxy. Envoy receives this via the xDS APIs (listeners, routes, clusters, endpoints, secrets). The control plane is not in the request path: if it goes down, proxies keep running with their last config, but changes and certificate rotation stop.
What the data plane gives you: - mTLS: both sides present certificates, so traffic is encrypted and each service has a verifiable identity. Authorization policies can then say "only orders may call payments". - Resilience: timeouts, retries with budgets, outlier detection (eject endpoints returning errors), circuit breaking via connection and request limits. - Traffic management: weighted routing for canaries, header-based routing, mirroring, fault injection for testing. - Observability: uniform golden-signal metrics (rate, errors, latency) per service pair, access logs, and trace spans. Note: the app must still propagate trace headers from inbound to outbound calls, or traces break into fragments.
Costs. - Latency: two extra proxy hops per call (outbound and inbound), typically around a millisecond or less each at the median, more at the tail and with heavy filters. It adds up across deep call chains. - Resources: a proxy per pod uses CPU and memory; across thousands of pods this is significant. - Operational complexity: another critical system to upgrade, debug and understand. Misconfigured retries or timeouts are now invisible from the application code. - Startup ordering: the app may start before its proxy is ready, or the proxy may exit before the app finishes a job. Kubernetes added native sidecar containers (introduced in 1.28) to manage this lifecycle.
Alternatives and newer designs. Proxyless gRPC uses the same xDS config inside the client library. Sidecarless meshes move work to a per-node component: Istio's ambient mode uses a per-node ztunnel for mTLS and L4, with optional waypoint proxies for L7; Cilium uses eBPF in the kernel plus per-node Envoy. Linkerd keeps sidecars but uses its own lightweight Rust proxy instead of Envoy.
+-------------------------------+
| Control plane (e.g. istiod) |
| config, service discovery, |
| certificates (CA) |
+---------------+---------------+
xDS config | certs
+------------------------+------------------------+
v v
+-----------------------+ +-----------------------+
| Pod: orders | | Pod: payments |
| [app] -> [sidecar] --+==== mTLS, retries ===>+-> [sidecar] -> [app] |
+-----------------------+ metrics, traces +-----------------------+
data plane: every request passes through two proxiesWhy does it exist?
In a large microservice estate, consistent security and reliability can't depend on every team implementing them correctly in every language. Libraries (like Netflix's Hystrix/Ribbon era) solved it per language and needed every service upgraded for each change. A mesh makes these cross-cutting concerns a platform feature: configured centrally, applied uniformly, changed without redeploying applications.
When to use it
- Many services in several languages, where consistent mTLS, retries and telemetry are hard to achieve through libraries. - Zero-trust or compliance requirements to encrypt and authenticate all service-to-service traffic. - Frequent canary and progressive deployments needing fine-grained traffic splitting. - A platform team exists to own and operate the mesh.
When not to use it
- A monolith or a handful of services: a load balancer, a shared client library and TLS at the ingress are far simpler. - Very latency-sensitive paths where extra hops matter, unless you measure and accept the overhead (or use proxyless/sidecarless modes). - Teams without capacity to run it. A mesh you don't understand turns outages into mysteries. - To fix poor service design. Chatty, deeply nested calls stay slow; a mesh just makes them observable.
Common mistakes
Configuring retries in both the mesh and the application, multiplying attempts and amplifying load during incidents.
Retrying non-idempotent requests (POST /payments) at the mesh layer without idempotency keys.
Assuming the mesh gives full distributed tracing automatically; the app must still forward trace context headers.
Rolling out STRICT mTLS everywhere at once, breaking callers that aren't in the mesh yet; use permissive mode during migration.
Ignoring sidecar resource requests and limits, so proxies are throttled or evicted and look like application failures.
Practice exercises
- Easy:
Explain the difference between the control plane and the data plane in a service mesh, and what keeps working if the control plane goes down.
- Medium:
Service A calls B, which calls C. Each layer (app client, mesh, gateway) retries 3 times. Calculate the worst-case number of calls to C from one user request, and propose a better retry policy.
- Hard:
Your company has 300 services in 4 languages and must encrypt and authorize all internal traffic within 6 months. Compare a sidecar mesh, a sidecarless mesh and shared client libraries on latency, cost, rollout risk and operational burden, and recommend a migration plan.
Interview questions
What is a sidecar?
A helper container deployed alongside the application container in the same pod, sharing its network namespace, that adds functionality such as proxying, without changing application code.
What is a service mesh?
An infrastructure layer of proxies handling service-to-service communication (discovery, load balancing, retries, timeouts, mTLS, telemetry), configured by a central control plane.
What's the difference between the control plane and the data plane?
The data plane is the proxies that every request passes through. The control plane computes and distributes their configuration and certificates. The control plane is off the request path.
What is a Kubernetes pod and why does it matter for sidecars?
A pod is the smallest deployable unit: one or more containers scheduled together sharing network and optionally storage. Shared localhost and IP let a sidecar proxy transparently intercept the app's traffic.
What does mTLS give you inside a cluster?
Encryption in transit plus mutual authentication: each workload proves its identity with a certificate, enabling identity-based authorization policies instead of trusting network location.
Why put retries and timeouts in the mesh instead of the application?
Consistency across languages and teams, central changes without redeploys, and uniform metrics. The trade-off is that the mesh doesn't know which operations are safe to retry, so policies must be per route and coordinated with app-level logic.
What are the main costs of a sidecar mesh?
Extra latency from two proxy hops per call, CPU and memory for a proxy in every pod, more moving parts to upgrade and debug, and lifecycle issues like startup and shutdown ordering.
How do retries interact across layers, and how do you prevent retry storms?
Retries multiply: 3 attempts at each of 3 layers can produce 27 calls downstream. Retry at one layer only, use per-try timeouts within an overall deadline, set retry budgets, add jittered backoff, and only retry idempotent or explicitly safe failures.
What is a sidecarless or ambient mesh?
A design that removes per-pod proxies, moving L4 functions like mTLS to a shared per-node component (Istio ztunnel, or eBPF in Cilium) and running L7 proxies only where needed. It reduces resource overhead and lifecycle issues, at the cost of a shared component per node.
When would you advise against adopting a service mesh?
With few services, a single language where a good client library suffices, no platform team to operate it, or strict latency budgets. Start with simpler tools (ingress TLS, shared libraries, observability agents) and adopt a mesh when the cross-language consistency problem is real.