Nov 11, 2010

Local DNS caching - just do it already!

I recently encountered weird problems with my network connection at home. Everything worked, but was unbelievably slooooow. Ping showed times of ~30 ms, but I could easily see it took more time for those packets to go there and back.

I took me some time to figure out what was happening. Looking back, checking DNS server should have been one of the first things to do. Seems like first DNS server provided by my provider has been down. That meant that every DNS query timed out and then went to the second DNS which got me my response. For some reason ping did DNS query before every new package being sent. That explains its weird behaviour.

This problem got me to finally install local caching dns. I was thinking about doing it before, but I never got around to do it until now. I always thought it's gonna be a few-hour nightmare. Now I blame my previous experience with bind :-D For simple local caching bind would be overkill, so I chose dnsmasq. Using it was as simple as installing, running dnsmasq and executing
$ echo 'nameserver 127.0.0.1' > /etc/resolv.conf.head

From that point on every resolv.conf file generated by dhcpcd will have my local DNS as first DNS server to try. For this time you can add it there manually. Then you can verify your setup works by running following command twice in a row:
$ dig randomserver.com
First execution should have Query time: XX msec with XX being few tens of miliseconds. Query time for second run should be zero or very close to zero.

Congratulations. You have your very own caching server. Who knows...maybe you'll even notice some improvements in your network connection :-)

Share/Save/Bookmark

6 comments:

Post a Comment
  1. I've had this same problem with F13 and F14: DNS queries seem to resolve slow. I came up with the same solution you did.

    Where I'm at we have two firewalls, if I route through one the DNS is slow. If I route through the other it's fine. I just assumed it was a problem with my firewall. We have an Astaro if it's makes any difference.

    I never did find a "real" solution.

  2. Note that Dan built support for this right into NetworkManager from what will ship in F15 on - http://blogs.gnome.org/dcbw/2010/09/23/dont-try-to-run-honey/

  3. I probably should have been clear about two things:
    a) All of this happend on my Gentoo home machine
    b) I am not classic Fedora user probably :-)

    I am not interested in NetworokManager since I am fine with running custom configurations, running ifconfig, setting up custom routing, setting up openvpn on-demand etc. I agree it's useful for most people, but I myself prefer direct control :-)

  4. You do not need to run whole recursive server, just start nscd (part of glibc). Otherwise you should choose better title like `Local recursive DNS caching server'.

    OTOH running its own recursive server is good idea. Especially in these times of mangling DNS traffic by stupid ISPs to prevent enforce IWF black list.

  5. I also highly recommend to try these public DNS servers: 8.8.8.8 and 8.8.4.4. Ultra-fast.

  6. Those two DNS servers are of course Google's. I have nothing against them, they provide great service, but people should be aware that they will give even more information to Google (not judging, but people should realize that).