What is a DNS Lookup?
A DNS stands for Domain Name System which maintains records pertaining to which domain name maps to which IP address. This is commonly compared to a phonebook in that people’s names are associated to a particular phone number. More specifically, a DNS lookup is the process of finding which IP the domain name belongs to. This system allows us to use user-friendly domains such as https://mydomain.com while still being able to communicate with the server that stores the requested information.
The process of performing the DNS lookup falls on the responsibility of the browser. Upon accessing a web page, the browser finds all resources which require a DNS lookup and must wait until the lookup completes before it can download anything. According to YSlow, DNS lookups on average take between 20-120ms to complete.
This article will cover how to reduce DNS lookups in order to help improve overall page load times.
DNS Lookup Example
To demonstrate an example of how DNS lookup time can impact webpage speed we’ll run a test of https://www.keycdn.com through web page test.
In total, 32 requests are made and of these, 7 requests required a DNS lookup. In total, the DNS lookup time for these resources took 1.23 seconds. As we can see, the time required to complete a DNS lookup can add up fast which is why is it important to know how to reduce DNS lookups to help improve page speed performance.
How To Reduce DNS Lookups
To process of reducing DNS lookups is actually quite straightforward. Where things get a little more granular is in optimizing the remaining DNS lookups you just can’t do without (we’ll cover that in the following section). However, to reduce the number of exisiting DNS lookups follow the suggestions below:
1. Reduce the Number of Hostnames
Simply reducing the number of hostnames that are used in order to generate your web page is the most straightforward method. Go through your site and audit the hostnames and assets that are triggering requests.
For example, if you are making external requests to generate social icon buttons, fonts, libraries, etc. these all require a DNS lookup. Determine if all of these external resources are in fact necessary and evaluate the cost/benefit ratio between keeping the external resources and the additional loading time that this creates.
2. Host Third Party Resources Locally
Hosting third party resources locally allows you to reduce DNS lookups while garnering better control of how the resource is delivered in general. Using this method, you can define how long the asset should be cached, whether it should be delivered by a CDN, and much more.
Download the files that are being referenced to an external resource and host them on your origin server or CDN. This way, resources will be retrieved without having to make an additional DNS lookup. Storing them on a CDN will help further improve loading times as users will be delivered content from the closest edge server. Additionally, hosting files on a CDN will help take a load off of the origin server.
This method however should not be used in all cases. For example, third party scripts are often times updates frequently therefore if you’re hosting the asset locally or on your CDN, you may not be using the most updated script. However, in certain instances, there are ways to host third party scripts and check periodically for updates. Learn more about how to do this for Gravatars, Google Analytics, and font libraries.
Additionally, check out our complete guide to optimizing third-party scripts and learn more.
Improve the Speed of Remaining DNS Lookups
To reduce DNS lookups by following the suggestions above is a good step towards optimizing the number of lookups required. However, what about optimizing the DNS lookups that are necessary to render the site. There are a number of ways to improve these as well, including:
1. Use DNS Cache to Your Advantage
Thankfully, due to DNS caching DNS lookups are not required every time a browser requests an asset. As long as the cache hasn’t expired, the DNS server’s cache can provide the hostname to the browser without having to look it up. What defines the length of DNS cache is what’s called TTL or time to live.
Not all DNS records should be cached equally. It is based on the type of record you use that should define what its TTL should be. There are no values which are “set in stone” however the following provide a good baseline:
CNAME Records – 24 hours
A and AAAA Records – Anywhere between 5 minutes to 1 hour
TXT Records – 12 hours
MX Records – 12 hours
Note that in many cases (like in the screenshot above) TTL values are shown in seconds instead of minutes or hours so be sure to do the conversion before you define a value.
Use DNS prefetching to allow the browser to perform DNS lookups in the background while the user browses the current page. Therefore, when the user clicks on a particular link with prefetched resources, they won’t need to wait for the DNS lookup to occur. The image below shows how prefetching in general works.
DNS prefetching is very easy to configure and can be added to your link attributes as follows:
3. Minimize the Number of CNAME Records
CNAME records produce an additional lookup which creates a small amount of added wait time. Having a few CNAME records is normal and you won’t experience much change. However, if your setup has multiple CNAME records it may be time to consider removing a few from your DNS setup. That being said, you can use ANAME records instead which give you the same functionality as a CNAME record, but at the root level. Therefore, instead of the following lookup process with a CNAME record:
www.yourwebsite.com. 43200 IN CNAMEyourwebsite.com.
yourwebsite.com. 43200 IN A 22.214.171.124
An ANAME record will return the following:
www.yourwebsite.com. 43200 IN A 126.96.36.199
Furthermore, Cloudflare also provides a solution to this that they call CNAME Flattening.
5. Use a Fast DNS Provider
Lastly, use a fast DNS provider when looking to optimize your DNS lookups. Although many DNS providers are available, they are not all created equal. Check out our complete guide to the 10 best DNS providers to learn more about what’s available and how they compare. Do your research before making a final decision and run some performance tests to compare them against your current DNS provider.