Labs › networking

Build Your Own CDN Edge Node

Build a CDN edge node in Go from scratch: LRU cache with stale-while-revalidate, request coalescing, a two-level L1/L2 tier hierarchy, and prefetch-hint processing — the architecture behind Cloudflare and Fastly edge nodes.

go 1.23 advanced ~8h build · ~2h read ~920 LoC (v3)
v0: LRU cache + Cache-Control parsing + stale-while-revalidatev1: Request coalescing + stale-if-error + Vary header supportv2: Two-level L1/L2 tier hierarchy + adaptive TTL + Link prefetch
☕ Java/Spring integration included service client

Why this lab exists

Every HTTP response that travels from a database through an application server to a user could instead be served from a cache 50 ms away. CDNs exploit that opportunity at scale. But understanding which responses to cache, for how long, and what to do when the cache is stale requires internalizing HTTP cache semantics that most developers only encounter when they accidentally break production.

Building an edge node from scratch forces you to make the decisions that Cloudflare and Fastly have already made. Do you serve stale content during a revalidation? Do you let 10,000 concurrent misses all hit the origin at once? What happens when the origin is down?

What we’re building

A CDN edge node in Go across three stages:

  • v0: LRU cache, Cache-Control parsing (including the stale-while-revalidate and stale-if-error windows, recorded on each entry)
  • v1: Serving stale content (stale-while-revalidate, and stale-if-error when the origin returns a 5xx), request coalescing (100 concurrent misses → 1 origin call), Vary header keyspace splitting
  • v2: Two-level L1/L2 cache hierarchy, adaptive TTL, Link: rel=prefetch cache warming

Explicitly excluded: geographic distribution, WAL-backed origin shields, signed URLs, QUIC/HTTP3, Range request caching, streaming responses.

Mental model

Think of the edge node as a caching reverse proxy with four decisions per request:

  1. Fresh cache entry? → Serve it. Done.
  2. Stale entry within stale-while-revalidate window? → Serve it immediately, revalidate in the background.
  3. Stale entry and origin returned 5xx? → Serve the stale entry (stale-if-error).
  4. Complete miss? → Fetch from origin, cache the response, return it.

Request coalescing wraps case 4: instead of N concurrent origin requests, one goroutine does the fetch while N-1 goroutines wait on a channel. The winner stores the result and closes the channel; all waiters receive the same CacheEntry.

The LRU cache uses a doubly-linked list (container/list) plus a hash map. Get calls list.MoveToFront — O(1). Set calls list.PushFront; when over capacity, list.Back() is the victim.

Prerequisites

Stage-by-stage build

Stage 0 LRU cache + Cache-Control parsing + stale-while-revalidate ~280 LoC ›
  • CacheEntry with Body, Headers, StatusCode, ExpiresAt, StaleWhileRevalidateUntil, StaleIfErrorUntil
  • Cache backed by container/list + map — O(1) Get (MoveToFront) and Set (evict Back on overflow)
  • ParseCacheControl: split on comma, parse max-age, stale-while-revalidate, stale-if-error, no-store
  • EdgeProxy.ServeHTTP: fresh serve → miss → fetch and cache; the SWR and SIE windows are recorded for v1 to use

The core data structure stores the full HTTP response plus three timestamps:

type CacheEntry struct {
    Body                      []byte
    Headers                   http.Header
    StatusCode                int
    ExpiresAt                 time.Time
    StaleWhileRevalidateUntil time.Time
    StaleIfErrorUntil         time.Time
    AccessCount               int64
    OriginalTTL               time.Duration
}

func (e *CacheEntry) IsExpired() bool           { return time.Now().After(e.ExpiresAt) }
func (e *CacheEntry) IsStaleWhileRevalidate() bool {
    now := time.Now()
    return now.After(e.ExpiresAt) && now.Before(e.StaleWhileRevalidateUntil)
}

ParseCacheControl splits the Cache-Control response header on commas and handles max-age=N, stale-while-revalidate=N, stale-if-error=N, no-store, no-cache. It also parses the Vary header for v1.

The EdgeProxy implements http.Handler. The dispatch logic maps to the four decisions above:

