TRT
Sign in
NetworkingUpdated

CDNs and Anycast Routing

How content delivery networks place edge servers near users and how anycast uses BGP to route to the nearest replica.

The Runtime Theory Team8 min read#cdn#anycast#routing#performance

Serving every byte from one origin means every user pays the full round-trip distance to that building. A user in Sydney fetching from Virginia waits ~200 ms per round trip before a single byte of content moves — and a page needs dozens of round trips. Content delivery networks attack the distance itself, and anycast routing attacks the problem of finding the nearest copy.

Edge caches serve content from nearby locations

A CDN is a globally distributed network of edge servers that cache content close to users. When a user requests a resource, the CDN serves it from the nearest edge location rather than the origin server, reducing latency and offloading the origin. A provider with 300+ points of presence puts most users within 20–30 ms of an edge, turning that 200 ms Sydney-to-Virginia fetch into a local one.

How a request finds the nearest edge

  1. User resolves a CNAME — e.g., cdn.example.com might resolve to edge-01.lax.cdnprovider.net.
  2. DNS returns an edge location — the resolver answers with the IP of the edge server that is cheapest to reach, using the resolver's location, BGP path costs, and current load.
  3. Edge cache lookup — the edge server checks its cache. If the content is there (a cache hit), it serves it immediately with no origin traffic at all.
  4. Cache miss — the edge server fetches from the origin (or a parent edge), caches it, and serves the user. Subsequent requests hit the cache.

Expiry and purging bound staleness

CDNs use TTL (time-to-live) to determine how long to cache content. After the TTL expires, the next request triggers a fresh fetch from origin. A one-year TTL on fingerprinted assets (app.a3f9c2.js) is safe because the name changes with the content; a 60-second TTL on an API response bounds staleness to a minute. For content that changes unpredictably, invalidation APIs purge the cache proactively — typically effective worldwide within seconds, though providers charge per purge beyond a free quota.

Anycast sends packets to the nearest identical address

Anycast is a network addressing and routing strategy where the same IP address is assigned to multiple servers in different locations. When a client connects to an anycast address, BGP routes the connection to the "nearest" server (by hop count, not geographic distance). DNS root servers and major CDN entry points all use anycast: one address, dozens of replicas, and the routing fabric picks the closest.

When to use anycast versus DNS-based steering

AnycastCDN (DNS-steered)
RoutingBGP selects the nearest replicaDNS returns the nearest edge server
ContentSame IP, same serviceEdge servers cache specific content
Use caseDNS root servers, CDN entry pointsWeb assets, videos, API responses

Anycast is often combined with CDNs: an anycast address serves as the entry point, and the edge server then serves cached content. The combination covers both halves — anycast absorbs the routing decision and DNS steering handles capacity-aware load balancing. Anycast failover is also automatic: if an edge site goes dark, BGP withdraws its announcement and traffic shifts to the next-nearest replica with no DNS TTL to wait out.

Compute at the edge removes the origin round trip

Modern CDNs extend beyond caching — they run compute at the edge. Edge functions execute application code (authentication, A/B testing, image resizing) at the edge location, eliminating a 100+ ms origin round trip per request. A personalization check that ran at the origin on every page view becomes a sub-millisecond edge lookup against a replicated key-value store — at the cost of an execution model with strict CPU and memory limits (typically 50 ms wall-clock and 128 MB) and eventual consistency on replicated state.

Where the latency savings show up

A well-configured CDN cuts 50–90% of latency for cacheable static content: a 1.2 MB bundle served from 25 ms away instead of 220 ms away finishes roughly 10 round trips sooner. Dynamic content improves less (20–50%) because the origin still does the work — the gain is the shorter client-to-edge leg plus connection reuse. The first byte time (TTFB) is dominated by the TCP and TLS handshake cost, which is why connection reuse and TLS 1.3 resumption (including 0-RTT, inherited by QUIC and therefore usable by HTTP/3) matter most on exactly the long-distance paths CDNs shorten.

Where this bites in production

  • Cache-hit ratio is the metric. Below ~90% for static assets, the CDN is decoration — check Cache-Control headers and query-string cache-busting before buying more edge.
  • Stale-while-revalidate hides origin slowness. Serving a slightly stale copy instantly while refreshing in the background keeps p99 flat during origin incidents; without it, every origin hiccup becomes user-facing.
  • Anycast and state do not mix. Anycast routes each packet independently, so a mid-connection route change lands on a different server. Keep anycast endpoints stateless and push session state to a shared store or a sticky layer above.

Read: HTTP/2 Multiplexing · Review: TCP Transport

Production cost model and latency numbers

To apply CDNs and Anycast Routing effectively in production systems, engineers must move beyond qualitative diagrams and quantify the exact hardware and network costs at each boundary:

Operation / BoundaryTypical LatencyDominant BottleneckProduction Mitigation
L1 / L2 Cache Access1 ns – 4 nsRegister dependency chains & pointer chasingContiguous memory layouts, cache-line alignment (64 B), and batch iteration
Main Memory (DRAM) Fetch60 ns – 100 nsTLB misses, random heap indirection, NUMA hopsHugePages (2 MB), arena/slab allocators, and pre-allocated ring buffers
Kernel Syscall / Context Switch1 µs – 5 µsUser-to-kernel mode switch, page-table flush, lock contentionNon-blocking readiness loops (epoll / io_uring), batching, and zero-copy I/O
NVMe SSD Random Read / fsync50 µs – 500 µsBlock I/O queue depth, write amplification, WAL flushesGroup commit, sequential append logs, and bounded buffer-pool pinning
Intra-Region RPC Roundtrip0.5 ms – 2.5 msSerialization, TCP/TLS framing, tail queueing ($L = \lambda W$)Connection pooling, deadline propagation, and p95 hedged requests

Failure modes, debugging, and operational checklist

In real incidents, systems rarely fail at average (p50) load—they fail when a slow dependency, skewed hot key, or resource leak pushes concurrency across a non-linear saturation cliff. When reviewing code or architecture that relies on CDNs and Anycast Routing, walk through three verification questions:

  1. What is the invariant under partial failure or contention? If a worker crashes mid-operation, a network packet is delayed and reordered, or two threads race on the same state transition, prove which atomic primitive, version number, or idempotency key prevents silent corruption.
  2. How is backpressure enforced when demand exceeds capacity? Unbounded in-memory queues, unthrottled retries, and blocking calls without explicit deadlines turn a localized 100 ms slowdown into a fleet-wide Out-Of-Memory (OOM) cascade. Always bound queue depths, propagate caller deadlines, and shed excess load early.
  3. Which metrics and traces isolate the bottleneck in under 60 seconds? Instrument saturation, queue wait time vs service execution time, cache hit/miss ratios, and error rates by failure reason so on-call engineers can immediately distinguish a compute bottleneck from an I/O or lock-contention stall.

Spotted a mistake or something broken? Report a problem

Not started

Sign in to save your learning progress.

Sign in to save