SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
  • Service Discovery
  • API Gateway Pattern
  • Backend for Frontend (BFF)
  • Sidecar Pattern
  • Circuit Breaker
  • Bulkhead
  • Strangler Fig
  • Service Mesh
  • Database per Service
  • Timeouts and Deadlines
  • API Composition
  • Anti-Corruption Layer
  • Ambassador Pattern
  • Idempotent Consumer and Inbox Pattern
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Architecture/Microservices Patterns

Sidecar Pattern

Run a helper next to your service to handle logging, security or networking for it.

⏱ 11 min readIntermediateHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

The sidecar pattern puts a small helper program right next to your main service. They run together, share the same machine (or pod), and talk over localhost.

  • The main app does the business work (orders, payments, search).
  • The sidecar does the chores every service needs: encryption, retries, logging, metrics, config, service lookup.
  • The helper is separate from your code, so it works for any language and can be upgraded on its own.

02Concept: What is it?

Imagine you have 50 services. Each of them needs the same things: secure connections, retries, timeouts, logs and metrics. You can write this code as a library and add it to every service. But then you need a copy for every programming language, and every fix means changing and redeploying 50 services.

The sidecar pattern moves those chores out of the app and into a second container that is always deployed together with it. The name comes from a motorcycle sidecar: a small extra seat attached to the main bike. It shares the trip, but it is a separate part.

In Kubernetes, one pod holds both containers. They share the same network (so they talk over localhost), can share disk volumes, and always start, stop and scale together. The app does not even need to know the sidecar is there: its outgoing calls are simply sent through the sidecar.

One pod, two containers
Pod
🧠Main appbusiness logic
↔localhost
πŸ›΅SidecarTLS, retries, logs
→↓network
🌐Other services
The app talks only to its neighbour. The sidecar handles the outside world.
🧠Same pod, different program
A sidecar is not a library inside your code. It is its own process, with its own image, version and resources. That is why it works for every language.

03Real-Life Example

🍡A Chef and a Kitchen Assistant
A chef cooks in a busy restaurant. The chef should only think about the food. So every chef gets an assistant who stands beside them: the assistant washes dishes, answers the phone, writes down every order, and fetches ingredients. The chef never leaves the stove. If the restaurant changes its phone rules, only the assistants get new training. The chefs keep cooking. Every chef, French or Italian, can use the same kind of assistant.
Restaurant
Sidecar pattern
The chef
The main service (business logic)
The assistant at the chef's side
The sidecar container
Talking across the counter
Calls over localhost
Phone, dishes, notes
TLS, retries, logs, metrics
One assistant per chef
One sidecar per service instance
New phone rules, train assistants only
Upgrade the sidecar, no app code change
Chefs from any country
Services in any language
A request leaving the pod
🧠Appplain call
→↓localhost
πŸ›΅Sidecaradds TLS, retry, trace
→↓encrypted
🌐Other service

04Why Do We Need It?

🌍Any language
A Python, Java and Go service all get the same feature from the same sidecar.
🧩Separation of concerns
Developers write business code. Platform teams own the chores.
πŸ”„Upgrade in one place
Roll out a new sidecar image instead of editing 50 code bases.
πŸ”Consistent security
Every service gets the same encryption and identity rules, without relying on each team.
πŸ‘€Free visibility
The sidecar sees every call, so it can produce logs, metrics and traces for all services.
🧱Works with old code
Add features to a legacy app without opening its source code.
Shared chores should not be rewritten in every service. Put them in a neighbour that any service can use.

05Where Do We Use It?

πŸ•ΈοΈService meshes
Istio and Linkerd put a proxy (Envoy or linkerd2-proxy) next to every service.
πŸ”Security and identity
A sidecar handles mTLS certificates, or fetches secrets from a vault.
πŸ“œLog shipping
A log agent container reads the app's log files and sends them to a central place.
πŸ“ˆMetrics and tracing
A sidecar collects metrics and trace data and exports it.
βš™οΈConfig and secrets
A helper refreshes configuration files while the app runs.
🧰Distributed runtimes
Dapr runs as a sidecar and offers messaging, state and secrets through a simple local API.

06Types / Parts / How It Works

Common jobs for a sidecar

