Routing logic for multi-region CDN content delivery
Multi-region delivery hinges on deterministic routing at the edge: before forwarding to origin, the CDN resolves geographic proximity, latency, and cache topology. Get it wrong and you get cache divergence, stale propagation, and split-brain hydration where APAC users receive EU-cached content during a regeneration window. A predictable routing layer is the foundation of any multi-region Data Fetching & Caching Strategies setup.
Edge evaluation and cache topology
Routing runs independently of the origin fetch cycle. Edge nodes evaluate each request against rules that prioritize geographic proximity, then fall back to latency-based routing when a primary region degrades — using the client ASN, an IP geolocation database, and historical round-trip times. The request goes to the nearest healthy node; on a cache miss, that node makes a controlled origin fetch.
This is the routing decision the edge runs per request:
flowchart TD
A["Request at edge node"] --> B["Resolve geo proximity (ASN + IP geo)"]
B --> C{"Primary region healthy?"}
C -->|no| D["Latency-based fallback to next region"]
C -->|yes| E["Select nearest region"]
D --> F{"Edge cache HIT?"}
E --> F
F -->|hit| G["Serve cached payload"]
F -->|miss| H["Controlled origin fetch (regional CMS)"]
H --> I["Respect data residency + Cache-Control"]
I --> G
The fetch must respect regional data residency and align with the broader Content Delivery Network Routing Logic. Without synchronized Cache-Control headers and consistent TTLs, the edge serves mismatched payloads during a regional failover. Enforce header hygiene at the CMS so cache fragments don’t accumulate.
ISR, hydration, and data residency
Frontends fetch through a global load balancer, but Next.js ISR and client hydration add background fetch cycles that can bypass edge routing. If an ISR worker hits a different regional origin than the node serving the user, cache consistency fractures. Pin revalidation endpoints to the same routing topology as the initial fetch to avoid split-brain regeneration.
Frameworks run ISR through background workers that respect regional boundaries, but origin routing must be configured explicitly. The Next.js ISR documentation covers scoping revalidation per region so background updates don’t cross a data-sovereignty boundary.
Deterministic routing via edge middleware
Cloudflare Workers and Vercel Edge Middleware run before the fetch layer. This pattern maps the geo header to a regional CMS endpoint, injects routing metadata, and rewrites the request with explicit cache directives.
// middleware.ts (Next.js / Vercel Edge Runtime)
import { NextRequest, NextResponse } from 'next/server';
const CMS_REGION_MAP: Record<string, string> = {
US: 'https://api.cms-na.example.com/graphql',
DE: 'https://api.cms-eu.example.com/graphql',
JP: 'https://api.cms-apac.example.com/graphql',
};
export function middleware(request: NextRequest) {
// Edge-provided geo header selects the regional origin
const geoRegion = request.headers.get('x-vercel-ip-country') || 'US';
const targetOrigin = CMS_REGION_MAP[geoRegion] || CMS_REGION_MAP['US'];
// Inject routing metadata the CMS can read
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-cms-region', geoRegion);
requestHeaders.set('x-forwarded-host', new URL(targetOrigin).host);
// Rewrite to the regional CMS
const url = new URL(request.url);
url.host = new URL(targetOrigin).host;
return NextResponse.rewrite(url, {
request: { headers: requestHeaders },
});
}
// Scope to CMS API routes only
export const config = {
matcher: ['/api/cms/:path*', '/_next/data/:path*'],
};
The CMS must honor x-cms-region to return localized data and respect compliance boundaries. Rewriting at the edge instead of proxying through a central origin removes cross-region latency and sheds origin load during spikes.
Cache directives and revalidation windows
Routing is only as good as the directives behind it. Pair region-aware routing with Cache-Control: s-maxage=3600, stale-while-revalidate=86400 — the edge serves cached content for an hour, then refreshes in the background for up to 24 hours, so users never block on a revalidation. This follows the MDN Cache-Control reference.
The CMS must return consistent ETag and Last-Modified validators. Edge nodes use them to decide whether a background fetch is needed; without them, regional nodes trigger redundant origin fetches and defeat the distributed cache.
Validation
Automated testing for headless integrations must verify routing across simulated regions. Inject x-vercel-ip-country or cf-ipcountry with synthetic monitors and assert the response came from the expected regional endpoint. Assert cache validators (x-cache / x-vercel-cache) match expected states — HIT, MISS, STALE, REVALIDATED.
Run these region-specific checks in CI when deploying new schemas or routing rules, so TTLs, headers, and endpoint mappings stay synchronized and cache divergence can’t ship silently.
Region-aware middleware, synchronized TTLs, and validated routing paths are what separate consistent low-latency global delivery from fragmented split-brain failures.