Local DNS caching

Create a system to locally cache the DNS requests

This configuration uses dnsmasq to locally cache DNS requests, potentially making web browsing and certain applications that requires frequent DNS resolution faster.

If you want to benchmark this configuration, uncomment/comment the appropriate lines at /etc/resolv.conf

Depending on the location it will make a massive difference. For example, in certain data center we have this benchmark results

Benchmark from a data center: 30% faster

# optimized version, caching from opendns
# for i in {1..100}; do time dig slashdot.org @localhost; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
0.00724
# using normal opendns servers
# for i in {1..100}; do time dig slashdot.org; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
0.00942


Benchmark from the office: 311% faster

# optimized version, caching from opendns
# for i in {1..100}; do time dig slashdot.org @localhost; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
0.00747
# using normal opendns servers
# for i in {1..100}; do time dig slashdot.org; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
0.03072

Configuration summary

Debian Wheezy 7.0 Debian Wheezy 7.0
Package dnsmasq install
Files

/etc/resolv.dnsmasq ensure file exist

# open dns servers
# put here your preferred servers
nameserver 208.67.222.222
nameserver 208.67.220.220

/etc/resolv.conf ensure file exist

# Enable local DNS caching
nameserver 127.0.0.1

# disable local DNS caching (put your preferred servers)
# nameserver 208.67.222.222
# nameserver 208.67.220.220

/etc/default/dnsmasq ensure file exist

# This file has five functions: 
# 1) to completely disable starting dnsmasq, 
# 2) to set DOMAIN_SUFFIX by running `dnsdomainname` 
# 3) to select an alternative config file
#    by setting DNSMASQ_OPTS to --conf-file=<file>
# 4) to tell dnsmasq to read the files in /etc/dnsmasq.d for
#    more configuration variables.
# 5) to stop the resolvconf package from controlling dnsmasq's
#    idea of which upstream nameservers to use.
# For upgraders from very old versions, all the shell variables set 
# here in previous versions are still honored by the init script
# so if you just keep your old version of this file nothing will break.

#DOMAIN_SUFFIX=`dnsdomainname`
#DNSMASQ_OPTS="--conf-file=/etc/dnsmasq.alt"

DNSMASQ_OPTS="-r /etc/resolv.dnsmasq"

# Whether or not to run the dnsmasq daemon; set to 0 to disable.
ENABLED=1

# By default search this drop directory for configuration options.
# Libvirt leaves a file here to make the system dnsmasq play nice.
# Comment out this line if you don't want this. The dpkg-* are file
# endings which cause dnsmasq to skip that file. This avoids pulling
# in backups made by dpkg.
CONFIG_DIR=/etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new

# If the resolvconf package is installed, dnsmasq will use its output 
# rather than the contents of /etc/resolv.conf to find upstream 
# nameservers. Uncommenting this line inhibits this behaviour.
# Not that including a "resolv-file=<filename>" line in 
# /etc/dnsmasq.conf is not enough to override resolvconf if it is
# installed: the line below must be uncommented.
#IGNORE_RESOLVCONF=yes
Hooks

Post restart-daemon

#!/bin/bash
set -u
set -e


Quick Deployment
Advanced Deployment