JobWhat the sidecar doesExample
Network proxyHandles all in and out traffic: retries, timeouts, load balancing, mTLSEnvoy in Istio
Log / telemetry agentCollects logs and metrics from the app and ships themFluent Bit, OpenTelemetry Collector
Config / secrets syncKeeps files fresh and fetches secretsVault Agent
Runtime helperGives the app a local API for messaging, state and moreDapr
Adapter / ambassadorTranslates formats (for example, metrics into one standard) or talks to the outside world for the appPrometheus exporter container

Library vs sidecar

Two ways to share a feature
Library
β˜•Java app+ lib
🐍Python app+ lib
One copy per language, inside the process
Sidecar
β˜•Java app
πŸ›΅Sidecar
🐍Python app
πŸ›΅Same sidecar
Shared code in the app Separate helper
A library lives inside every app. A sidecar lives beside every app.
QuestionLibrarySidecar
Language supportOne build per languageAny language
Extra network hopNone, runs in processA fast local hop
Resource useSmall, shared with the appExtra memory and CPU for every pod
UpgradeChange code, build, redeployUpdate the image or config
Failure isolationA bug can crash the appA sidecar crash is separate (but may block traffic)
VisualizerLibrary vs sidecar: maintenance and costSwitch between a library and a sidecar. Change the number of services and languages, then follow one request through the pod.
Where does the shared feature live?
Shared feature
One pod (one service instance)
🧠Your applogic + shared library
πŸ“šLibraryinside the app (Java copy)
→↓network
🌐Other services
Step 1 of 3: Your code decides to call another service
Copies to maintain
3
one per language
Code changes per fix
8
services to edit
Pods to restart
8
to roll out
Extra memory
0 MB
all sidecars
Extra latency
0 ms
per call (example)
With a library, you need retry library + settings in every language: 3 copies to write and keep in sync. A fix means changing code in 8 services, building and redeploying each one, and teams will not all do it on the same day.

Example numbers only (60 MB per sidecar, 1 ms per local hop). Real sidecars (Envoy, for example) vary with traffic, config and version.

07How to Use It / Steps / Implementation

Adding a sidecar

  1. 1
    Pick the chore
    Choose something every service needs and that is not business logic, like mTLS, log shipping or retries.
  2. 2
    Choose a ready-made sidecar
    Use a proven one (Envoy, Fluent Bit, Vault Agent, Dapr) before writing your own.
  3. 3
    Deploy it in the same pod
    List both containers in one pod spec. They share the network, and optionally volumes, and start and stop together.
  4. 4
    Route the traffic
    Send the app's calls through the sidecar, for example with localhost ports or network rules that redirect traffic (often an init container sets this up).
  5. 5
    Set resource limits
    Give the sidecar its own CPU and memory limits, so it cannot starve the app, and the other way around.
  6. 6
    Handle start and stop order
    The app should not start sending traffic before the sidecar is ready, and the sidecar should not exit before the app has finished.
  7. 7
    Monitor the sidecar too
    It is now part of every request path. Watch its health, memory and version across the fleet.
πŸ’‘Automatic injection
Platforms like Istio can add the sidecar to every pod for you (a webhook edits the pod spec). Developers do not touch their deployment files.
⚠️Do not turn it into a second app
A sidecar should stay small and generic. If it starts holding business rules, you have hidden half your service in a place nobody expects.

08Key Terms to Know

TermMeaning in easy words
SidecarA helper container deployed next to the main service, sharing its pod or host
PodIn Kubernetes, the smallest unit: one or more containers that share network and storage
localhostThe address a program uses to talk to another program on the same machine
ProxyA program that passes requests on behalf of another (Envoy is a popular one)
mTLSMutual TLS: both sides prove who they are and the traffic is encrypted
Cross-cutting concernA chore needed by many services (logging, security, retries)
AmbassadorA sidecar that represents the app towards the outside world
AdapterA sidecar that changes the app's output into a standard shape
Init containerA short container that runs first in a pod, often to set up network rules
InjectionAutomatically adding the sidecar to a pod definition
PolyglotA system using many programming languages

09Pros, Cons and Trade-offs

βœ… Pros
  • Works for every language
  • Upgrade shared features without touching app code
  • Same rules for all services
  • Keeps app code focused on business logic
  • Gives easy visibility of all traffic
  • Good for legacy apps
