Skip to content

DNS lookup latency tuning

Where DNS latency comes from — kernel, libc, resolver, network — and the dials that actually move the numbers in a production tier where every millisecond is measured.

A web request that takes 200ms might spend 50ms in DNS resolution and nobody notices — until it spends 500ms because the resolver path is wrong. This article maps the layers DNS latency hides in: getaddrinfo, glibc options, the local stub, the recursive resolver, the network round trips. The deliverable is the knowledge of which dial actually moves which number.

How to verify

Before you tune, measure. The standard tools tell you very different things:

time host -t A example.com               # full path including local stub
dig +stats example.com                   # query time at the recursor
strace -e trace=connect,sendto -tt -p $(pgrep -f myapp) 2>&1 | head -50
sudo systemd-resolve --statistics        # cache and query counts on resolved
journalctl -u systemd-resolved -n 20 --no-pager

dig +stats reports the recursor’s view of latency, not the application’s. A time host measures the whole stack: stub cache lookup, NSS chain, system call overhead. If the gap between the two is large, the latency is in the local NSS / stub layer, not the resolver.

What’s happening

A DNS lookup from inside an application traverses multiple layers, each with its own latency:

  1. Application library call. getaddrinfo(3) in C, the equivalent in other languages. Often goes through a Name Service Switch (NSS) chain configured in /etc/nsswitch.conf — typically files (hosts file) then dns, optionally mdns, nis, etc.
  2. Local stub resolver. systemd-resolved, nscd, dnsmasq, or libnss-resolve, depending on distro and config. Caches results. May or may not be on the same host.
  3. Recursive resolver. Local Unbound, ISP recursor, public resolver (1.1.1.1, 8.8.8.8). Caches recursive results. May reach the authoritative or answer from cache.
  4. Authoritative resolution. Walks from root if uncached. Includes referral chasing, glue lookups, CNAME chasing.

Latency lives in:

  • Cache misses at any layer. Hot path is single-digit microseconds (memory cache); cold path is one or more round trips over the wire.
  • NSS chain misconfiguration. A chain that hits mdns first on every lookup adds 5-10ms even if the next layer is a fast local resolver.
  • single-request vs single-request-reopen in resolv.conf. Glibc, by default, sends A and AAAA queries in parallel on the same socket; some networks drop the responses, causing timeouts. options single-request serializes them, costing latency but increasing reliability.
  • EDNS0 fragmentation. Large responses (DNSSEC, big TXT) fragment if the recursor advertises EDNS buffer size too large. Some networks drop fragments, leading to retries.

The procedure

  1. Confirm the NSS chain. Read /etc/nsswitch.conf:

    grep '^hosts:' /etc/nsswitch.conf

    For most servers, hosts: files dns is right. If you see mymachines, mdns, nis, ask whether you need them. files dns short-circuits at the hosts file (which holds localhost, ip6-localhost, often nothing else) and goes straight to DNS otherwise.

  2. Decide on the stub. Modern Ubuntu/Fedora ship systemd-resolved on 127.0.0.53:53. This is fine for most workloads — it caches, validates DNSSEC if asked, and integrates with NetworkManager. Disable it only if you have a specific reason (running an authoritative on the host, running Unbound on 127.0.0.1, etc.).

    nscd is the older alternative; on RHEL/CentOS it is sometimes still preferred. It caches at the NSS layer, so a hot getaddrinfo does not even reach the stub socket.

  3. Tune resolv.conf options. A solid default:

    nameserver 127.0.0.53
    options edns0 trust-ad timeout:1 attempts:2 single-request-reopen
    • timeout:1 — drop a query attempt after 1 second (default is 5).
    • attempts:2 — try only twice before giving up.
    • single-request-reopen — A and AAAA in parallel, but on separate sockets so dropped responses do not interfere with the other.
    • edns0 — enable EDNS0 (needed for DNSSEC and big responses).
    • trust-ad — accept the AD flag from a trusted resolver (so AD propagates to applications that want it).
  4. Pin the recursor close. For a typical Linux server, a local Unbound or systemd-resolved with DNSStubListener=yes and upstream of a fast resolver gives you sub-millisecond hot-path lookups. If you must use a remote resolver (cloud provider’s), confirm it is in the same region — not the global anycast IP, which might route to a distant POP.

  5. Cache aggressively, with bounded TTLs. A long minimum TTL keeps things fast; a stale answer is sometimes worse than a cache miss. For systemd-resolved:

    # /etc/systemd/resolved.conf
    Cache=yes
    CacheFromLocalhost=yes

    For Unbound, see recursive resolver tuning.

  6. Eliminate cold paths with prefetch. Unbound’s prefetch: yes re-resolves popular records before they expire — keeps the hot path hot. PowerDNS Recursor has similar logic. Worth enabling if you have hundreds of QPS and want predictable tail latency.

  7. Measure tail latency. Mean DNS latency is uninteresting; p99 matters. Tools:

    • Unbound: unbound-control stats_noreset | grep histogram
    • DNSPerf: synthetic load testing
    • Prometheus + node_exporter: node_dns_* from a sidecar exporter
    • Custom: dig +stats in a loop, parse query times

    Anything north of 10ms p99 inside a datacenter suggests a problem worth tracing.

Common pitfalls

  • Forgetting that getaddrinfo is blocking. A multi-threaded application that does sync DNS lookups will pile up on a slow resolver. Use the asynchronous resolution path (c-ares, Go’s net.Resolver with PreferGo, Node’s dns.lookup vs dns.resolve).
  • AAAA queries on IPv4-only networks. If the host has no IPv6 but applications still ask for AAAA, every lookup waits for the AAAA timeout. Use options no-aaaa in resolv.conf (glibc 2.31+) or compile applications to disable AAAA.
  • search domains explosion. A long search list in resolv.conf means every short name expands to N queries. options ndots:1 reduces this if your apps use FQDNs.
  • NetworkManager / cloud-init rewriting resolv.conf. Pinned options vanish on reboot. Use the systemd-resolved drop-in or NetworkManager profile to persist them.
  • Stale glibc cache on long-running daemons. Some glibc versions cache successful lookups indefinitely inside a single process. Restart the daemon after a DNS change if you cannot wait for TTL.

In the engagements we run, DNS latency tuning is rarely about the recursor — it is about the stack between the application and the recursor. We codify the resolv.conf, stub configuration, and NSS chain in configuration management, monitor tail latency per host, and treat any sustained p99 above the SLO as a real incident. See our managed operations service for the broader operating model.