← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

How CDNs Work

networking Intermediate

Also Known As

CDN PoP Anycast edge caching origin pull

TL;DR

CDNs use Anycast routing and distributed edge PoPs to serve content from the nearest location — reducing latency from 300ms to 5ms for cached responses.

Explanation

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.

Common 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

Code Examples

✗ Vulnerable
// No cache headers — CDN pass-through:
public function products(): JsonResponse {
    return response()->json(Product::all());
    // Every request hits PHP and database — CDN provides no benefit
}
✓ Fixed
// CDN-cacheable response:
public function products(): JsonResponse {
    $etag = md5($this->productService->getLastModified());
    return response()
        ->json($this->productService->all())
        ->withHeaders([
            'Cache-Control' => 'public, s-maxage=300, stale-while-revalidate=60',
            'ETag'          => $etag,
            'Vary'          => 'Accept-Encoding',
        ]);
    // CDN serves for 5 minutes without touching PHP
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 4 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 5 Unknown AI 2 SEMrush 2 Google 1 Ahrefs 1
crawler 19
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ 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
📦 Applies To
any web
🔗 Prerequisites
🔍 Detection Hints
PHP serving static files directly; no CDN in front of production; static assets without Cache-Control: public max-age headers
Auto-detectable: ✓ Yes lighthouse curl webpagetest
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: File

✓ schema.org compliant