Aman Goyal

LeetCode LeetCode

The Sidecar Pattern: Extending Applications Without Modifying Them


Core Concept


Why Sidecars Are Powerful

1. Extend apps without modifying them

Huge for legacy systems you can’t rebuild


2. Enable cloud-native capabilities

Example: Config management


3. Promote modularity & reuse

Avoid rewriting same logic in every service


4. Separation of concerns


5. Enables powerful compositions


Key Design Principles for Sidecars

1. Parameterization (VERY important)

Example:


2. Define a stable API

Sidecars are like reusable libraries


3. Documentation matters

If people can’t use it, it’s useless


Trade-offs

Pros

Cons


Mental Model

Think of a sidecar like:

A plug-in container attached to your app at runtime


One-line Summary

Sidecars let you extend, modernize, and modularize applications by attaching helper containers that run alongside the main app without changing its code.


Code Example

##  PID Namespace Sharing Example (Sidecar)

### 1. Run the main application container
docker run -d --name app-container nginx

### 2. (Optional) Verify it's running
docker ps

### 3. Run a sidecar container sharing the PID namespace
docker run -it --rm \
  --pid=container:app-container \
  alpine sh

### 4. Inside the sidecar container, list processes
ps aux

#  You will see processes from the nginx container here

---

##  What's happening?

- --pid=container:app-container
  → Sidecar joins the SAME PID namespace as the main container

- Both containers can:
  - See each other's processes
  - Send signals (kill, inspect, etc.)

---

##  Mental Model

[ app-container (nginx) ]  <-- shared PID namespace -->  [ sidecar (alpine) ]
          PID 1 (nginx)                              visible here via ps

#Distributed Systems #System Design #Sidecar Pattern #Kubernetes #Containers