func (p *EdgeProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    key := cacheKey(r)
    if entry := p.cache.Get(key); entry != nil {
        if !entry.IsExpired()            { serve(w, entry); return }
        if entry.IsStaleWhileRevalidate() {
            serve(w, entry)
            go p.revalidate(key, r)
            return
        }
    }
    resp, err := p.fetchOrigin(r)
    if err != nil {
        if entry := p.cache.Get(key); entry != nil && entry.IsStaleIfError() {
            serve(w, entry); return
        }
        http.Error(w, "bad gateway", 502); return
    }
    p.cache.Set(key, toEntry(resp, directives))
    serve(w, ...)
}
Stage 1 Request coalescing + stale-if-error + Vary header support ~300 LoC ›
  • inflightReq{done chan struct{}, entry *CacheEntry, err error} — the coalescing primitive
  • First goroutine on a miss creates inflightReq and fetches; rest block on done channel
  • close(inflight.done) broadcasts to all waiters simultaneously
  • Vary key: append normalized Vary field value to cache key to separate per-header entries
  • Stale serving: SWR serves stale + revalidates in the background; an origin 5xx is an error, so waiters get the stale copy (stale-if-error) and the 5xx is never cached

The thundering herd problem: when a popular cache entry expires, 100 concurrent requests all see a miss and all race to the origin — a 100× traffic spike. Request coalescing prevents this:

type inflightReq struct {
    done  chan struct{}
    entry *CacheEntry
    err   error
}

func (cp *CoalescingProxy) fetch(key string, r *http.Request) (*CacheEntry, error) {
    cp.mu.Lock()
    if inf, ok := cp.inflight[key]; ok {
        cp.mu.Unlock()
        <-inf.done         // block until winner finishes
        return inf.entry, inf.err
    }
    inf := &inflightReq{done: make(chan struct{})}
    cp.inflight[key] = inf
    cp.mu.Unlock()

    entry, err := cp.base.fetchOrigin(r)
    inf.entry, inf.err = entry, err
    close(inf.done)        // broadcast to all waiters

    cp.mu.Lock()
    delete(cp.inflight, key)
    cp.mu.Unlock()
    return entry, err
}

Vary header support: some origins return Vary: Accept-Encoding or Vary: Accept-Language. Without Vary-aware keying, a compressed response would be served to a client that didn’t accept gzip. The cache key in v1 appends the normalized value of each Vary field:

key = method + ":" + path + "?" + query + "|vary:" + accept_encoding_value

A single /api/data URL with Vary: Accept-Language stores separate entries for en, fr, and zh.

Stage 2 Two-level L1/L2 tier hierarchy + adaptive TTL + Link prefetch ~340 LoC ›
  • TieredProxy with l1 (100 entries) and l2 (10 000 entries) — lookup L1 first, promote L2 hits
  • L1 evictions are demoted to L2 rather than discarded
  • AdaptiveTTL: double TTL on re-cache if AccessCount > 10, cap at 4x original
  • Prefetcher.ProcessLinkHeader: parse Link: rel=prefetch, warm each URL in a background goroutine

Real CDNs maintain multiple cache tiers. A tiny hot L1 serves the most popular content at the lowest latency. A larger warm L2 stores the long-tail:

func (tp *TieredProxy) lookup(key string) *CacheEntry {
    if e := tp.l1.Get(key); e != nil {
        tp.stats.L1Hits.Add(1)
        return e
    }
    if e := tp.l2.Get(key); e != nil {
        tp.stats.L2Hits.Add(1)
        tp.l1.Set(key, e) // promote to L1
        return e
    }
    tp.stats.Misses.Add(1)
    return nil
}

When L1 evicts an entry (capacity overflow), it is demoted to L2 rather than discarded. An item does not fully leave the cache until it is also evicted from L2.

Adaptive TTL: popular content should stay fresh longer. Each CacheEntry tracks AccessCount. On background revalidation, if the count exceeds 10, the TTL is doubled (up to 4× the original Cache-Control: max-age).

Link prefetch: the Prefetcher parses Link: </next-page>; rel=prefetch response headers and warms those URLs in background goroutines — before any client requests them.

Run it yourself

Run it yourself labs/cdn-edge ↗
Prereqs: Go 1.22+
  1. 1
    Clone and build
    git clone https://github.com/pushkarkumar-dev/system-design-labs
    cd system-design-labs/labs/cdn-edge
    go build ./...
  2. 2
    Start a simple origin on port 9000
    go run ./cmd/origin &
  3. 3
    Start the CDN edge on port 8080
    go run ./cmd/edge
  4. 4
    First request — cache MISS
    curl -v http://localhost:8080/api/data
  5. 5
    Second request — cache HIT
    curl -v http://localhost:8080/api/data
  6. 6
    Check cache stats
    curl http://localhost:8080/stats
  7. 7
    Purge an entry
    curl -X DELETE 'http://localhost:8080/cache/purge?path=/api/data'
  8. 8
    Run tests
    go test ./pkg/cdn/ -v -race

