Skip to content
DERKONLINE

Route Every User to Their Nearest Region With Latency Based DNS

Serve the closest healthy origin automatically so a user in Accra and one in Frankfurt both get a fast first byte.

Derrick S. K. Siawor7 min read

A user in Accra and a user in Frankfurt both open your app. If you run one server, in one place, one of them is fast and the other is paying for every kilometer of distance between them and your origin. The data still moves at close to the speed of light, but light is not instant across an ocean, and the round trips add up into a first byte that arrives noticeably later for the user who happens to be far away. Latency-based DNS routing is how you make both users fast at once, by sending each of them to the nearest healthy copy of your service automatically, without either of them ever knowing there was a choice.

The naive solution is to ask users to pick a region, or to hardcode one server and accept that half your audience gets a slower experience. The better solution lives in DNS, the system that turns your domain name into an IP address, because DNS is the first thing every request touches and therefore the earliest place you can make a routing decision. Answer the DNS query with the right server for that specific user, and the rest of the request flows to the optimal place from the very first connection.

GeoDNS: answer based on where the user is

GeoDNS returns a different IP address depending on the geographic location of whoever is asking. When a DNS query arrives, the authoritative DNS server checks the location of the requesting resolver against a GeoIP database and responds with the IP best suited to that region. A resolver in West Africa gets your African origin. A resolver in Central Europe gets your European origin. Same domain, different answer, chosen by location.

GeoDNS routing flow: DNS query, GeoIP and latency lookup, nearest healthy origin chosen, user connects to close server

The performance gain is concrete. Geographic DNS routing improves response times by roughly 30 to 50 percent compared to non-geographic routing, simply because users connect to a server near them instead of one across the world. That improvement is not a micro-optimization. It is the difference between a snappy app and a sluggish one for the half of your audience that is not sitting next to your data center.

Latency-based routing is a refinement on the same idea. Instead of inferring the best server purely from geography, it directs users to the lowest-latency server, often working alongside GeoDNS. The query that resolves to that IP can itself be slow, which is why anycast DNS cuts resolver latency without adding servers on the lookup path before the connection even opens. Geography is a good proxy for latency but not a perfect one, because network paths do not always follow the map. Latency-based routing measures the actual network distance and picks the genuinely fastest origin, which sometimes is not the geographically closest one.

Health checks turn routing into resilience

Routing to the nearest server is a performance feature. Routing to the nearest healthy server is a reliability feature, and the difference is health checks. When your GeoDNS is wired to health checks, it continuously verifies that each regional origin is actually up and responding. If a server goes down, the system automatically removes it from rotation and routes that region's traffic to a healthy origin elsewhere.

This is the part that makes geographic routing more than a speed trick. Your Frankfurt origin has a problem at 3am, and instead of European users hitting a dead server, the health check notices, pulls Frankfurt out of the DNS answers, and European traffic flows to your next-best origin automatically. How fast that switch actually takes effect depends on setting DNS TTLs so failover is instant without hammering resolvers, because a record cached for an hour will keep sending traffic at a dead origin for an hour. Slower for those users while the primary is down, yes, but online, which beats an error page every time. The same mechanism that makes users fast also makes your service survive the loss of a region without manual intervention, and that resilience is part of how reliability becomes a competitive moat nobody can copy.

Anycast: the same idea, one layer deeper

There is a related technique that operates below DNS, at the IP layer itself: anycast. With anycast, a single IP address is assigned to multiple servers in different physical locations. When traffic is sent to that anycast IP, the network's own routing protocol, BGP, automatically delivers it to the topologically nearest server. The user's packets find the closest origin without DNS having to make a per-user decision, because the routing happens in the network fabric.

Because anycast works at the IP layer rather than the DNS layer, it can react faster than DNS-based approaches, which are subject to caching and TTLs that delay how quickly a routing change propagates. The tradeoff is that anycast requires the kind of network control that DNS-based routing does not, which is why DNS routing is the more accessible technique for most teams while anycast tends to be operated by CDNs and large providers.

In practice the two combine. Modern content delivery networks layer them: DNS routing selects the nearest point of presence, then anycast routes traffic to the best edge node inside that region. A user requests your site, DNS picks the nearest region, and anycast finds the optimal node within it. The user experiences a single fast connection; underneath, two routing decisions handed them the closest healthy server twice over.

Why this matters more the wider your audience spreads

If every one of your users sits in one city, none of this matters, run one server there and be done. The moment your audience spans regions, the picture changes, and it changes in a way that is invisible from where you sit. You test from your own location, near your own server, and everything is fast, so the slowness for distant users never shows up in your experience. They feel it. You do not. This is the same blind spot that makes your Lighthouse score lie while field data tells the truth: the conditions you test under are not the conditions your distant users live in. That asymmetry is exactly why teams underinvest in geographic routing: the problem is real but it is felt by the people furthest from the developers.

For a team building from Accra and serving users across Africa, Europe, and beyond, this is not a hypothetical. The distance between your users and your origin is a tax some of them pay on every request, and routing each user to their nearest region is how you stop charging it. It is one piece of what most software teams get wrong about shipping to African markets, where serving from a distant default data center quietly excludes the audience you are trying to reach. Designing that routing is part of how we approach networking for products with a geographically spread audience, and it sits on top of server administration that keeps each regional origin healthy and the health checks honest, because a routing layer is only as good as the origins it routes to and the health signals it trusts.

Building it without overbuilding it

You do not need a global anycast network to get most of the benefit. The progression is sensible and incremental.

  • Start with a CDN in front of your origin. A CDN gives you geographic routing and edge caching with almost no infrastructure work on your side, and for many products it is the entire answer. Static content gets served near the user from a cache hit ratio you have pushed past 95 percent, and dynamic requests get routed to your origin over an optimized path.
  • Add GeoDNS with health checks when you run multiple origins. Once you have more than one regional origin, GeoDNS routes users to the nearest one and health checks keep failed regions out of rotation. This is the point where you gain both the speed and the resilience.
  • Reach for anycast when you operate at a scale that justifies it. Anycast is powerful and it is also operationally heavier. Most teams get it indirectly through a CDN long before they would ever run it themselves, which is usually the right order.

The goal is not the most sophisticated routing topology. It is that every user, wherever they are, connects to a healthy server near them, and that when a region fails, the routing quietly heals around it. Get that right and the user in Accra and the user in Frankfurt both get a fast first byte, neither one waits on a distant origin, and an outage in one region becomes a slowdown rather than a failure. That is geographic routing doing its job: invisible when it works, which is almost always.