DNS resolution is the first thing that happens when a request is made to a remote server. It is a process of finding the computer-friendly address of the remote server using a human-friendly domain name.
There are few performance improvement possibilities like perfect cache invalidation time. Preferring A record over CNAME. But before all these let's understand how DNS resolution actually works.
- A network request is made to the server using its domain name.
- The browser first checks its DNS cache, if present use the IP address else asks Operating System.
- Operating System checks its cache (and few more things like host file) if present return it else asks resolver.
- Resolvers are ISP(Internet Service Provider) servers but 5. can be configured to any other DNS service provider.
- Resolvers check if the domain is in its cache return it else look for root servers.
- Root servers are top in the DNS hierarchy and know the address of TLDs(top-level domain like .com, .net, .org, etc.) servers.
- Based on the type of domain, Root servers direct resolver to corresponding TLD servers. For a .com domain, root servers direct to .com TLD servers.
- TLD servers know the address of name servers.
- Name servers know the real IP address of the requested domain. The browser receives the resolved IP address.
Ahh too confusing and too much to remember. Let's keep it simple and know what is in our control and find the scope of performance improvements.
A registrar is a place where one buys a domain. The registrar provides name servers and a few other DNS management tools.
Example Registrars and DNS management tools
- Amazon Web Service(AWS) — Route 53
- Google Cloud — domains.google.com
The main job of DNS management tools.
- Provide name servers details to TLDs.
- Add domain validation and authorization config for third parties.
- Define cache invalidation time or Time To Live(TTL). It’s a duration for which resolvers, browsers, etc can cache IP addresses in their cache.
- Define how to resolve a particular request. For mails check MX record. For HTTP/HTTPS check A, AAAA, or CNAME record.
Increasing cache invalidation time will ensure that the domain IP addresses will be served from the nearest cache. This will result in low latency DNS resolution.
This will be a problem in cases where domain to IP mapping is frequently changed.
To handle such cases follow these steps.
- Set TTL to 0 to avoid the cache of any new request.
- Wait for the previous TTL value time to ensure the previous cache is invalidated.
- Make the new changes.
This will fix any downtime possibilities from any known changes. But what if the server crashed or something unexpected happened. For such cases keeping a static IP and assigning it to a new server will help.
Let's look at the TTL value of some popular domains.
- apple.com — 60 min
- microsoft.com — 60 min
- Booking.com — 24 hrs
- google.com — 5 min
- indiatimes.com — 20 min
- godaddy.com — 10 min
- azure.microsoft.com — 60 min
Increasing TTL is a tradeoff between availability and performance. Controlling availability with high TTL is possible but needs extra effort and care.
CNAME or Canonical Name is like recursion where one domain resolves to another domain. The DNS resolution algorithm keeps looking until it finds the real IP address.
In most cases replacing CNAME will not be possible because of no control over the resolved IP address. This rule is only applicable for cases where IP address is known but still, CNAME is preferable due to unmanageable DNS records. DNS records if not maintained properly becomes unmanageable in most mid to large-scale organizations.
Some CDN and DNS service providers use the concept of CNAME Flattening to resolve IP directly without going through the whole chain of DNS resolution. Opt for it if your CDN or DNS service provider has support for it.
CDN works in two ways.
- Ask to replace registrar name servers with their name servers.
- Ask to add CNAME for domain resolution.
Both approaches have their own Pros and Cons. The first one is good for fast DNS resolution. The second one gives more flexibility and control to the maintainer.
CDN has other limitations they are yet not equipped to serve dynamic content. There is some development in this area like using lambda on the edge(AWS) but still, there is a long path to cover.
The purpose of name servers is to provide a real IP address that corresponds to a domain. Using custom logic a domain can be resolved to a different IP each time it receives a request.
CDN uses this approach to serve content from the nearest host to the user but they can’t be used for dynamic content.
Using custom name servers to resolve IP addresses based on the region can significantly reduce latency.
- In California for users in North America
- In Paris for users in Europe
- In Mumbai for users in Asia
- In Sydney for users in Australia
Now people in North America can get their content directly from servers in California and people in India get content from Mumbai. Data belongs to a region can be stored along with the region and fallbacks to other regions.
This gives a lot of flexibility for dynamic logic without any compromising on performance.
There are lots of complexities in this approach. One problem is database sharding keeping a region's data close to it but still able to serve content from other shards.
Many large scale organizations use this approach to distribute traffic and serve the request quickly.
Improving DNS resolution can have a huge impact on the performance of a site. But every possible optimization has some cost.
- Increasing Time to Live(TTL) reduces latency but impact availability.
- CNAME is not always replaceable with the corresponding A record.
- CDN’s are not yet ready for dynamic content. Custom name servers are hard to put in place.
- Figure out appetite for performance at your org and accordingly tune DNS settings.
Previously published at https://ashu.online/blogs/optimize-dns-resolution-for-fast-website