{
    "slug": "sidecar_pattern",
    "term": "Sidecar Pattern",
    "category": "architecture",
    "difficulty": "advanced",
    "short": "Deploying a helper container alongside the main application container in the same pod — extending functionality (logging, proxying, TLS) without modifying the application.",
    "long": "A sidecar runs in the same Kubernetes pod as the main container, sharing its network namespace and storage. Common sidecars: Envoy proxy (service mesh — mTLS, tracing, retries), Filebeat/Fluentd (log shipping — reads app log files and forwards to aggregator), secret management agents (Vault agent injects secrets as files), and configuration reloaders. The sidecar pattern keeps the main application simple — it doesn't need to know about service mesh, log aggregation, or secret rotation. The pattern is the foundation of service mesh architectures.",
    "aliases": [
        "sidecar container",
        "sidecar proxy",
        "Envoy sidecar"
    ],
    "tags": [
        "architecture",
        "kubernetes",
        "devops",
        "microservices"
    ],
    "misconception": "Sidecar containers share the main container's filesystem — sidecars share the pod's network namespace (same localhost) and can share volumes, but have separate filesystems unless volumes are explicitly mounted.",
    "why_it_matters": "A PHP application that needs distributed tracing would normally require SDK integration in every service — a sidecar proxy intercepts all traffic and adds trace headers automatically, zero application changes needed.",
    "common_mistakes": [
        "Sidecar with heavy resource usage stealing CPU/RAM from the main container — always set resource limits.",
        "Putting business logic in sidecars — sidecars are for cross-cutting infrastructure concerns only.",
        "Not handling sidecar startup ordering — main container may start before sidecar is ready.",
        "Too many sidecars per pod — each adds overhead; consolidate where possible."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "service_mesh",
        "container_orchestration",
        "kubernetes_basics",
        "distributed_tracing"
    ],
    "prerequisites": [
        "microservices",
        "containerisation",
        "service_mesh"
    ],
    "refs": [
        "https://kubernetes.io/docs/concepts/workloads/pods/#how-pods-manage-multiple-containers"
    ],
    "bad_code": "// All concerns in the main container — bloated:\n// PHP app must handle: TLS termination, log shipping,\n// trace injection, secret rotation, health proxying\n// Results in: large image, mixed concerns, hard to update each independently",
    "good_code": "# Kubernetes pod with sidecars:\napiVersion: v1\nkind: Pod\nspec:\n  containers:\n  - name: php-app          # Main: pure business logic\n    image: myapp:abc123\n  - name: envoy            # Sidecar: mTLS + tracing\n    image: envoyproxy/envoy:v1.28\n  - name: filebeat         # Sidecar: log shipping\n    image: elastic/filebeat:8.12\n    volumeMounts:\n    - name: logs\n      mountPath: /var/log/app  # Reads PHP app logs",
    "quick_fix": "Deploy a sidecar container alongside your PHP container to handle cross-cutting concerns (log shipping, mTLS, secret rotation) — the PHP container stays simple and focused on business logic",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/sidecar_pattern",
        "html_url": "https://codeclaritylab.com/glossary/sidecar_pattern",
        "json_url": "https://codeclaritylab.com/glossary/sidecar_pattern.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Sidecar Pattern](https://codeclaritylab.com/glossary/sidecar_pattern) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/sidecar_pattern"
            }
        }
    }
}