There are a lot of different optimizations when it comes to WordPress, and some of them are more important than others. A factor that is overlooked  is the lookup times associated with DNS, and just how much impact it has on your site. Just like TTFB and network latency, it’s an important part of considering how much time it takes to load a website. So let us today look into certain factors which will tell us how to reduce DNS lookups and speed them up, why you should, and how it plays a part in the performance of your WordPress Site.

What is DNS Lookup?

To explain what DNS lookup is you should consider what DNS is and how it works. DNS is the backbone of the internet. A common way to consider how DNS is, is that it is like a phonebook of the world wide web. Every address and domain you visit can be listed as an IP address:

When you enter a website address like google.com, there is an DNS lookup and request done by your ISP provider to request the nameserver associated with the domain. Then the interlocking the DNS Lookup with the IP address is done which allows you to access the information on the website. Without the help of a DNS lookup you would have to type the IP Address to get to Google.

When you browse a website, a DNS lookup has to be made for each domain that is requested before anything is downloaded. However, a DNS lookup is not performed on each resources. So for example, let’s say we have the following HTTP requests:

https://wpdev.ink/
https://js.stripe.com/v3/
https://cdn.wpdev.ink/wp-content/themes/twentyseventeen/assets/images/header.jpg
https://cdn.wpdev.ink/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js
https://cdn.wpdev.ink/wp-content/themes/twentyseventeen/assets/js/global.js
https://cdn.wpdev.ink/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js
https://cdn.wpdev.ink/fonts/open-sans-v13-latin-regular.woff2
https://cdn.wpdev.ink/fonts/open-sans-v13-latin-700.woff2

Even though there are eight DNS and HTTP requests above, there are only three unique domains. Which means there are three DNS lookups.

https://wpdev.ink
https://js.stripe.com
https://cdn.wpdev.ink

DNS Lookup in Pingdom

Below is how the website request would look in Pingdom website speed test tool. DNS is measured in the pink bar and measured in ms. When you enter a new domain address to look up in the tool it performs a fresh look up, and it has to query the DNS record of each domain to get the IP information. Notice it doesn’t have to query DNS multiple times on the cdn.wpdev.ink domain. This is how DNS works. It only has to query a  domain once. There are a total of 8 domains but only 3 DNS lookups.

The DNS lookup results in additional load time, due to the fact that nothing can load until the DNS lookups are finished. Notice below that the DNS lookup on the one CDN URL took over 300 ms. And this doesn’t include the lookups on wpdev.ink and js.stripe.com. So you can see how this can add up over time.

When you run your website again in the Pingdom tool, it doesn’t not require to look up the DNS as it has already cached the term and doesn’t take much time. This is the reason why your website appears faster after running it through Pingdom multiple times. As you can see in the screen below, on the 2nd test, the DNS lookup time on the CDN URL is 0ms. This is one area where a lot of people misinterpret. But don’t worry we will dive into DNS cache below.

DNS Lookups in GTmetrix

Every website tool which test speed, works differently but calculates the time take to check out the DNS lookup times the same way. Below is how the same requests appears in GTmetrix, DNS is represented in the green bar and measured in ms.

DNS Lookups in WebPageTest

Here is an example using WebPagetest. They have an overview of all the test which are performed and can be cliked on to check the DNS values and it is sorted by highest response time. We ran usatoday.com through the tool as an example, and the DNS lookup time alone took 6.5 seconds!

It is highy common for all these news sites to not be optimized because of the external requests. But as you can see, the DNS lookup time on their site probably took more time than most people’ entire sites. This is why DNS does matter.

How to Reduce and Speed Up DNS Lookup

Now that you know how DNS works, we will look at some examples of how to reduce DNS lookup time on your WordPress Site. In some websites this is also referred to as minimum DNS lookups.

Tip 1- Use a Fast DNS Provider

You need to understand along with the speed of hosting, there are also faster and slower DNS providers out there. This should be the first thing you fix.

Typically the free DNS provided by domain registrars like GoDaddy and Namecheap is very slow. DNS work like CDN providers, they have multiple POPS across the globe. Large DNS providers out there like Amazon, Cloudflare, Dyn, and DNS Made Easy, all have massive infrastructure’s specifically designed for DNS with low-latency environments.

We ran a few test with a few DNS providers out there. We use KeyCDN’s performance tool as well as SolveDNS speed test, and took the average. Speeds were measured from multiple locations across the globe. As you can see, except for Cloudflare, free DNS providers provide slower speed. This is why if you take your business seriously you should think about getting your website a premium DNS provider.

Tip 2- Change TTL Values to Take Advantage of DNS Cache

Thankfully, like we said before due to the DNS lookup, you don’t need to worry about to worry what is happening on every single page load. It is really about first time lookups. DNS lookup works on similar ways on how you cache your WordPress site. DNS is served from cache until it expires. The length of the DNS cache is defined by the lookup of the time-to-live cache load time. The higher the cache time the less likely it will perform the DNS Lookup.

TTL values can be changed with your domain registrar or 3rd-party DNS provider to improve your DNS cache times. It is important to note that ISPs also cache DNS. Below are a few common TTL Values.

  • 300 seconds – 5 minutes
  • 1800 seconds- 30 minutes
  • 3600 seconds- 1 hour
  • 43200 seconds- 12 hours
  • 86400 seconds – 24 hour

