How to Add DNS Caching to Your Web Server!

If you have a website that uses a lot of API requests (such as dealing with Twitter), you can save a lot of server load/costs simply by installing DNS caching to your web server.

Each DNS request (every time you request from any domain name using an API) takes anywhere between 0 to 500 miliseconds. Sometimes, adding DNS caching to your web server can make your website load in 1 second versus 10 seconds.

Anyways, it’s easy to do and here’s how to do it on CentOS/Fedora linux servers: (Ubuntu should be similar, just use apt-get install of yum)

First install Bind-utils:

yum install bind-utils

Do a command:

dig yahoo.com

Do it several times and note how many miliseconds it takes to retrieve the URL. Later, after we install DNSMasq, we will try dig command again to see if your web server has started caching DNS requests.

Install DNSMasq:

yum install dnsmasq

Edit the file in /etc/dnsmasq.conf:

Then add the following lines:

listen-address=127.0.0.1

cache-size=500

Next, edit the file in /etc/dhcp6c.conf:

Then add the following line:

prepend domain-name-servers 127.0.0.1;

Next, edit the file in /etc/resolv.conf:

Add the following lines with 127.0.0.1 first:

nameserver 127.0.0.1

nameserver 208.67.220.220

nameserver 8.8.8.8

nameserver 8.8.4.4

208.67.220.220 is DNS server of Open DNS, which I highly recommend as it’s FAST.

Also 8.8.8.8 and 8.8.4.4 are DNS servers of Google, which are also FAST.

You can however, decide to use any other DNS such as your hosting company’s.

You can do a test of how fast they are by doing a ping test such as:

ping 8.8.8.8

to test Google’s DNS server response times.

Next, you can start the DNSMasq so your web server starts caching, do:

/sbin/service dnsmasq start

Also, you will want to add DNSMasq to your chkconfig list so it will start up next time your web server reboots, do:

chkconfig dnsmasq on

To check all the processes running at boot-up, do:

chkconfig –list

That’s it!

Now do a dig command on yahoo.com, you should see 0 miliseconds after the first dig command!

dig yahoo.com

Share

Recommended Web Server Hacks



This entry was posted in Web Server Hacks and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Related News and Resources

    None Found