Mar 23, 2023
How to Reduce DNS Lookups?
DNS is an element that often gets overlooked by webmasters. However, if well optimized, it can hugely contribute to website speed and overall performance. So today we’re going to talk about how to reduce DNS lookup, to better understand how it benefits your site.
What Is DNS Lookup?
DNS is fundamentally the backbone of our internet. Just like an address helps you avoid the use of coordinates, so does the domain name free us from memorizing IP addresses.
Every domain is mapped to an IP address. For example, when you type google.com in your browser’s address bar, your ISP will request the nameservers associated with that domain. Without DNS, you would have to type 18.104.22.168 to reach the site.
This process of translating and finding which IP belongs to which website is what DNS lookup is all about.
So, before you can view and download all the resources with your browser, a DNS lookup has to be performed for every domain that provides the information.
However, the lookup doesn’t have to be pulled off from every resource. For example, when you make the following HTTP requests:
Though as in the example you made five requests, there are only three unique domains in total. In other words, your browser only makes three DNS lookups:
In general, when you’re visiting a web page, the browser will request all the resources with DNS lookups. And it must wait until all the processes are completed before the browser can load anything.
It can take some time, especially on the page that uses many DNS lookups. And this event can eventually cause slow load times on that website.
What Does It Matter?
According to a Google study, around 45% of visitors are reported to never revisit the same website if they encounter a bad experience. And website load time contributes significantly as one of the negative factors.
Google also uses page speed as a ranking factor. Slower page speeds can further impact web crawler and indexation too. That’s why having a fast loading website is beyond important for webmasters to succeed on the internet.
To find out how your website is performing DNS lookups and if you should worry about the need to reduce DNS lookups, you can measure its score by using web page test. Here’s an example that we took for The Longreads.
Reduce DNS Lookups Easily
Now you’ve learned about its function. All that is left is to reduce DNS lookups so your website performance can improve.
1. Using a Fast DNS Service
Same as website hosting services, there are also reliable and less reputable DNS providers out there. And using a great one can significantly reduce your DNS lookups. Among the most popular DNS services are Cloudflare, WordPress.com, Edgecast, DNSMadeEasy, and DYN.
DNS providers work quite similar to a CDN – they have multiple point-of-presence (POPs) all over the globe. And large providers, such as Cloudflare, usually have a large infrastructure to deliver location-based DNS with low-latency.
2. Optimizing DNS Caching
Thanks to DNS caching, you don’t have to worry about lookups happening on every single page. That’s because the same asset won’t need to be downloaded for a second time due to the caching process.
DNS caching works pretty similarly to a cache on a WordPress site. With this method, DNS is served from a cache until it expires. You can also set the length of a DNS cache by what we know as time-to-live (TTL) value.
You can change your TTL value with your domain registrar or third-party DNS providers to increase cache validity time and in turn, reduce DNS lookup.
If you’re on Hostinger, you can do so by modifying the value from your DNS Zone Editor.
Below are the most common TTL values:
- 1800 seconds = 30 minutes
- 3600 seconds = 1 hour
- 14400 seconds= 4 hours
- 43200 seconds = 12 hours
- 86400 seconds = 24 hours
3. Optimizing DNS Prefetching
Another way to reduce DNS lookups is by minimizing the activity in the background while visitors are browsing the site. This method is known as DNS Prefetching.
Prefetching allows a browser to fetch assets needed to display the site content at a later time. It will start downloading other resources once the idle time has passed and stored these resources are in the cache.
And when visitors click a particular link that has been prefetched, it enables a faster data transfer allowing access to the content instantly.
You can add DNS prefetch to a specific URL by adding the rel= tag to the link attribute. If you’re using WordPress, you can add the following line on the header of your site:
<!-- 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="//www.keycdn.com">
And more importantly, DNS prefetching is also supported by most of the modern browsers.
4. Enabling Keep-Alive
You can also reduce DNS lookups by enabling Keep-Alive. This is an always-active communication channel between a server and browser, helping you to load more asset files a lot faster.
For example, if you have one asset at n1.assetdomain.com and five resources at n2.assetdomain.com, it would be totaled as six DNS requests. With Keep-Alive, those resources would be loaded as two requests.
It’s also worth mentioning that this active transmission only uses a tiny message, thus consuming very little bandwidth. You can enable this function on Apache and Nginx servers.
To enable Keep-Alive on Apache server, add the following code to your .htaccess file:
<ifModule mod_headers.c> Header set Connection keep-alive </ifModule>
For Nginx servers, look for the HTTP core module (ngx_http_core_module) and search for a line that looks similar to keep alive_disable. And you can change it as in the example below:
5. Replacing CNAME with ANAME Records
CNAME records produce an additional lookup that can create a small amount of IP resolve delay. Having a few CNAMEs is usually normal. However, if your website has multiple CNAME records, you can consider removing them from your DNS records to reduce DNS lookup.
As an alternative, you can use ANAME records instead. It shares the same functionality as CNAME, but at the root level, and this translates to a faster IP resolver.
For example, if you have a configuration for www.domain.com, with CNAME, usually has to resolve the hostname first and then the IP address. And this requires two sets of different requests.
ANSWER SECTION: www.domain.com. 3599 IN CNAME domain.com. domain.com. 3599 IN A 22.214.171.124
On the other hand, ANAME record helps you to skip these requests and will return the following answer instead:
ANSWER SECTION www.domain.com. 3599 IN A 126.96.36.199
If you use Cloudflare, it also provides a feature called CNAME Flattening that shares a similar function as an ANAME record.
Huft, that’s quite a lot, let us do a quick recap about how to reduce DNS lookups:
- Using a Fast DNS Service — is a sure-fire way to have better DNS lookups, as the providers will take care of the essential elements.
- Optimizing DNS Caching — is another great way to cache your DNS. It works pretty much the same as a cache on WordPress.
- Optimizing DNS Prefetching — you can also fetch the DNS, allowing you to deliver a faster response. All of the major browsers also support this method.
- Enabling Keep-Alive — enables you to keep active dual-channel communication between a browser and a server. It consumes very little bandwidth too.
- Replacing CNAME with ANAME Records — you can also replace CNAME with a more effective, ANAME record.
Do you have any experience in optimizing your DNS lookups? Please share it in the comments section below.