Recipe
Recipe: Geo-aware request routing
Route users to the nearest edge node based on their geographic location. Reduce latency and keep data within regional boundaries.
Ingredients
- Cloudflare Workers or Vercel Edge Middleware
- Request.cf country / region / city fields
- GeoIP database (MaxMind or IP2Location) for fallback
- Regional origin servers or edge-optimized CDN
Steps
- Extract geo hints. Read
request.cf.countryfrom the incoming request on the edge. - Map region to origin. Maintain a lookup table: EU → Frankfurt, NA → Virginia, APAC → Singapore.
- Rewrite upstream. Forward the request to the closest healthy origin. Use weighted fallback if the primary is degraded.
- Set response headers. Stamp
x-served-byandx-geo-regionfor observability. - Cache regionally. Partition cache keys by region to avoid cross-region cache poisoning.
Gotchas
- VPNs and Tor exit nodes skew geo data — treat unknown as a region.
- GDPR may require EU traffic to stay in EU; encode this in routing rules.
- Stale GeoIP databases misroute traffic; refresh weekly.