{
    "slug": "cdn_how_it_works",
    "term": "How CDNs Work",
    "category": "networking",
    "difficulty": "intermediate",
    "short": "CDNs use Anycast routing and distributed edge PoPs to serve content from the nearest location — reducing latency from 300ms to 5ms for cached responses.",
    "long": "Anycast routing: multiple edge servers share the same IP address; BGP routes requests to the geographically closest PoP. Cache miss: the edge fetches from origin and caches the response. Cache key: URL + Vary headers. Benefits: latency reduction, throughput offload from origin, reliability when origin is down, DDoS absorption. PHP sends Cache-Control: public, s-maxage=N to enable CDN caching. Dynamic API responses with correct headers are CDN-cacheable too.",
    "aliases": [
        "CDN PoP",
        "Anycast",
        "edge caching",
        "origin pull"
    ],
    "tags": [
        "networking",
        "performance",
        "cdn"
    ],
    "misconception": "CDN only works for static files — any HTTP response with correct Cache-Control headers is CDN-cacheable, including JSON API responses and PHP-rendered HTML.",
    "why_it_matters": "A user in Tokyo requesting content from a London origin experiences 250ms RTT — the same content served from a Tokyo CDN edge delivers in 5ms, a 50x improvement for cached responses.",
    "common_mistakes": [
        "No Cache-Control headers — CDN cannot cache",
        "Caching Set-Cookie responses — serves another user's cookie",
        "Vary: * disables all caching",
        "Same URL different content without Vary header"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cloud_cdn",
        "http_request_response_cycle",
        "prefetching_strategies"
    ],
    "prerequisites": [
        "http_caching",
        "asset_caching",
        "gzip_compression"
    ],
    "refs": [
        "https://www.cloudflare.com/learning/cdn/what-is-a-cdn/"
    ],
    "bad_code": "// No cache headers — CDN pass-through:\npublic function products(): JsonResponse {\n    return response()->json(Product::all());\n    // Every request hits PHP and database — CDN provides no benefit\n}",
    "good_code": "// CDN-cacheable response:\npublic function products(): JsonResponse {\n    $etag = md5($this->productService->getLastModified());\n    return response()\n        ->json($this->productService->all())\n        ->withHeaders([\n            'Cache-Control' => 'public, s-maxage=300, stale-while-revalidate=60',\n            'ETag'          => $etag,\n            'Vary'          => 'Accept-Encoding',\n        ]);\n    // CDN serves for 5 minutes without touching PHP\n}",
    "quick_fix": "Serve all static assets (CSS, JS, images) through a CDN with immutable cache headers — PHP only handles dynamic requests; CDN handles the 90% that are static files",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/cdn_how_it_works",
        "html_url": "https://codeclaritylab.com/glossary/cdn_how_it_works",
        "json_url": "https://codeclaritylab.com/glossary/cdn_how_it_works.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": "[How CDNs Work](https://codeclaritylab.com/glossary/cdn_how_it_works) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/cdn_how_it_works"
            }
        }
    }
}