30 minutes to an hour are the most commonly used TTL Values. However, some companies set their TTL values low for faster updates. Cloudflare uses a TTL value of 5 minutes if you use their automatic TTL. It can also be beneficial to look at your different types of DNS records and set them accordingly based on how often they change. Here is an example:

  • A and AAAA records: Change more frequently, anywhere from  5 minutes ot an hour is usually OK
  • CNAME records: Almost never change, a higher TTL like 24 hours is usually OK
  • MX records: Change less often, a high TTL like 12 hours is usually OK
  • TXT records: Change less often, a high TTL like 12 hours is usually OK

There are no right answers and wrong answer when it comes to TTL. But it will improve DNS lookup times by choosing a TTL that coincides with how often changes are made on your site.

Tip 3- Reduce Number of Domains

The easiest way to reduce DNS lookup times on your site is to get away from requests that query different hostnames. Remember, with DNS lookup it is not only to do with number of requests, but also to do with different domains. Run your WordPress site through a tool like Pingdom and determine if each request is really necessary. This is because IPs changes frequently, but hostnames never do.

Tip 4- Use Alternative Services with Faster DNS

While replacing and reducing the number of domains is easier said than done it would be better to first look up those with higher DNS lookup times. For example, in this site we see that the lookup times of the DNS is taking longer than required for CrazyEgg. This is probably because DNS lookup is not as fast as some of the others.

This is one scenario where perhaps you could look at other providers such as Hotjar, who provide the same service but perhaps are utilizing a faster DNS provider. It can be very important when adding external services to your WordPress site that you evaluate their performance.

Tip 5- Move and Host Resources on a CDN

One of the easiest ways to reduce DNS lookup time is to move resources to a CDN provider. If you run the site through Pingdom, you can see the total number of requests by domain. As you can see in this example, 93.8% of the requests are to the CDN URL. By moving as many resources to the CDN as possible, this reduces the number of DNS lookups involved, therefore decreasing the load times.

Now for sure, it won’t be easy to move everything to a CDN. There will always be some external resources which you would need to load from their servers. But we recommend looking through your site. Many times, we see that WordPress users have a lot of resources not properly hosted on their CDN. By doing this, you can also take better advantage of HTTP/2 and parallelization. Below we see certain resources which could be improved:

Font Awesome

We often see that developers and designers use the Font Awesome on the Font Awesome on WordPress sites, but sometimes they enqueue it in such a way that the CDN enabler won’t catch it. It is in such times that we reach out to the developer and ask them to load it from your CDN.

Another common scenario with Font Awesome is that it’s sometimes loaded from the default BootStrapCDN. While there is nothing wrong with this, it adds another DNS lookup that you don’t need. It would be better to load Font Awesome from your CDN so that there is no lookup.

Gravatars

If you are working with WordPress and you need to load a comment on the website, you may require a lookup to Gravatars secure.gravatar.com to load the gravatar. One solution to this is to use lazy loading plugins. While this won’t reduce the DNS Lookup. It will delay it until the user scrolls down the page. So you are reducing the DNS lookup on first-page load. Make sure to check out our blog post on other ways to speed up comments.

Google Fonts

While Google Fonts have one extra DNS request to lookup fonts.googleapis.com to grab the CSS file. You have extra requests to lookup font Google with the lookup for fonts.gstatic.com to download the file. If you load all the attributes locally, you won’t need to lookup from the same domain or CDN, you won’t need another CSS file as this will be inside the WordPress theme’s CSS file.

Tip 6= Take Advantage of DNS Prefetching

Another tip to speeding up DNS is to use DNS prefetching. This helps user perform DNS lookup on a page n the background. You can do so by adding some lines of code to the header. See the examples below.

<!-- Prefetch DNS for external assets -->
<link rel="dns-prefetch" href="//fonts.googleapis.com">
<link rel="dns-prefetch" href="//www.google-analytics.com"> 
<link rel="dns-prefetch" href="//cdn.domain.com">

DNS prefetch is supported on all browsers except for iOS Safari, Opera Mini and Android Browser.

Tip 7 – Defer Loading of JavaScript

If you defer the loading of JavaScript, this can stop files from loading, until the whole document is loaded. This won’t prevent the number of DNS Lookups required, but it can delay it until everything is loaded, and improve user experience. Varvy has a great explanation of how to defer JavaScript. An example he does it by loading it on Google Analytics Script, as there is no reason it should load before everything.

In WordPress, you can use Async JavaScript to defer the loading of JavaScript. However, it is important to know that you will most likely have to go through and list any scripts which you would like excluded from having deferred applied. Because some Javascript, will be required to be rendered above the fold. Async JavaScript is also fully compatible with the Autoptimize plugin.

Tip 8 – Take Advantage of ANAME Records and CNAME Flattening

There are some additional records available from providers like DNS Made Easy which can help reduce the number of request on the backend.

ANAME Records with DNS Made Easy

ANAME Records give the functionality of CNAME Records, but at the root level. So for example if you have a CNAM configured for www.domain.com. The www has to first resolve to the host name and then the IP address with been resolved. This requires two requests. With an ANAME records, you can skip one of these requests. If setup in conjunction with CDN it can be used properly.

CNAME

ANSWER SECTION:
www.domain.com. 1799 IN CNAME domain.com.
domain.com. 1799 IN A 192.168.1.2

ANAME

ANSWER SECTION:
www.domain.com. 1799 IN A 192.168.1.2

CNAME Flattening with CloudFlare

Similarly to ANAME records, Cloudflar provides automatic CNAME Flattening at the zone apex for their customers.

 

Original Article Credit : kinsta.com