Benchmarks

Benchmark results on M2 MacBook Pro, 16GB RAM, Go 1.23
Metric Result Notes
Cache hit throughput (L1) 450,000 req/s Varnish ~600K via lock-free sharded hash
Cache miss (origin loopback) 45,000 req/s nginx proxy_pass ~55K; both dominated by loopback RTT
Coalescing (100 concurrent misses) 1 origin call 99% origin call reduction vs naive miss
LRU eviction throughput 45,000,000 ops/s list.MoveToFront + map delete; groupcache ~40M

Stress tests

10,000 concurrent requests: without coalescing, a popular entry expiring at peak traffic sends 10,000 simultaneous origin requests. With coalescing, only 1 fires. The other 9,999 wait on the channel and add roughly 100 µs of latency — negligible compared to the origin RTT saved.

Vary explosion: if an origin returns Vary: User-Agent, the cache stores one entry per user-agent string. At 1,000 distinct user-agents and LRU capacity 100, the cache is effectively bypassed — 99% of requests miss. Real CDNs normalize user-agents into buckets (mobile/desktop/bot) before using them in Vary keys.

Adaptive TTL saturation: after four doublings, the TTL cap is reached. Content cached with max-age=60 and heavy access will be held for 240 seconds. If the origin changes the content more frequently, the edge serves stale data for up to 240 seconds — the multiplier must be tuned to the content’s rate of change.

What the toy misses Honest gaps vs. production
  • No geographic distribution A real CDN routes requests to the nearest PoP using anycast BGP routing or GeoDNS. Our single-process edge node has no notion of geography.
  • No WAL-backed origin shield Fastly's origin shield serialises all cache misses for a given URL through a single mid-tier node, preventing the origin from seeing traffic spikes. Our coalescing only works within one process.
  • No signed URLs or token validation Cloudflare and CloudFront support signed URL policies that expire after a time window or IP range. Our edge trusts all incoming requests unconditionally.
  • No QUIC / HTTP3 termination Modern CDNs terminate QUIC at the edge and reuse HTTP/1.1 or HTTP/2 to the origin. Our proxy speaks HTTP/1.1 end-to-end.
  • No partial-content (Range) caching Range requests require caching byte ranges and reassembling slices — critical for video streaming. Our cache stores full responses only.
  • No streaming / chunked transfer We buffer the full origin response before serving. A real edge node starts streaming to the client while the response is still being received, minimising time-to-first-byte for large objects.

Wiring it into a Java/Spring service

☕

Wiring it into a Java/Spring service

service client spring-webmvc
Maven dependencies
org.springframework.boot:spring-boot-starter-web:3.3.0

The Java side uses Spring RestClient to route requests through the CDN edge node. The CDN is transparent to the application — it sees Cache-Control headers and caches accordingly:

@Component
public class CdnClient {
    private final RestClient http;

    public CdnClient(RestClient.Builder builder,
                     @Value("${cdn.edge-url:http://localhost:8080}") String base) {
        this.http = builder.baseUrl(base).build();
    }

    public byte[] fetch(String path) {
        return http.get().uri(path).retrieve().body(byte[].class);
    }

    public void purge(String path) {
        http.delete().uri("/cache/purge?path={p}", path)
            .retrieve().toBodilessEntity();
    }
}

Spring Cache comparison: Spring’s @Cacheable and CacheManager (Caffeine, EhCache) provide in-process caching. The key difference is scope: Spring Cache is per-JVM; a CDN edge is shared across all JVMs and all clients. Use @Cacheable to avoid recomputing expensive in-JVM results; use the CDN edge to avoid hitting the application layer at all.

Where to go deeper

  • RFC 9111 — the authoritative HTTP caching spec; sections 4 and 5 cover constructing and invalidating responses
  • RFC 5861 — stale-while-revalidate and stale-if-error extensions
  • groupcache (github.com/golang/groupcache) — the singleflight package is the coalescing pattern in 100 lines
  • Varnish Documentation — Grace mode explains production-grade SWR implementation
  • Cloudflare Blog — “How Cloudflare Cache works” and their approach to cache invalidation at 330 PoPs
  • http-server — the http.Handler interface this lab builds on
  • kv-cache — the Redis-lite cache that handles the distributed tier
  • load-balancer — sits in front of the CDN edge node
  • rate-limiter — protects the origin from overload on cache misses
  • How does Cloudflare’s Tiered Cache differ from our L1/L2?
  • What happens to stale-while-revalidate when background revalidation fails three times in a row?