Skip to main content Skip to search

Behind the Scenes: How Geographic Routing Works

What is Geographic DNS Routing?

The basic concept behind geographic routing, also known as geolocation or geo-locational routing, is to return a DNS answer that is physically closest to the requestor.

For example, if you have EC2 instances hosted in the Amazon AWS us-west-1 region, and additional instances in the us-east-1 region (shown on the right), and a user located in Las Vegas issued a DNS request (pink icon), then geographic routing would return the us-west-1 (green arrow) answer as it is the closest answer.

To accomplish this, two pieces of information must be known at the time the DNS request is received on an NS1 nameserver, and a calculation operation must be performed in real-time.

The two requisite data points are:

  1. The location of each of the answers configured within the DNS record that is being queried.
  2. The location of the IP address from which the DNS query was received.

Both data points are latitude and longitude numbers, which are reference points for identifying a precise location on the face of the earth, also known as GPS coordinates.

How Geolocation DNS Routing Works

When the DNS query arrives on an NS1 nameserver, it is checked to see if the EDNS0 option known as edns-client-subnet is present, and if the record being queried has edns-client-subnet enabled. If both EDNS0 conditions are met, the class C or /24 subnet specified by edns-client-subnet is used. This is the first three octets ( of the real end-user’s public IP address, and allows for much more accurate geolocation targeting.

If the EDNS0 option is not present, or the record being queried does not have edns-client-subnet enabled, the IP address from which the query was received is used. This is typically a caching or recursive resolver, which might not be located anywhere near the actual user. This option will determine the routing based on the location of the recursive resolver and return the appropriate address for that location.

The IP address is looked up in real-time in an IP address-to-location database, which contains base data from MaxMind and undergoes constant refinement and improvement with extensive IP address mapping by NS1. It tells the NS1 resolver the latitude and longitude of the source of the DNS request, either using the EDNS0 option, or the IP address of the recursive resolver.

Next, unless a precise latitude and longitude was assigned to each of the record’s answers, the NS1 resolver uses the latitude and longitude that represents the midpoint of whatever location is assigned to each answer. In other words, if an answer is marked as being in the us-west region, then the geographic center of the western United States is used. On the other hand, if an answer is marked as being in the state of New York, then the latitude and longitude of the exact middle of NY is used.

The distance from the query source IP address’ latitude and longitude to the determined latitude and longitude of each of the answers is calculated in mere microseconds (millionths of a second) and the answers are sorted by decreasing proximity – closest answer on top, and furthest answer on the bottom. If the record is to return only a single final answer, then typically the SELECT_FIRST_N filter is employed to return only the top-most, and in this case closest, answer.

Learn more about geographic routing with NS1.