⚠️ Cons
  • Extra memory and CPU in every pod
  • An extra local hop adds a little latency
  • More moving parts to run and debug
  • Start-up and shutdown order is tricky
  • A broken sidecar can block the app
  • Version mismatch across a large fleet is possible

Trade-offs

ChoiceWe gainWe lose
Sidecar instead of libraryLanguage freedom and central upgradesMemory, CPU and an extra hop for each pod
Auto-inject everywhereZero work for app teamsSurprise: hidden containers that teams must understand
Rich sidecar (many features)Fewer things in app codeA heavier, harder to debug sidecar
Tiny single-job sidecarsSimple and easy to reason aboutMore containers per pod
Library in the appNo extra hop, no extra processMany copies to build and keep in sync

10Common Problems and Mistakes

Common mistakes

  1. Sidecar for a tiny system. With 2 services, a plain library is simpler and cheaper.
  2. No resource limits. A hungry sidecar steals memory from the app and the pod gets killed.
  3. Wrong start order. The app sends a request before the proxy is ready, and the first calls fail.
  4. Putting business logic inside. Hidden rules in the sidecar make behaviour hard to find.
  5. Forgetting the cost. 1000 pods with a 60 MB sidecar is about 60 GB of extra memory.
  6. Not watching the sidecar. It sits in every request path, so its failure looks like an app failure.
  7. Mixed versions. Half the fleet runs an old sidecar with different behaviour.

Things to watch

🐒Added latency
Two extra local hops per call. Small, but it adds up in long call chains.
πŸ’ΎResource tax
Every pod pays for its own sidecar. Measure the total.
🚦Job and shutdown issues
A batch job pod may never finish if the sidecar keeps running. Plan how it exits.
πŸ”Harder debugging
Errors can come from the app or the proxy. Good logs and tracing help you tell which.

11Real-World Examples

πŸ•ΈοΈIstio and Linkerd
Both put a proxy beside every service to give encryption, retries and metrics across the mesh.
🏒Lyft and Envoy
Envoy was created at Lyft as a proxy that runs beside each service, so every language gets the same networking features.
πŸ”‘HashiCorp Vault Agent
Runs next to an app to fetch and renew secrets, so the app just reads a file.
πŸ“œFluent Bit
A tiny log shipper often deployed as a sidecar to send logs to a central store.
🧰Dapr
Gives apps a local HTTP/gRPC API for state, pub/sub and secrets, so they avoid vendor SDKs.
☁️Cloud meshes
Managed offerings such as AWS App Mesh and Google's Cloud Service Mesh use the same idea.
πŸ“Note
Newer meshes also offer "sidecar-less" modes (for example Istio ambient mode) to reduce the per-pod cost. The pattern is still common, but not the only option.
Mini case: logs without changing the app
🧠Legacy appwrites app.log
→↓shared volume
πŸ“œLog sidecarreads and ships
→↓send
πŸ—ƒοΈCentral logs
The app writes to a file. The sidecar ships it.

12Related Topics to Read Next

Service Mesh→

A mesh is a network of sidecar proxies controlled from one place.

Microservices→

The sidecar solves chores repeated in every small service.

Circuit Breaker→

A sidecar proxy can apply circuit breaking for the app.

Proxy vs Reverse Proxy→

A sidecar proxy is a proxy placed next to a single service.

API Gateway Pattern→

A gateway sits at the edge, while sidecars sit beside each service.

Service Discovery→

Sidecars often find other services for the app.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • βœ”What: a helper container deployed beside the main service, in the same pod.
  • βœ”Talks: over localhost or a shared volume. App code stays simple.
  • βœ”Does: mTLS, retries, timeouts, logs, metrics, secrets, config.
  • βœ”Why: any language, central upgrades, same rules everywhere.
  • βœ”Cost: extra CPU and memory per pod, a small extra hop, more to debug.
  • βœ”Real tools: Envoy (Istio, Linkerd proxy), Fluent Bit, Vault Agent, Dapr.
  • βœ”Watch: resource limits, start and stop order, and sidecar versions.
  • βœ”One line: β€œPut the shared chores in a neighbour, not in every app.”
← Previous
Backend for Frontend (BFF)
Next β†’
Circuit Breaker