Nov 20, 20265 min read

The Sidecar Pattern: When to Use It and When Not To

What the sidecar pattern is, the jobs sidecars usually do in Kubernetes, native sidecar containers, the costs in memory, startup ordering and upgrades, and the alternatives: libraries in the process, node agents and sidecar-less meshes.

Illustration for The Sidecar Pattern: When to Use It and When Not To

A sidecar is a second container that runs next to your application in the same pod and adds something the application does not do itself, such as a proxy for mTLS and retries or an agent that ships logs.

It became the default way to bolt platform features onto services without touching their code, and that convenience costs something in every pod, with newer options making the pattern optional for many of its original jobs.

What sidecars usually do

JobTypical sidecarWhat it gives the app
Traffic management and mTLSEnvoy in a service meshRetries, timeouts, encryption, traffic metrics
Log shippingFluent BitLogs forwarded without app changes
SecretsVault AgentSecrets written to a shared volume and renewed
Database accessCloud SQL Auth ProxyIAM-authenticated, encrypted connections
Runtime APIsDaprPub/sub, state and bindings over a local HTTP API

The sidecar pattern description in the Azure Architecture Center frames it as isolation and encapsulation: the helper can be written in another language and maintained by another team, and the app talks to it over localhost or a shared volume.

Native sidecars in Kubernetes

For years a sidecar was a second entry in containers, which caused two known problems: a Job never completed because the proxy kept running, and the proxy could stop before the app during shutdown and drop in-flight requests. Native sidecar containers, stable since Kubernetes 1.33, fix both by declaring the sidecar as an init container that keeps running:

apiVersion: v1 kind: Pod metadata: name: api spec: initContainers: - name: log-shipper image: fluent/fluent-bit:3.2 restartPolicy: Always # makes it a sidecar: starts first, stops last volumeMounts: - { name: logs, mountPath: /var/log/app } containers: - name: api image: example/api:1.4.0 volumeMounts: - { name: logs, mountPath: /var/log/app } volumes: - name: logs emptyDir: {}

Kubernetes starts the sidecar before the app containers, does not count it when deciding whether a Job finished, and terminates it after the app containers on shutdown.

What sidecars cost

The costs are multiplied by the number of pods, which is easy to underestimate:

  • Resources. A proxy reserving 100m CPU and 128 MiB per pod adds up to whole nodes across a few hundred pods.
  • Latency. Every request through a proxy sidecar takes extra hops through userspace on both ends.
  • Upgrades. A new proxy version means restarting every pod that carries it.
  • Debugging. Timeouts, retries and TLS now happen in a process the application team did not write and may not have logs for.

These costs are why meshes moved away from per-pod proxies: Istio's ambient mode handles mTLS in a per-node component and only adds a proxy where layer 7 features are needed.

Alternatives

  • Node agents. A DaemonSet runs one log shipper or metrics agent per node instead of per pod.
  • In-process libraries. OpenTelemetry SDKs, cloud SDKs with workload identity and HTTP clients with retries do the same jobs inside the app, with no extra container.
  • Platform features. Managed load balancers, managed certificates and cloud IAM replace the proxy and secrets agent for many setups.

Best fit: sidecars when the helper must be language-neutral and owned by a platform team; node agents or in-process libraries when the per-pod cost and operational overhead outweigh that.

Cross-cutting concerns in Encore

Encore

Encore brings infrastructure from code, and many jobs a sidecar does are handled by its runtime inside the application process: every request is traced across services, a service calls another as a typed function through generated clients described in the API calls docs, and secrets are injected into the container on deploy instead of fetched by an agent. Policy that would otherwise live in proxy config goes in middleware in code:

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

package checkout import ( "context" "time" "encore.app/inventory" // another service in the same app "encore.dev/middleware" "encore.dev/rlog" ) var secrets struct { PaymentProviderKey string } // Applies to every endpoint in this service, as a proxy filter would. // //encore:middleware target=all func Timing(req middleware.Request, next middleware.Next) middleware.Response { start := time.Now() resp := next(req) rlog.Info("handled", "endpoint", req.Data().Endpoint, "duration_ms", time.Since(start).Milliseconds()) return resp } type PlaceParams struct { SKU string `json:"sku"` Quantity int `json:"quantity"` } //encore:api auth method=POST path=/checkout func Place(ctx context.Context, p *PlaceParams) error { // A typed call to the inventory service; discovery and tracing are handled by the runtime. if _, err := inventory.Reserve(ctx, &inventory.ReserveParams{SKU: p.SKU, Quantity: p.Quantity}); err != nil { return err } return charge(ctx, secrets.PaymentProviderKey) } func charge(ctx context.Context, apiKey string) error { // Payment provider call goes here. return nil }

encore run runs every service locally with the same tracing, so the call from checkout to inventory appears in one trace on a laptop, and on deploy the services run on ECS, EKS or Cloud Run in your own account, as the AWS infrastructure docs describe, without a proxy container in each task.

An AI coding agent adding a call to another service writes a function call that the compiler checks, rather than a URL plus a proxy route, and the trace shows whether it worked on the first local run.

Best fit: backends that want tracing, service calls and secrets handled without running and upgrading per-pod helpers.

How to approach it

List the sidecars you run and the job each does. Keep the ones that need to be language-neutral and platform-owned, move per-pod log and metrics agents to node agents, and replace proxies with in-process handling where the services are yours to change. Use native sidecar containers for whatever remains.

Frequently asked questions

What is the sidecar pattern?

A sidecar is a helper process deployed next to an application container, sharing its lifecycle, network and often its filesystem, to add a capability such as a proxy, a log shipper or a secrets agent without changing the application code.

What are native sidecar containers in Kubernetes?

They are init containers with restartPolicy: Always. Kubernetes starts them before the app containers, keeps them running for the life of the pod, and stops them after the app containers, which fixes the old problems of Jobs that never finished and proxies that shut down first. The feature is stable since Kubernetes 1.33.

What are the downsides of sidecars?

Each pod pays the sidecar's CPU and memory, startup and shutdown need ordering, upgrading the sidecar means restarting every pod, and one more process per pod adds failure modes and configuration to debug.

Does Encore use sidecars for tracing and service calls?

No. Tracing, metrics, service-to-service calls and secrets are handled by the Encore runtime inside the application process, so a service needs no proxy or agent container for those concerns.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders