CHAPTER 9, Rule 9: Reduce DNS Lookups
- DNS resolver has a cost. It typically takes 20–120 milliseconds for the browser to look up the IP address for a given hostname.
- The browser can’t download anything from this hostname until the DNS lookup is completed.
- DNS lookups are cached in different locations for better performance:
- on a special caching server maintained by the user’s ISP.
- local area network.
- in the operating system’s DNS cache (the “DNS Client service” on Microsoft Windows).
- browsers own caches.
Factors Affecting DNS Caching
- The DNS record returned from a lookup contains a time-to-live (TTL) value. This tells the client how long the record can be cached.
- Operating system caches respect the TTL,
- Browsers often ignore it and set their own time limits.
- The Keep-Alive feature of the HTTP protocol, can override both the TTL and the browser’s time limit (i.e. as long as the browser and the web server are communicating and keeping their TCP connection open, there’s no reason for a DNS lookup).
- Browsers put a limit on the number of DNS records cached (i.e. earlier DNS records are discarded).
- If the browser doesn't have a DNS record, the operating system cache will be checked, if it is not there, the local area network or the ISP cache will be checked.
- When the browser does a DNS lookup, the DNS resolver returns the amount of time remaining in the TTL for its record. (that is because the DNS entry has already lived for an amount of time in this DNS resolver).
- For example, if the maximum TTL is 5 minutes, the TTL returned by the DNS resolver ranges from 1 to 300 seconds.
DNS From OS and Browser’s Perspective
- The DNS cache on Microsoft Windows is managed by the DNS Client service
- to view the cache : ipconfig /displaydns
- to fulsh: ipconfig /flushdns
- Rebooting clears the DNS Client service cache
- These settings created in the registry key:
- DnsCacheTimeout: 30 minutes (i.e. if IE received a TTL value less than 30 minutes from the server, it will be ignored).
- KeepAliveTimeout: 1 minute (i.e. a persistent TCP connection is used until it has been idle for one minute, during this 1 minute no DNS lookups will be happened).
- ServerInfoTimeOut: 2 minutes (i.e. even without Keep-Alive, if a hostname is reused every two minutes without failure, a DNS lookup is not required).
- network.dnsCacheExpiration: 1 minute.
- network.dnsCacheEntries: 20 (this value is too small).
- network.http.keep-alive.timeout: 5 minutes.
- network.dnsCacheExpiration: 1 hour.
- network.dnsCacheEntries: 512.
- network.http.keep-alive.timeout: 30 seconds.
- Reducing the number of unique hostnames in the page reduces the number of DNS lookups (this is true only if the client DNS cache is empty).
- However, reducing the number of unique hostnames has the potential to reduce the amount of parallel downloading.
- for a good compromise between reducing DNS lookups and allowing a high degree of parallel downloads, the author suggests to split the components across at least two but no more than four hostnames.
- remember that using Keep-Alive reduced DNS look-ups.