There are a lot of different optimizations you can make when it comes to WordPress, and some are more important than others. A factor that is often overlooked is the lookup times associated with DNS, and just how much of an impact it has on your site. Just like with TTFB and network latency, it’s an important piece of the puzzle when it comes to calculating your total page load times. So today we’ll dive into a few recommendations on 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 show you what a DNS lookup is, you need to first understand how DNS works. DNS (domain name system) is essentially the backbone of the internet. A common reference to explaining how DNS works is to think of it like a phone book for the world wide web. Every website and domain you visit are all mapped to an IP address.
When you type Google.com into your address bar, a DNS query is performed by your ISP to request the nameservers associated with the domain. The mapping to the IP address is then done behind the scenes by the server which allows you to then use the domain name to access it. Without DNS you would have to type something like 220.127.116.11 to get to Google. Wouldn’t that be fun!
Tired of hassling with slow WordPress hosting and 500 errors?
When you browse to a website, a DNS lookup has to be made for each domain that is requested before anything is downloaded. However, a DNS lookup doesn’t have to be performed on every resource. 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 is a total of eight HTTP requests above, there are only three total unique domains. Which means there are three DNS lookups.
https://wpdev.ink https://js.stripe.com https://cdn.wpdev.ink
DNS Lookups in Pingdom
Below is how the above requests appear in the Pingdom website speed test tool. DNS is represented as the pink bar and measured in ms. When you first run your website through Pingdom, 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. That is how DNS works. It only has to query a domain once. So there are 8 total requests below, but only 3 DNS lookups.
This DNS lookups result 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 that doesn’t include the lookups on wpdev.ink or js.stripe.com. So you can see how this can add up over time.
When you run your website through Pingdom more than once, it caches the DNS because it already knows the IP information and doesn’t have to perform the lookup again. This is one 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 0 ms. This is one area a lot of people misinterpret. But don’t worry, we’ll dive more into DNS cache below.
DNS Lookups in GTmetrix
Every website speed test tool works slightly different, but almost all of them calculate DNS lookup times for you. Below is how the same requests appear in GTmetrix. DNS is represented as the green bar and measured in ms.
DNS Lookups in WebPagetest
Here is an example using WebPageTest. They actually have a quick overview of your request details in which you can click the “DNS lookup” column and sort it by the highest response times. We ran usatoday.com through the tool as an example, and the DNS lookup time alone took 6.5 seconds!
It is fairly common for a lot of these news sites to not be highly optimized due to all of their external requests. But as you can see, the DNS lookup time on their site probably took more time than most people’s entire sites. This is why DNS does matter.
How to Reduce and Speed Up DNS Lookups
Now that you know a little more about how DNS works, we’ll show you some recommendations on how to reduce DNS lookups and speed them up on your WordPress site. In some website speed test tools, this is also sometimes referred to as “minimize DNS lookups.”
Tip 1 – Use a Fast DNS Provider
It is important to understand that just like with hosting, there are faster and slower DNS providers out there. This should be one of the first things you fix.
Typically the free DNS provided by domain registrars like GoDaddy and Namecheap is very slow. DNS providers typically work just like a CDN, they have multiple POPs around 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 tests to compare the speeds of some popular DNS providers. We used 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, other than Cloudflare, the free DNS providers are definitely slower. That’s why if you are serious about your business and website we highly recommend going with a premium DNS provider.
Some of the providers above are also faster than others in certain regions, so it is important to determine where you need fast DNS lookup times, regionally or globally. DNSPerf is another newer DNS speed comparison tool you could use when choosing a provider. Did you know that you can also use Cloudflare’s DNS without their CDN functionality?
Tip 2 – Change TTL Values to Take Advantage of DNS Cache
Thankfully, like we mentioned above, due to DNS Cache you don’t have to worry about lookups happening on every single page load. It is really about the first time requests. DNS cache works similarly to how you cache your WordPress site. DNS is served from cache until it expires. The length of DNS cache is determined by what they call a time-to-live (TTL) value. The higher the TTL, the less likely the browser will need to perform another 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 hours
30 minutes to an hour are typically the most commonly used TTL values. However, some also set their TTL very low to allow for quick updates. Cloudflare is one example that defaults to 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 to 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 is really no right or wrong answer when it comes to TTL. But it can improve your DNS lookup times by choosing a TTL that coincides with how often changes are made on your site.
Tip 3 – Reduce Number of Domains (hostnames)
The easiest way to reduce DNS lookups on your site is to simply get rid of requests that query different hostnames. Remember, with DNS lookups it is not about the number of requests, but about the number of different domains. Run your WordPress site through a tool like Pingdom and determine if each request is really necessary. Since DNS is a mapping of a hostname to an IP, you might wonder why people don’t just point to an IP instead? This is because an IP address can change frequently, whereas hostnames will not.
Tip 4 – Use Alternative Services With Faster DNS
While simply reducing the number of domains (hostnames) is easier said than done, we recommend looking first at those with higher DNS lookup times. For example, in this site we tested below, we can easily spot that the DNS lookup time to the Crazy Egg tracking script was a lot longer than others. This is most likely due to their DNS provider not being 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 lookups is to move as many resources as you can to your CDN provider. If you run your 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. There is one request to the host, and one to Google Analytics. By moving as many resources to the CDN as possible, this reduces the number of DNS lookups involved, therefore decreasing the load times.
Now, of course, it won’t always be possible to move everything to your CDN. There will always be some external resources or services that require you load them 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 are a few resources we often see that could be improved:
We occasionally see plugin developers and themes include Font Awesome on WordPress sites, but sometimes the enqueue it in such a way that by default a plugin like CDN enabler won’t catch it. In this case, we recommend reaching out to the developer to ask them how you can load it from your CDN.
Another common scenario with Font Awesome is that it’s sometimes loaded from the default BootstrapCDN (MaxCDN). While there is nothing wrong with this, it adds another DNS lookup that you don’t need. It would be better in most cases to load Font Awesome from your own CDN so there isn’t another lookup.
If you are using WordPress native comments you might have an additional lookup to secure.gravatar.com to load the gravatars. One easy solution here could be to utilize the lazy load comments plugin. While this won’t reduce the DNS lookup, it will delay it until the user scrolls down the page. So in a sense, 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.
With Google Fonts you have one extra DNS request to lookup fonts.googleapis.com to grab the CSS file. You then have additional requests to fonts.gstatic.com to download the font files. When you host locally all the requests are from the same domain or CDN, and most likely you will not need another CSS file as it will be inside of your WordPress theme’s CSS file. There are pros and cons of doing this, but check out our in-depth article on how to host fonts locally.
Font Awesome, Gravatars, and Google Fonts are just a couple examples of ways you can reduce DNS lookups by ensuring they are loading from your CDN. Are you currently loading everything possible from your CDN? It’s always good to take a look.
Tip 6 – Take Advantage of DNS Prefetching
Another tip on speeding up DNS is to use DNS prefetching. This allows the browser to perform DNS lookups on a page in the background. You can do so by adding some lines of code to the header of your WordPress site. See some 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 also supported by all major browsers except for iOS Safari, Opera Mini, and Android Browser.
Or if you are running WordPress version 4.6 or newer, you might want to use resource hints. Developers can use the wp_resource_hints filter to add custom domains and URLs for dns-prefetch, preconnect, prefetch or prerender.
Tip 8 – Take Advantage of ANAME Records and CNAME Flattening
ANAME Records With DNS Made Easy
ANAME records give you the functionality of a CNAME record, but at the root level. So for example, let’s say you have a CNAME configured for www.domain.com. The www has to first resolve to the hostname, and then resolve to the IP address. This requires two requests. With an ANAME record, you can skip one of these requests! If setup correctly these can also be used in conjunction with a CDN.
ANSWER SECTION: www.domain.com. 1799 IN CNAME domain.com. domain.com. 1799 IN A 192.168.1.2
ANSWER SECTION: www.domain.com. 1799 IN A 192.168.1.2
However, this is not always true. If the hostname is in the same DNS zone, the A record might be retrieved in the same request.
CNAME Flattening With Cloudflare
Similarly to ANAME records, Cloudflare provides automatic CNAME Flattening at the zone apex (or root) for their customers.
DNS is often one of those performance factors that is overlooked, but it can easily contribute to a large portion of your overall load time if you aren’t careful. It’s important to understand how DNS works, that there are faster and slower providers out there, and how you can reduce lookups to speed up your site.
Do you have any additional tips on speeding up DNS or reducing lookups? If so, we would love to hear about them below in the comments!