How to Reduce DNS Lookups
If you're concerned about website performance, then reducing DNS lookups is an essential step that you can take. DNS (Domain Name System) lookups refer to the process of translating a domain name into an IP address. Each time a browser requests a resource from a domain, it needs to perform a DNS lookup to resolve the domain name to an IP address. DNS lookups can slow down your website's load times, particularly if you're using external resources such as third-party plugins or CDNs.
In this article, we'll provide you with some practical tips on how to reduce DNS lookups and improve your website's performance.
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
www.example.com while still being able to communicate with the server that stores the requested information.
When you enter "example.com" in your address bar, your ISP initiates a DNS query to retrieve the domain's associated nameservers. The server then performs the mapping to the IP address, which allows you to access the website using its domain name. Without the DNS system, you would need to type the IP address (220.127.116.11) to access the site. Imagine how much fun that would be!
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.
On average, DNS lookup times can range anywhere from 20-120 milliseconds, but in some cases, they can take several seconds or even longer, particularly if the DNS server is overloaded or experiencing network issues. For optimal website performance, it's generally recommended to aim for a DNS lookup time of less than 50 milliseconds.
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 Website Speed Test.
In KeyCDN's Website Speed Test, the DNS queries are displayed as a light pink bar and are measured in milliseconds. When you conduct the test for the first time, it performs a new lookup and queries the DNS record of each domain to obtain the IP information. However, it does not need to query the DNS on the domain keycdn.com multiple times. This is how DNS operates, and it only needs to query a domain once.
In the given example, there are six queries, but only two DNS queries occur, which is a positive result. If there are more DNS queries, it could indicate a decrease in performance, and you could try to consolidate the different data sources (hostnames).
It is crucial to monitor the DNS lookup time in milliseconds. In total, the DNS lookup time for these resources took 1.46 milliseconds. If the DNS requests exceed 100 milliseconds, it may indicate that you have a slow DNS provider or that there are issues with the DNS provider.
Below is the result we got from another WebPageTest some time ago. The website was not yet as optimized as it is today, and we see a significantly worse performance.
Back then, a total of 32 requests were made, 7 of which required a DNS lookup. In total, the DNS lookup for these resources took 1.23 seconds. As we can see, the time required for a DNS lookup can quickly add up. Therefore, it is important to know how to reduce DNS lookups to 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.
Here are some strategies for reducing the number of hostnames on your website:
Consolidate resources: Instead of hosting your website's resources on multiple subdomains, consider consolidating them onto a single domain. This will reduce the number of hostnames and, therefore, the number of DNS lookups required to load the page.
Use a content delivery network (CDN): A CDN can help to reduce the number of hostnames required by serving resources from a network of servers located around the world. Instead of requesting resources from your server, users will request them from the closest CDN server, which can help to reduce latency and improve the speed of your website.
Avoid third-party resources: Third-party resources, such as social media plugins or analytics tools, often require their own hostnames, which can increase the number of DNS lookups required by your website. Try to limit the use of third-party resources whenever possible or consider using a service that provides these resources through a single hostname.
By reducing the number of hostnames on your website, you can help to minimize the number of DNS lookups required and improve the overall performance of your website.
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.
2. DNS prefetching
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:
<link rel="dns-prefetch" href="https://www.yourlink.com">
Read more about prefetching in our complete prefetching guide.
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 18.104.22.168
An ANAME record will return the following:
www.yourwebsite.com. 43200 IN A 22.214.171.124
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.
6. Minimize the TTL of DNS records
The TTL, or time to live, of a DNS record determines how long a DNS resolver will cache the IP address associated with a domain name. By setting a lower TTL for your DNS records, you can ensure that changes to your DNS configuration take effect more quickly. This can help to reduce the time it takes for DNS lookups to complete, especially during periods of high traffic or when changes are made to your DNS configuration.
By using these techniques, you can help to optimize the speed of any remaining DNS lookups required by your website, which can help to improve its overall performance and provide a better user experience for your visitors.
Keeping an eye on and reducing DNS lookups is essential if you want to optimize your site's performance. With so many third party services now available, it's easy to stack up a slurry of external requests which really adds a lot of latency for your website visitors.
By using a CDN, minimizing the number of external resources, using DNS prefetching, reducing the TTL of DNS records, choosing a DNS provider with a global network, and removing unused DNS records, you can help to ensure that your website loads quickly and efficiently.
Remember to regularly review your DNS configuration to ensure that it's up-to-date and correctly configured. By following these tips, you can help to improve your website's performance and provide a better user experience for your visitors.