Speedup DNS requests with a local cache Debian

orjinal link: http://www.debian-administration.org/articles/390

One common server bottleneck is DNS lookups. Many common server tasks such as from looking up hostnames to write Apache logfiles and processing incoming mail require the use of DNS queries. If you’re running a high-traffic system it might be useful to cache previous lookups.

There are several different packages you can use for caching DNS requests – including bind, djbdns, dnsmasq and pdnsd.

The pdnsd package is a very simple and lightweight tool for DNS caching. It will, like many of the other systems, act as a small DNS server forwarding requests to a “real” DNS server and caching the responses.

When pdnsd is stopped it will save all the lookups which have been made against it so they may be reloaded when it starts again.

Installation is very straightforward:

apt-get install pdnsd

Once installed the software is configured via the file /etc/pdnsd.conf.

To configure the software you must do two things:

  • Configure pdnsd so that it will forward requests it doesn’t know about to a real DNS server, letting it cache those results.
  • Update your system so that DNS lookups against the newly installed cache, or proxy.

Once you’ve completed these two steps all DNS lookups upon your system will be cached, and your DNS lookups should be much faster.

Upon your Debian GNU/Linux system you configure the DNS server(s) which are being used by means of the file, /etc/resolv.conf, this file will contain a list of name servers to query, perhaps along with a search domain to be used for unqualified hosts.

To tell your server to make DNS queries against the freshly installed server you would update that file to read:

nameserver 127.0.0.1

The next thing to do is to edit the pdnsd configuration file /etc/pdnsd.conf to specify which DNS servers the cache should use for its own lookups – these will most likely be your ISPs nameservers.

Locate the section of the configuration file which starts with server and add the IP address:

#
#  Specify the IP address of the real DNS server to query against here:
#
server {
        ip=11.22.33.44;   
        timeout=30;
        interval=30;
        uptest=ping;
        ping_timeout=50;
        purge_cache=off;
}

With this setting updated you can restart the caching service:

root@itchy:/etc# /etc/init.d/pdnsd restart
Restarting proxy DNS server: pdnsd.
root@itchy:/etc#

If you wish to add more DNS servers to query against you can add them seperated by commas, or you can add multiple ip= lines such as these two examples:

       # Several IPs seperated by commas.
       ip=11.22.33.44,111.222.333.444;

       # Easier to read - one per line:
       ip=11.22.33.44;
       ip=111.222.333.444;

For more details of the supported options please consult the documentation by running “man pdnsd.conf“.

You can test the cache is working by issuing a manual request to it:

root@itchy:/etc# dig  @localhost example.com mx

;; QUESTION SECTION:
;example.com.                   IN      MX

;; AUTHORITY SECTION:
example.com.            86400   IN      SOA     dns1.icann.org. hostmaster.icann.org.

;; Query time: 2224 msec
;; SERVER: 192.168.1.50#53(192.168.1.50)
;; WHEN: Sun Apr 23 21:47:41 2006
;; MSG SIZE  rcvd: 90

Here we used the dig command (part of the dnsutils package) to lookup the MX record of the domain name example.com. Notice at the bottom it shows “Query time: 2224msec”? Lets run that same query again – if our cache is working correctly it should be significantly faster:

root@itchy:/etc# dig  @itchy example.com mx |grep time
;; Query time: 1 msec

Much faster 🙂

(Yes DNS queries are ordinarily cached to a certain extent; so you’d expect the speedup even without our explicit DNS caching server…)